how to marge the values of column in mysql - mysql

Lets consider this query
select class_id,case when event_id=2 then sum(time_spent) end as timespent ,case when event_id=3 then sum(timespent) end as visitedtimespent from class group by class_id,event_id;
output is looking like
class_id timespent visitedtimespent
1 2000 NULL
1 NULL 10
2 4000 NULL
2 NULL 5
when I use this query
select class_id,case when event_id=2 then sum(time_spent) end as timespent ,case when event_id=3 then sum(time_spent) end as timespent from class group by class_id;
output is looking like
class_id timespent visitedtimespent
1 2000 NULL
2 4000 NULL
but I expected this output
class_id timespent visitedtimespent
1 2000 10
2 4000 5
how can I achieve this?

select class_id,
sum(case when event_id=2 then time_spent else 0 end) as timespent,
sum(case when event_id=3 then time_spent else 0 end) as visitedtimespent
from class
group by class_id

sum the case.
select class_id,
sum(case when event_id=2 then time_spent end) as timespent ,
sum(case when event_id=3 then time_spent end) as visitedtimespent
from class group by class_id;
to explain the difference:
case when id... then sum(value) is equivalent to
select case when id then value from
(
select id, sum(value) as value from table
)subquery
which is an illegal grouping(ID is not aggregated or included in grouping, so the ID value will be chosen at random between all existing entries), and your ID information will be lost. IF you then apply a case to the ID info, you will not get relevant results.

Related

How to select values by groups, using select sum()?

I need to select values by groups (from 0 to 10, from 10 to 50, more than 50).
user_id amount
1 20
1 40
2 5
3 30
3 1
Why this query doesn't work correctly?
select (select sum(amount)),
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id);
output
diapason number_of_users
0-10 1
10-50 1
>50 1
Give me hint plz
Your query has a number of issues, but primarily it will not work because you need to do the sum by user prior to the sorting into ranges. Try this instead:
SELECT CASE
WHEN amount BETWEEN 0 AND 9 THEN ' 0-10'
WHEN amount BETWEEN 10 AND 50 THEN '10-50'
ELSE '>50' END AS diapason,
COUNT(*) AS number_of_users
FROM (SELECT SUM(amount) AS amount
FROM payments
GROUP BY user_id) p
GROUP BY diapason;
Output
diapason number_of_users
0-10 1
10-50 1
>50 1
Try below way
select
sum(case when amount <10 then 1 else 0 end) as "0-10",
sum(case when amount >=10 and amount <50 then 1 else 0 end) as "0-50" , sum(case when amount>50 then 1 else 0 end) as ">50"
from table
Your syntax is incorrect:
select case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end
You can try this, writting a subquery get SUM of amount by each user_id, then do CASE WHEN, you don't need select sum(amount) in CASE WHEN.
CREATE TABLE t(
user_id int,
amount int
);
insert into t values(1,20);
insert into t values(1,40);
insert into t values(2,5);
insert into t values(3,30);
insert into t values(3,1);
Query 1:
select
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end as diapason,
count(distinct user_id) number_of_users
from (
SELECT user_id,SUM(amount) total
FROM table
GROUP BY user_id
) t1
group by
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end
Results:
| diapason | number_of_users |
|----------|-----------------|
| 0-10 | 1 |
| 10-50 | 1 |
| >50 | 1 |

Sql query for clubbing the data according to date

I have a table like this. Now I want to show the total of same dates of different status in single row.
What should be the query?
Expected Result
Created | Total1 | Total2 | Total3
2017-02-28 | 1 | 1 | 2
you could use a sum for case when for each status and group by
select
created
, sum( case when story_status ='Draft' then total else 0 end ) as Draft_count
, sum( case when story_status ='Private' then total else 0 end ) as Private_count
, sum( case when story_status ='Published' then total else 0 end ) as Published_count
from my_table
group by created
This will give you one row per date created, with columns for each story_status:
SELECT
`created`,
SUM(if(`story_status` = 'Draft',`total`,0)) as `Total Draft`,
SUM(if(`story_status` = 'Private',`total`,0)) as `Total Private`,
SUM(if(`story_status` = 'Published',`total`,0)) as `Total Published`
FROM table
GROUP BY `created`
ORDER BY `created`

query count id where 2 records condition

I have a problem to display some information from my table
Table
I want to count idxdaftar where this have kode with value only 1
or I want to count idxdaftar where this have kode with value 1 and 2
for example
1) the result count(idxdaftar) where kode with value only 1 = 1
2) the result count(idxdaftar) where kode with value 1 and 2 = 1
Help me, please...
You should be more specific about the logic behind it but in you can use conditional count using CASE expression like
sum(case when kode = 1 then 1 else 0 end)
If I got it right, try
select type, count(*)
from (
select idxdaftar, case when count(distinct kode) = count(distinct case when kode = 1 then kode end) then '1' else '1+2' end type
from (
select 50 idxdaftar, 1 kode
union all
select 51 idxdaftar, 1 kode
union all
select 50 idxdaftar, 2 kode
) tab
group by idxdaftar
having count(distinct kode) in (
count(distinct case when kode = 1 then kode end),
count(distinct case when kode in (1,2) then kode end))
) t
group by type
I think this is what you want:
select count(distinct case when kcode in (1, 2) then idxafter end)
from t;

How to do a SELECT for total from beginning until the specified date in MySQL?

I have entry table:
I need to do a SELECT to receive 'Date', 'Number of entries' (in that date), 'Total number of entries until that date'.
When I do the SELECT:
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
I've got a result table
but the column 'Total' has a wrong data.
How the correct select should be? Is this logic of select is the best and more efficient one?
Here is a demo
If it is just the 5 vs 7 that is off I think it is because that subquery in your select list, which accesses the inline view e1 (which is filtered to competitionID = 166), is not itself filtered when also utilizing the original entry table (unfiltered). You have to filter the original table to that competitionID as well.
Notice line 3 in sql below (only change)
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date
and competitionID=166) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
Fiddle - http://sqlfiddle.com/#!9/e5e88/22/0

SQL QUERY- multiple COUNT returns wrong (same) results when using in GROUP BY

I will simplfy this:
I have two SQL expressions which works OK:
First:
select count(*) as number1
from T1
where DATE1>'2012-01-01' and DATE2<'2012-12-31'
Result:13
select count(*) as number2
from T1
where DATE3>DATE2 and CURDATE()>DATE2
Result:5
But when I try to insert those two COUNTS in GROUP BY I am getting as resulyt always 13!!!
SELECT NAME,
COUNT(case when DATE1>'2012-01-01' and DATE2<'2012-12-31' then 1 else 0 end) as number1,
COUNT (case when (DATE3>DATE2 and CURDATE()>DATE2) then 1 else 0 end) as number 2
from T1
I am getting as result:
NAME NUMBER1 NUMBER2
A 5 5
B 4 4
C 4 4
But I should get:
NAME NUMBER1 NUMBER2
A 5 4
B 4 0
C 4 1
So that SUM of columns be 13 and 5 like in first two queries . What am I doing wrong?
Thank you
COUNT(expression) counts not null expressions. You can modify your query by changing the ELSE 0 to ELSE NULL or by removing it (the ELSE NULL is implied at CASE expressions):
SELECT name,
COUNT(CASE WHEN date1 > '2012-01-01' AND date2 < '2012-12-31'
THEN 1 END
) AS number1,
COUNT(CASE WHEN date3 > date2 AND CURDATE() > date2
THEN 1 END
) AS number2
FROM T1
GROUP BY name ;
Do not use count here.Use Sum. If you want to use the count then put null in else instead of 0
try:
SELECT NAME,
(select count(*) from T1 where DATE1>'2012-01-01' and DATE2<'2012-12-31') AS number1,
(select count(*) from T1 where DATE3>DATE2 and CURDATE()>DATE2) AS number2
FROM T1
GROUP BY NAME
Try this:
SELECT name,
SUM(CASE WHEN DATE1>'2012-01-01' AND DATE2<'2012-12-31' THEN 1 ELSE 0 END) AS number1,
SUM(CASE WHEN (DATE3>DATE2 AND CURDATE()>DATE2) THEN 1 ELSE 0 END) AS number2
FROM T1 GROUP BY name