I have made a sql query:
select Count (UserId)
from PlanData
where AirlineCode='cl'
and DateStart>'2019-07-01'
and DateEnd<'2019-07-31'
group by UserId
This will give me false results because actually I would like to exclude UserIds completely from the query which have GeneralEventCode='code1','code2' in it
From your comment I think that you need a HAVING clause and not a WHERE clause:
select Count(UserId)
from PlanData
where AirlineCode='cl'
and DateStart>'2019-07-01'
and DateEnd<'2019-07-31'
group by UserId
having Count(case when GeneralEventCode in ('code1', 'code2') then 1 end) = 0
This conditional COUNT() in the HAVING clause will filter out any user with any occurrence of 'code1' or 'code2' in the column GeneralEventCode.
Also recheck your condition about the dates, maybe you need >= and <=.
Related
folks!
Could someone please explain to me this CASE statement? I'm puzzled about the THEN user_id, what does it does exactly?
SELECT modal_text,
COUNT(DISTINCT CASE
WHEN ab_group = 'control' THEN user_id
END) AS 'control_clicks'
FROM onboarding_modals
GROUP BY 1
ORDER BY 1;
Thanks in advance!
This is simple aggregation:
COUNT(DISTINCT user_id)
and it counts all the distinct non null user_ids.
But this is conditional aggregation:
COUNT(DISTINCT CASE WHEN ab_group = 'control' THEN user_id END)
and it counts the distinct non null user_ids only if in the same row the column ab_group contains the value 'control'.
For an AB test , the select statement is trying to find out the the count of distinct users in control_group.
So instead of counting all distinct users for each modal_text, the case is counting the user only if it is in control_group i.e. the column ab_group = 'control'
THEN is a conditional statement.
To explain you more clearly,
If your ab_group column has value 'control' then print user_id column
It's similar to if else statement
if (ab_group = 'control')
{
user_id
}
Use below link to understand more,
https://www.w3schools.com/sql/sql_case.asp
I want to count the number of ABC group using id.
SELECT group, count(id) as total FROM `user` WHERE group=`ABC`;
What's wrong?
Many thanks.
Include the columns in the select list in group by clause when using aggregate functions.
SELECT group, count(id) as total FROM user
WHERE group=`ABC`
GROUP BY group
Else simply get the count with out using other columns in the select statement.
SELECT count(id) as total FROM user
WHERE group=`ABC`
Try this:
SELECT group, count(id) as total FROM `user`
group by group having group like 'ABC';
If you want to get COUNT of users, who has the "group" field = "ABC"
SELECT count(id) as total FROM user WHERE group='ABC';
Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)
I'm not too sophisticated with SQL, I don't know how to compare a count back to a column I'm selecting in a Query in Access. One column is generated as a count that counts IDs, but I want to verify it's correct (compare the column GroupCount with a SQL Count clause)
Here is what I have at the moment:
SELECT GroupID, GroupCount, COUNT(*) as A
FROM Table1 GROUP BY GroupID, GroupCount
How can I add a where clause that will compare "A" to GroupCount?
You should be able to use a having in your current query.
SELECT GroupID
,GroupCount
,COUNT(*) AS A
FROM Table1
GROUP BY GroupID
,GroupCount
HAVING COUNT(*) = GroupCount
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
I have a legacy MySQL table called lnk_lists_addresses with columns list_id and address_id. I'd like to write a query that reports all the cases where the same list_id-address_id combination appears more than once in the table with a count.
I tried this...
SELECT count(*), list_id, address_id
FROM lnk_lists_addresses
GROUP BY list_id, address_id
ORDER BY count(*) DESC
LIMIT 20
It works, sort of, because there are fewer than 20 duplicates. But how would I return only the counts greater than 1?
I tried adding "WHERE count(*) > 1" before and after GROUP BY but got errors saying the statement was invalid.
SELECT count(*), list_id, address_id
FROM lnk_lists_addresses
GROUP BY list_id, address_id
HAVING count(*)>1
ORDER BY count(*) DESC
To combine mine and Todd.Run's answers for a more "complete" answer. You want to use the HAVING clause:
http://dev.mysql.com/doc/refman/5.1/en/select.html
You want to use a "HAVING" clause. Its use is explained in the MySQL manual.
http://dev.mysql.com/doc/refman/5.1/en/select.html
SELECT count(*) AS total, list_id, address_id
FROM lnk_lists_addresses
WHERE total > 1
GROUP BY list_id, address_id
ORDER BY total DESC
LIMIT 20
If you name the COUNT() field, you can use it later in the statement.
EDIT: forgot about HAVING (>_<)