bifurcating the values basis bucket and count using MySQL - mysql

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.

Related

How to use count in sql based on a IF condition

From this table
groupId
flag
flagValue
1
0
500
2
0
100
1
1
10
2
1
50
3
0
100
1
1
200
3
1
1000
2
1
50
I need this result
groupId
flag1
flag0
valFlag1
valFlag0
totalFlags
1
2
1
210
500
3
2
2
1
100
100
3
3
1
1
1000
100
2
where
flag1 is number of times flag is 1 for a particular group
flag0 is number of times flag is 0 for a particular group
valFlag1 is sum of flagVal when flag is 1
valFlag0 is sum of flagVal when flag is 0
totalFlags is sum of total flags associated with a group
I am stuck as to how to actually count values based on an IF condition.
Anyhelp is appreciated. Thanks.
I have used a table named group_table with your values
Try using this:
SELECT
g.`groupId`,
SUM(g.`flag`=1 ) AS flag1,
SUM(g.`flag`=0) AS flag0,
SUM(CASE WHEN g.`flag`=1 THEN g.`flagValue` ELSE 0 END) AS valFalg1,
SUM(CASE WHEN g.`flag`=0 THEN g.`flagValue` ELSE 0 END) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`
If you have to use the IF,
SELECT
g.`groupId`,
IF(g.`flag`=1,1,0 ) AS flag1,
IF(g.`flag`=0,1,0) AS flag0,
SUM(IF(g.`flag`=1,g.`flagValue`,0 )) AS valFalg1,
SUM(IF(g.`flag`=0,g.`flagValue`,0 )) AS valFalg0,
COUNT(*) AS totalFlags
FROM
`group_table` g
GROUP BY g.`groupId`, flag1, flag0
They'll produce the same result

Unique & Rept Customer Sales based on date

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
;

How to join two tables but not to repeat rows in mysql

I am facing one issue for getting data. I am new in Mysql. Firstly i will show my table structure
order_products
id user_id product_id product_name
1 1 10 Jacket1
2 1 10 Jacket2
order_products_sizes
id order_product_id size qty
1 1 S 56
2 1 M 36
3 1 XL 36
4 1 2XL 56
5 2 S 32
6 2 M 28
7 2 XL 28
8 2 2XL 32
9 2 3XL 69
My expected Output:-
product_name S M XL 2XL 3XL
JACKET1 56 36 36 56
JACKET2 32 28 28 32 69
for first row 3xl would be empty beacuse there is no size available in order_product_sizes
Actually i am using join but when i use join the rows are repeating because of joining two table that is actual behavior of joins.
I have tries so far:-
SELECT order_products.product_name,
CASE
WHEN order_product_sizes.order_product_id = order_products.id AND
order_product_sizes.size = 'L' THEN order_product_sizes.qty
END AS L
from order_products
join
order_product_sizes
on order_products_sizes.order_product_id = order_products.id;
You can try below - using conditional aggregation
SELECT order_products.product_name,
max(CASE
WHEN
order_product_sizes.size = 'S' THEN order_product_sizes.qty
END) AS S,
max(CASE
WHEN
order_product_sizes.size = 'M' THEN order_product_sizes.qty
END) AS M,
max(CASE
WHEN
order_product_sizes.size = 'XL' THEN order_product_sizes.qty
END) AS XL,
max(CASE
WHEN
order_product_sizes.size = '2XL' THEN order_product_sizes.qty
END) AS '2XL',
max(CASE
WHEN
order_product_sizes.size = '3XL' THEN order_product_sizes.qty
END) AS '3XL'
from order_products join order_products_sizes
on order_products_sizes.order_product_id = order_products.id
group by order_products_sizes.order_product_id

MYSQL get multiple fields count using Group by multiple fields by date

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`;

Enter the same value for the entire year

I have table Payments
Client Dt Payment
1 201311 10
1 201312 0
2 201401 0
1 201402 0
1 201403 0
And i want select where i add to this select column "OwnerFlag", where if the client has paid in that year then in all rows for that year for that client will be OwnerFlag 1, otherwise its 0. So the final select should look like :
Client Dt Payment OwnerFlag
1 201311 10 1
1 201312 0 1
2 201401 0 0
1 201402 0 0
1 201403 0 0
Thank you.
Presuming that DT is a datetime column:
SELECT Client, Dt, Payment,
OwnerFlag = CASE WHEN EXISTS
(
SELECT 1 FROM dbo.Payments p1
WHERE p1.ClientID = p.ClientID
AND YEAR(p1.Dt) = YEAR(p.Dt)
AND p1.Pament <> 0
) THEN 1 ELSE 0 END
FROM dbo.Payments p