How to get a distinct count based on date - obiee

I have a requirement to get a distinct count of people we offered a job, problem is since we can offer multiple jobs on potential candidate , when i write my query its counting multiple offers. Requirement is to count only the first offer, any subsequent offer should not count. any suggestions on this?

You can use this COUNT(DISTINCT...) like the following :
SELECT a.p_id, b.p_name, c.p_desc,
COUNT(DISTINCT CASE WHEN a.date BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(sysdate),-4) + 1) AND
ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) ...

Related

Oracle Sql group function is not allowed here

I need someone who can explain me about "group function is not allowed here" because I don't understand it and I would like to understand it.
I have to get the product name and the unit price of the products that have a price above the average
I initially tried to use this, but oracle quickly told me that it was wrong.
SELECT productname,unitprice
FROM products
WHERE unitprice>(AVG(unitprice));
search for information and found that I could get it this way:
SELECT productname,unitprice FROM products
WHERE unitprice > (SELECT AVG(unitprice) FROM products);
What I want to know is why do you put two select?
What does group function is not allowed here mean?
More than once I have encountered this error and I would like to be able to understand what to do when it appears
Thank you very much for your time
The phrase "group function not allowed here" is referring to anything that is in some way an "aggregation" of data, eg SUM, MIN, MAX, etc et. These functions must operate on a set of rows, and to operate on a set of rows you need to do a SELECT statement. (I'm leaving out UPDATE/DELETE here)
If this was not the case, you would end up with ambiguities, for example, lets say we allowed this:
select *
from products
where region = 'USA'
and avg(price) > 10
Does this mean you want the average prices across all products, or just the average price for those products in the USA? The syntax is no longer deterministic.
Here's another option:
SELECT *
FROM (
SELECT productname,unitprice,AVG(unitprice) OVER (PARTITION BY 1) avg_price
FROM products)
WHERE unitprice > avg_price
The reason your original SQL doesn't work is because you didn't tell Oracle how to compute the average. What table should it find it in? What rows should it include? What, if any, grouping do you wish to apply? None of that is communicated with "WHERE unitprice>(AVG(unitprice))".
Now, as a human, I can make a pretty educated guess that you intend the averaging to happen over the same set of rows you select from the main query, with the same granularity (no grouping). We can accomplish that either by using a sub-query to make a second pass on the table, as your second SQL did, or the newer windowing capabilities of aggregate functions to internally make a second pass on your query block results, as I did in my answer. Using the OVER clause, you can tell Oracle exactly what rows to include (ROWS BETWEEN ...) and how to group it (PARTITION BY...).

In Solr, how to sort documents differently based on their attributes?

My documents have these 2 fields:
status
open
upcoming
ended
end_date
Right now I'm sorting this way:
status_int ASC, end_date ASC (status_int is just a way to easily order by status open -> upcoming -> ended)
For open and upcoming events, I want to show the ones that end the soonest first, that's why I have end_date ASC. The issue is that it doesn't work well for ended events, as the user will get the events from 2010 for instance before the ones that ended in 2020.
So, I'd like to order by end_date ASC for documents that dont have the ended status, and end_date DESC for the ended ones.
Is it possible to change the order based on a document attribute?
On top of my head, an easy solution is to use streaming expressions, although the usability of them depends on some technical details (Solr version, your client implementation details etc.)
Start two search streams, one for documents which don't have the ended status, and one for others. You can specify different sort criterias for two different streams. Then merge those two streams. Something like:
list(
search(
yourCollection,
q="yourQuery",
qt="/export", //or "/select" based on your needs
fq="*:* AND NOT status:ended",
sort="end_date asc"
),
search(
yourCollection,
q="yourQuery",
qt="/export", //or "/select" based on your needs
fq="status:ended",
sort="end_date desc"
)
)
this will first return not-ended ones sorted by end_date asc, and after them status=ended ones sorted by end_date desc. You can also sort them again based on some other condition, or merge them differently based on your requirements.
Another simpler solution could be using function queries and pseudo fields, something along the lines of:
&fl=sortonthis:if(not(status=ended),rord(end_date),ord(end_date))&sort=sortonthis
asc
I didn't test but it should work with some tweaks.
If end_date is always in the future for events that haven't ended, you can probably make it work by using abs(ms(NOW, end_date)) - it'll give you a value that represents how far from the current date the end date is.

Sum on Count in hql - error Not yet supported place for UDAF 'count'

I'm new here so please be gentle, my first question after using this website for a long time regards the below:
I'm trying to create a sum of count of events in the past 30 days:
select key, sum((COALESCE(count(*),0)))
from table
Where date>= '2016-08-13'
And date<= '2016-09-11'
group by key;
but the sum doesn't seem to work. i'm looking at the last 30 days, and i would like to count any row that exists for each key, and then sum the counts (i need to count on a daily basis and then sum all day's count).
If you can offer any other way to deal with this issue i'm open for suggestions!
Many thanks,
Shira
You can't nest aggregate functions in HQL (or SQL). However, if you just want a count of records falling within range for each key, then you can simply just use COUNT(*):
select key, count(*)
from table
where date >= '2016-08-13' and
date <= '2016-09-11'
group by key;
It looks like there was a couple things wrong with your code.
I've written this for you, haven't tested it but it passes the syntax test.
SELECT COUNT(key) AS Counting FROM tblname
WHERE date>= '2016-08-13'
AND date<= '2016-09-11'
GROUP BY key;
And this might help you. You should definitely be using COUNT for this query.
I'm not sure if it's related but there might be an issue with calling a field 'key' I kept receiving syntax errors for it.
Hope I was able to help!
-Krypton

VisitorID in BigQuery doesn't match Sessions Google Analytics

I've looked around the web and I keep getting the same answer: to count sessions in BigQuery, take count(distinct concat(fullvisitorID, string(visitID))). But in some cases, that's not even getting me close to the sessions in Google Analytics. Is there any other way to count sessions better? Here's what I'm trying to do:
SELECT hits.customdimensions.value val,
count(*) as pageviews,
exact_count_distinct(CONCAT([fullVisitorId], STRING([visitid]))) sessions
FROM [xxx.ga_sessions_20150619]
where hits.customdimensions.index = 7 and lower(hits.type) = 'page'
group by val
order by pageviews desc
LIMIT 1000
For some custom dimension values, that gets close to GA, but others are off by twice the amount. Is there any way to get a better session count in BQ?
Well, I can't really speak to your GA data itself (of course, check to make sure you're not sampling the data at all), but if you run the following query, you'll pull the sum of each of the session counts per fullVisitorId:
SELECT SUM(sessionsPerUser)
FROM (SELECT fullVisitorId, COUNT(visitNumber) AS sessionsPerUser
FROM [xxx.ga_sessions_2017yyzz]
GROUP BY fullVisitorId)

Using uniq in ActiveRecord

Running this line of code
Activity.uniq.select(:ip).where("created_at > NOW() - INTERVAL 1 DAY").count
Returns the number of all lines (without the uniquness filter) although the SQL is correct (with the DISTINCT keyword)
If I run this command in 2 steps:
c = Activity.uniq.select(:ip).where("created_at > NOW() - INTERVAL 1 DAY")
c.size
Then I get the right count.
How can it be?
Thank you
Generally DISTINCT will remove duplicate results from a result set but it won't do any grouping. Since you have one row in a COUNT result, you won't see any effect. What you really want is this:
SELECT COUNT(DISTINCT activities.ip) FROM activities WHERE ...
Note that you want a count of the distinct activities, not a distinct count.
I'm not sure how you'd manage to emit this SQL using ActiveRecord. You may need to resort to using the ActiveRecord connection directly and use select_value for such an unusual sort of inquiry unless you can coach select to do it for you.

Resources