How to sum count on specific columns in sql - sql

I want to calculate specific sum of counts
select is_known_bot, count(*)
FROM "public"."bus_request"
where app_name = 'xxxxxx' and event_type <> 'browser_js'
and is_known_bot <>''
and date <= GETDATE() and date>= GETDATE()-14
group by is_known_bot
order by is_known_bot asc
I am getting the below table:
is_known_bot count
good 2
bad 3
Human 7
in the end, i want to get the below table:
is_known_bot count
bot 5
Human 7

You can use CASE instead the column is_know_bot
Case when is_know_bot = 'Human' then is_know_bot else 'Bot' end

Related

Combining multiple rows of data to one row per id

I have a raw data that has multiple dates per category, and I use code case when category = 'referral' then min(date) end as date_referral to get earliest dates of each category per id.
However, it will not return data in a row but create row per category, as such:
id date_entered date_referral date_reply date_final
-------------------------------------------------------------------------
1 2020-12-20 null null null
1 2020-12-20 2020-12-21 null null
1 2020-12-20 null 2020-12-21 null
1 2020-12-20 null null 2020-12-24
I tried enforcing single rows by using distinct or group by (separately and together):
select distinct id
, date_entered
, case when category = 'referral' then min(date) end as date_referral
, case when category = 'reply' then min(date) end as date_reply
, case when category = 'final' then min(date) end as date_final
from data
group by id
, date_entered
, category
but it will keep returning multiple rows, with each row being calculated earliest date per category. I also tried creating cte after this code to select distinct id, date_entered, date_referral, date_reply, date_final from table but that also still returns multiple rows..
How can I combine these rows and make it return one single row?
You should not group by category.
Use conditional aggregation like this:
select id, date_entered,
min(case when category = 'referral' then date end) as date_referral,
min(case when category = 'reply' then date end) as date_reply,
min(case when category = 'final' then date end) as date_final
from data
group by id, date_entered

Return the latest date based from a range of columns

I have a query that counts the number of completed tasks as well as returning the original values.
I'd like to add a new column which returns the most recent date (in this case task_1_completed_date or task_2_completed_date but in reality there are 20 task fields)
(CASE WHEN task_1_completed_date IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN task_2_completed_date IS NOT NULL THEN 1 ELSE 0 END
) AS task_completed_total
from (select JSON_EXTRACT_SCALAR(data, '$.task1.date') as task_1_completed_date
JSON_EXTRACT_SCALAR(data, '$.task2.date') as task_2_completed_date
from table
WHERE pet_store = 'london'
)
Not sure how to proceed, should I use a subquery here to order the task completion dates?
Use order by
(CASE WHEN task_1_completed_date IS NOT
NULL THEN 1 ELSE 0 END +
CASE WHEN task_2_completed_date IS NOT
NULL THEN 1 ELSE 0 END
) AS task_completed_total
from (select JSON_EXTRACT_SCALAR(data,
'$.task1.date') as task_1_completed_date
JSON_EXTRACT_SCALAR(data,
'$.task2.date') as task_2_completed_date
from table
WHERE pet_store = 'london'
)where rownum=1 order by
task_completed_total desc
-- if rownum doesn't work use Limit 1
I think you could use GROUP BY and MAX to get the most recent date. see here: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver15

Why my CASE WHEN gave me an AGGREGATION error message?

I'm trying to make a promo grouping using one promo_code field in a month where there's a chance that a single customer_ID would have more than one transaction and could have two different promo code
SELECT customer_id AS buyer,
CASE
WHEN COUNT(DISTINCT flag_promo) = 2 THEN 'Mixed'
WHEN COUNT(DISTINCT flag_promo) = 1 AND flag_promo = 1 THEN 'Promo'
WHEN COUNT(DISTINCT flag_promo) = 1 AND flag_promo = 0 THEN 'Organic'
END AS promo_group
FROM TABLE
WHERE DATE BETWEEN '2019-04-01' AND '2019-04-30'
GROUP BY 1
ORDER BY 2
It gave me an error message :
SELECT list expression references column flag_promo which is neither grouped nor aggregated at [4:41]
Below is for BigQuery Standard SQL
#standardSQL
SELECT customer_id AS buyer,
CASE
WHEN COUNT(DISTINCT flag_promo) > 1 THEN 'Mixed'
WHEN ANY_VALUE(flag_promo) = 1 THEN 'Promo'
WHEN ANY_VALUE(flag_promo) = 2 THEN 'Organic'
END AS promo_group
FROM `project.dataset.table`
WHERE DATE BETWEEN '2019-04-01' AND '2019-04-30'
GROUP BY 1
ORDER BY 2
This is the query I think you intended to do:
SELECT
customer_id AS buyer,
CASE WHEN COUNT(DISTINCT flag_promo) = 2 THEN 'Mixed'
WHEN COUNT(DISTINCT flag_promo) = 1 AND MIN(flag_promo) = 1 THEN 'Promo'
WHEN COUNT(DISTINCT flag_promo) = 1 AND MIN(flag_promo) = 2 THEN 'Organic'
END AS promo_group
FROM TABLE
WHERE
DATE BETWEEN '2019-04-01' AND '2019-04-30'
GROUP BY 1
ORDER BY 2;
This assumes that a flag_promo value of 1 means Promo and a value of 2 means Organic. If not, then we can easily edit the above query.

select query output not as expected

i need one single query which will give result like the one i give below
createddate recordcount acceptdate submitdate createddate
27-MAR-16 24 36 11
28-MAR-16 79 207 58
for reference i am providing some queries which i want to merge into one single query
select trim(date_created) createddate,count(*) recordcount
from man
where status IN ('CREATED')and date_created>sysdate-15
group by trim(date_created) ORDER BY TO_DATE(createddate,'DD/MM/YYYY');
this query will result like the following.
createddate recordcount
27-MAR-16 11
28-MAR-16 58
the second query
select trim(DATE_SUB) submitdate,count(*) recordcount
from man
where status IN ('SUBMITTED')and DATE_SUB>sysdate-15
group by trim(date_sub) ORDER BY TO_DATE(submitdate,'DD/MM/YYYY');
result of this query is like
submitdate recordcount
27-MAR-16 36
28-MAR-16 207
and the third query is like -
select trim(DATE_PUB) acceptdate,count(*) recordcount
from man
where status IN ('ACCEPTED')and DATE_PUB>sysdate-15
group by trim(DATE_PUB) ORDER BY TO_DATE(acceptdate,'DD/MM/YYYY');
acceptdate recordcount
27-MAR-16 24
28-MAR-16 79
how can i merger these three query so that i can get count for all in single query?which will give me result like
createddate recordcount acceptdate submitdate createddate
27-MAR-16 24 36 11
28-MAR-16 79 207 58
Your first query where clause has date but second query where clause has DATE_P.
Try like this
SELECT Trim(date) createddate,
COUNT(*) recordcount,
SUM(case when status = 'A' then 1 else 0 end) as a,
SUM(case when status = 'S' then 1 else 0 end) as s,
SUM(case when status = 'C' then 1 else 0 end) as c,
SUM(case when status = 'R' then 1 else 0 end) as r
FROM man
WHERE status IN ('A','S','C','R')and date >sysdate-15
GROUP BY trim(date) ORDER BY createddate;
You seem to want to get counts for each status type, for each day. The first step is generate all the dates you're interested in, which you can do with:
select trunc(sysdate) + 1 - level as dt
from dual
connect by level <= 15;
You can then (outer) join to your actual table where any of the three date columns match a generated date, and expand your case conditions to check which one you're looking at:
with t as (
select trunc(sysdate) + 1 - level as dt
from dual
connect by level <= 15
)
select t.dt,
count(*) as recordcount,
count(case when status = 'ACCEPTED' and trunc(m.date_pub) = t.dt
then 1 end) as acceptdate,
count(case when status = 'SUBMITTED' and trunc(m.date_sub) = t.dt
then 1 end) as submitdate,
count(case when status = 'CREATED' and trunc(m.date_created) = t.dt
then 1 end) as createddate
from t
left join man m
on (m.date_pub >= t.dt and m.date_pub < t.dt + 1)
or (m.date_sub >= t.dt and m.date_sub < t.dt + 1)
or (m.date_created >= t.dt and m.date_created < t.dt + 1)
group by t.dt
order by t.dt;
I've used range checks for the join conditions - it isn't clear if all your date columns are set at midnight, but it's safer to assume they might have other times and you cant everything from the matching day.
Each of the three count results is now only of those rows which match the status and where the specific date column matches, which I think is what you want. I've used trunc() here instead of a range comparison, as it doesn't have the potential performance penalty you can see in the where clause (from it potentially stopping an index being used).
This may throw out your recordcount though, depending on your actual data, as that will include rows that now might not match any of the case conditions. You can repeat the case conditions, or use an inline view to calculate the total of the three individual counts, depending on what you want it to include and what will be the easiest for you to maintain. If those are the only three statuses in your table then it may be OK with count(*) but check it gets the value you expect.

Count total number of records based on answers

i have a result which displays two answers and i want to result the total number of counts by each record. With my query i display two answers (like and dislike). i want to count the total number of like and also the total number of dislike
SELECT (CASE WHEN log_time <= rdate_up THEN 'like' ELSE 'dislike' end )as answer
FROM dbo.users
Considering the algorithm you gave us to validate if value is a like or dislike is
log_time <= rdate_up
Then you could use union and count to separate them.
select count(*) as count_of_like
from dbo.users
where log_time <= rdate_up
union
select count(*) as count_of_dislike
from dbo.users
where log.time > rdate_up;
You can do this another way using CASE. This will be faster because it only hits the base table once.
select sum(case when log_time <= rdate_up then 1 end) as LikeCount
, SUM(case when log.time > rdate_up then 1 end) as DislikeCount
from dbo.users