I am new to sql and I would like to select specific value form a column, count them and group them by another value; the code I am using is :
SELECT COUNT (DISTINCT event_name), client_package_id
FROM userevent
WHERE event_happened_at >= from_iso8601_timestamp('2020-10-19T11:44:24Z')
GROUP BY client_package_id
I am expecting 3 columns, one "client_package_id" other "event_name" which is grouped by specific values in the column and the other one is the quantity of column 2.
Here is the example :
If I'm reading it correctly, you need to also group by event_name, and include it in the SELECT.
eg
SELECT client_package_id, event_name, COUNT (*)
FROM userevent
WHERE event_happened_at >= from_iso8601_timestamp('2020-10-19T11:44:24Z')
GROUP BY client_package_id, event_name
If that doesn't return what you're after, please provide more details - ie sample data and expected result.
SELECT client_package_id, event_name, COUNT (event_name)
FROM userevent
WHERE event_happened_at >= from_iso8601_timestamp('2020-10-19T11:44:24Z')
GROUP BY client_package_id, event_name
SELECT client_package_id, event_name, COUNT(event_name) as 'event_quantity'
FROM userevent
WHERE event_happened_at >= from_iso8601_timestamp('2020-10-19T11:44:24Z')
GROUP BY client_package_id, event_name
Related
I'd like to, based on the first 6 columns, calculate the desired count. For each partition of user_id, session_id and orig_id, ordered by rank_agse ascending, I'd like to, starting in 1, add one every time the lag_agse column equals 'ACCESSED'. Please find it populated to illustrate what I would want in the table below.
It seems to me that you are looking for
select user_id, session_id, orig_id, type, lag_agse, rank_agse,
count(case when type = 'ACCESSED' then 1 end)
over (partition by user_id, session_id, orig_id
order by rank_agse) as desired_count
from your_table
order by user_id, session_id, orig_id, rank_agse desc
;
See my Comment under your question regarding ascending vs descending order by RANK_AGSE.
Note that count() does the same job as summing over 1 when type is 'ACCESSED' and 0 otherwise - and it does the same job in a simpler way.
I have a attendance table as below i want group them by time and section,status is null mean that the employee is absent :
Any idea how to generate output like below?
my current code :
SELECT TIME,COUNT(SECTION) AS SECTION,COUNT(STATUS) AS COUNT
FROM attendance_record
GROUP BY TIME,SECTION
ORDER BY TIME
If I understand your question, just use conditional aggregation:
SELECT TIME, SECTION, COUNT(*) as TOTAL,
COUNT(STATUS) AS IN, ( COUNT(*) - COUNT(STATUS) ) as ABSENT
FROM attendance_record
GROUP BY TIME, SECTION
ORDER BY TIME
Please help me with SQL query.
My table structure with data is:
How can select all rows without duplicates Event_ID?
And Event_NAME predominantly must contain http://
But if does not exist Event_NAME with http://, then Event_NAME must contain Connect
Finally selection result assumed
Syntax Oracle.
Thank all in advance for help.
If I understand correctly, you want to select all rows with Event_NAME containing 'http://', and then select any events not in the first set that have 'Connect' in Event_NAME.
I'm assuming that the only possibilities are the ones you show above - either there's two entries (http and Connect) for an event, or there's just 'Connect' - though this query could work for other situations.
The query is a union between 1. all events with 'http://' in the Event_NAME, and 2. events that don't have an Event_ID in the first set and that have 'Connect' in their Event_NAME.
There are probably prettier ways to do this, but it works in Oracle with the test data:
SELECT * FROM eventtest WHERE Event_NAME LIKE 'http://%'
UNION
SELECT * FROM eventtest
WHERE Event_ID NOT IN
(SELECT Event_ID FROM eventtest WHERE Event_NAME LIKE 'http://%');
here are 2 approaches requiring only a single pass of the data:
SELECT
Event_ID
, MAX(Event_NAME) AS Event_NAME
FROM eventtest
GROUP BY
Event_ID
;
SELECT
ID
, Event_ID
, Event_NAME
FROM (
SELECT
ID
, Event_ID
, Event_NAME
, ROW_NUMBER() OVER (PARTITION BY Event_ID ORDER BY Event_NAME DESC) AS rn
FROM eventtest
) dt
WHERE rn = 1
;
In the following sql fiddle, how would I change the view to get the desired output?
http://sqlfiddle.com/#!6/a737a/1
VIEW
select
sum(dollars) as totalDollars,
sum(dollars)/count(id) as factor,
count(id) as numberOfEvents,
id as eventID,
event_date
from
events
group by
id,
event_date
Query
select
*
from eventStats
where
event_date between '1/1/2015' and '1/16/2015'
desired output
The numberOfevents should = 2 (the actual number of events, not the number of records for each event, determined by the where clause in the query) to properly do the math in the view.
You can count distinct fk_id without the group by clause:
select count(distinct fk_id) as number_of_IDs
from [myTable]
where [someCondition]
Use distinct keyword in count function:
select
count(distinct fk_id) as number_of_IDs
,id
from
myTable
where
someCondition
group by
id
So this is probably a simple question, but here goes. I have some filtered data that get via a query like this:
SELECT DISTINCT account_id, count(*) as filtered_count
FROM my_table
WHERE attribute LIKE '%filter%'
GROUP BY account_id
ORDER BY account_id
This gives me an output table with two columns.
I'd like to add a third column,
count(*) as total_count
that counts the total number of occurrences of each account_id in the entire table (ignoring the filter).
How can I write the query for this three column table?
You can put a case expression inside the count function, then remove your where clause:
SELECT account_id,
count(case when attribute LIKE '%filter%' then 1 end) as filtered_count,
count(*) as total_count
FROM my_table
GROUP BY account_id
ORDER BY account_id;
Using DISTINCT although not actually harmful to your query, was redundant due to the grouping, so I have removed it.
You'll have to use a case statement for counting with your filter:
SELECT DISTINCT account_id,
count(case when attribute LIKE '%filter%' then 1 else null end) as filtered_count,
count (*)
FROM my_table
GROUP BY account_id
ORDER BY account_id