I want count by using group two columns. I have data like this:
id sectionid date
1 1 2015-09-16
2 1 2015-09-16
3 2 2015-09-16
4 2 2015-09-16
5 3 2015-09-16
6 1 2015-09-17
7 2 2015-09-18
8 2 2015-09-18
Result will be:
Date section1count section2count section3count
2015-09-16 2 2 1
2015-09-17 1 0 0
2015-09-18 0 2 0
Thanks in advance.
You can use a combination of the SUM() function along with GROUP BY to get the result set you want:
SELECT date AS Date,
SUM(CASE WHEN sectionid=1 THEN 1 ELSE 0 END) AS section1count,
SUM(CASE WHEN sectionid=2 THEN 1 ELSE 0 END) AS section2count,
SUM(CASE WHEN sectionid=3 THEN 1 ELSE 0 END) AS section3count
FROM table
GROUP BY date
SQLFiddle
her is my result. You can compare it to count each section
SELECT
SUM(IF( m.sectionid = 1 , 1,0)) section1count,
SUM(IF( m.sectionid = 2 , 1,0)) section2count,
SUM(IF( m.sectionid = 3 , 1,0)) section3count,
m.date
FROM myt m
GROUP BY m.`date`;
Related
I need to generate average sales per Title between year 2019 to 2021. There are 2 input tables:
Title Table
Title_id Title_type Price_per
1 tv 10
2 book 50
3 cd 20
Transactions table(trans)
tran_id Title_id Qty year
1 3 2 2019
2 1 1 2019
3 3 5 2020
4 3 3 2020
5 1 10 2021
The expected result should generate below columns:
Title_id|Avg_sales_2019|Avg_sales_2020|Avg_sales_2021
title_id avg_sales_2019 avg_sales_2020 avg_sales_2021
1 10.0 NULL 100.0
3 40.0 80.0 NULL
I used below query, but it does not generate the expected output
select a.title_id,
case when a.year=2019 then avg end as Avg_sales_2019,
case when a.year=2020 then avg end as Avg_sales_2020,
case when a.year=2021 then avg end as Avg_sales_2021
from (Select t.title_id, x.year, AVG(t.Price_per*x.Qty) as avg
from title t join trans x on t.title_id=x.title_id
group by t.title_id,x.year) a;
title_id avg_sales_2019 avg_sales_2020 avg_sales_2021
1 10.0 NULL NULL
1 NULL NULL 100.0
3 40.0 NULL NULL
3 NULL 80.0 NULL
How to combine the rows for a particular title_id to get the expected result
Note: I am running the query in Hive
Use conditional aggregation:
SELECT
t.title_id,
AVG(CASE WHEN x.year = 2019
THEN t.Price_per * x.Qty ELSE 0 END) AS avg_sales_2019,
AVG(CASE WHEN x.year = 2020
THEN t.Price_per * x.Qty ELSE 0 END) AS avg_sales_2020,
AVG(CASE WHEN x.year = 2021
THEN t.Price_per * x.Qty ELSE 0 END) AS avg_sales_2021
FROM title t
LEFT JOIN trans x
ON x.title_id = t.title_id
GROUP BY
t.title_id
ORDER BY
t.title_id;
I am new to Mysql, I have the data like this,
Order_dt User_ID Sales
28-03-2022 PRPK-12 84
28-03-2022 PRPK-11 41
28-03-2022 PRPK-10 55
28-03-2022 PRPK-12 76
26-03-2022 PRPK-10 54
27-03-2022 PRPK-11 88
27-03-2022 PRPK-11 51
27-03-2022 PRPK-10 40
27-03-2022 PRPK-10 40
& I need o/p like below the format.
Order_date Unique_Cx Unique_Cx_Sales Rept_Cx Rept_Cx_Sales
26-03-2022 1 54 0 0
27-03-2022 0 0 2 219
28-03-2022 2 96 1 160
I'm getting only unique & Rept count by using the 'Having' function, but im unable to map with the sales. Kindly help.
Thanking you.
First group by dt and user id in sub query then use conditional aggregation
select order_dt,
sum(case when cd = 1 then 1 else 0 end) unique_cx,
sum(case when cd = 1 then sales else 0 end) unique_cx_sales,
sum(case when cd > 1 then 1 else 0 end) repeat_cx,
sum(case when cd > 1 then sales else 0 end) repeat_cx_sales
from
(
select order_dt, user_id,
count(*) cd,sum(sales) sales
from t
group by order_dt,user_id
) s
group by order_dt
;
I have below mentioned three tables.
Table1:
ID Ref_ID category_id Date
II-1 xrt-11-gt 1 2019-01-01 15:34:18
II-2 xtt-10-xt 1 2019-01-05 17:14:20
II-3 xyt-09-yu 2 2019-02-04 11:04:12
II-4 xet-12-ct 2 2019-02-01 12:33:14
Table2
Ref_ID2 Value
xrt-11-gt 150
xrt-11-gt 175
xrt-11-gt 165
xrt-11-gt 168
xtt-10-xt 200
xtt-10-xt 45
xyt-09-yu 34
xet-12-ct 78
Table3
ref type
1 Active
2 InActive
3 Hold
I have a query, like
select a.ID,a.Date,b.Value,c.type from Table1 a
left join Table2 b on a.Ref_ID=b.Ref_ID2
left join Table3 c on a.category_id=c.ref;
which return me below mentioned output.
ID Value Type Date
II-1 150 Active 2019-01-01 15:34:18
II-1 175 Active 2019-01-01 15:34:18
II-1 165 Active 2019-01-01 15:34:18
II-1 168 Active 2019-01-01 15:34:18
II-2 200 InActive 2019-01-05 17:14:20
II-2 45 InActive 2019-01-05 17:14:20
II-3 34 InActive 2019-02-04 11:04:12
II-4 78 InActive 2019-02-01 12:33:14
I need to convert the above output in the below mentioned format in MySQL itself.
Where, the 1-3 and 3-5 are the bifurcation of count of ID basis on the bucket it fall as per the Type.
Month Total Active 1-3 3-5 InActive 1-3 3-5 Hold 1-3 3-5
Jan-19 6 2 1 1 0 0 0 0 0 0
Feb-19 2 0 0 0 2 2 0 0 0 0
Is this what you are looking for:
SELECT
DATE_FORMAT(date, '%b-%y') month,
COUNT(*) total,
SUM(type = 'Active') active,
CASE WHEN SUM(type = 'Active') BETWEEN 1 AND 3 THEN SUM(type = 'Active') ELSE 0 END `1-3`,
CASE WHEN SUM(type = 'Active') BETWEEN 4 AND 5 THEN SUM(type = 'Active') ELSE 0 END `4-5`,
SUM(type = 'InActive') inactive,
CASE WHEN SUM(type = 'InActive') BETWEEN 1 AND 3 THEN SUM(type = 'InActive') ELSE 0 END `1-3`,
CASE WHEN SUM(type = 'InActive') BETWEEN 4 AND 5 THEN SUM(type = 'InActive') ELSE 0 END `4-5`,
SUM(type = 'Hold') hold,
CASE WHEN SUM(type = 'Hold') BETWEEN 1 AND 3 THEN SUM(type = 'Hold') ELSE 0 END `1-3`,
CASE WHEN SUM(type = 'Hold') BETWEEN 4 AND 5 THEN SUM(type = 'Hold') ELSE 0 END `4-5`
FROM
Table1 a
LEFT JOIN Table2 b on a.Ref_ID=b.Ref_ID2
LEFT JOIN Table3 c on a.category_id=c.ref
GROUP BY DATE_FORMAT(date, '%b-%y');
Note that this will not return exactly what you are showing in your expected output. Especially, there will be a few differences in the first record, that is showing aggregated data for January: total will be 6 for that month, with 4 active. But this is what I understood from your requirement.
I have 2 tables in my mySQL database:
Table Sensor
_id Name
1 Sensor1
2 Sensor2
Table History
_id Sensor_fk Date
1 1 2015-05-24
2 2 2015-05-27
3 1 2015-05-28
4 1 2015-05-28
5 2 2015-05-28
...
I need a query that return something like this:
Date Sensor1 Sensor2
2015-05-24 1 0
2015-05-27 0 1
2015-05-28 2 1
that is the count of my 2 sensors grouped by date. I will populate a bar chart with this data.
Someone can help me?
You need to do a UNION on two individual GROUP BY clauses for each Sensor. Like
(Select count(*) as Sensor1 from History h
where h.Sensor_FK = 1
group by DateField
)
Union
(Select Count(*) as Sensor2 from History h
whre h.Sensor_FK = 2
group by DateField
)
I got it.
SELECT h.date as Date,
count(CASE WHEN h.sensor_fk = 1 THEN h.sensor_fk END) as 'Sensor1',
count(CASE WHEN h.sensor_fk = 2 THEN h.sensor_fk END) as 'Sensor2'
FROM history h
GROUP BY Date
I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4