group by case with rollup - mysql

I need a table like this:
However WITH ROLLUP I got this and total load twice which is wrong.
I am using mysql:
SELECT CASE
WHEN x.MarginProc < 0 THEN '< 0'
WHEN x.MarginProc = 0 THEN '= 0'
WHEN x.MarginProc > 0 THEN '> 0'
END AS 'Range'
, SUM(x.MarginValue) AS 'MarginValue'
FROM Product t
JOIN (
SELECT d.ProdId
, SUM(d.quantity * (d.ListPrice - d.Cost) AS 'MarginValue'
, SUM(d.quantity * (d.ListPrice - d.Cost )) / SUM(d.quantity * d.ListPrice)) * 100 AS 'MarginProc'
FROM OrderDetails d
JOIN OrderHeader h
ON d.DokId = h.DokId
WHERE h.Data BETWEEN startdate AND enddate
GROUP BY d.ProdId
) x
ON x.ProdId = t.ProdId
GROUP BY CASE
WHEN x.MarginProc < 0 THEN '0. <= 0'
WHEN x.MarginProc = 0 AND x.MarginProc <= 5 THEN '1. (0, 5>'
WHEN x.MarginProc > 5 AND x.MarginProc <= 10 THEN '2. (5, 10>'
END;
Any solutions?

Related

SQLSERVER Running Balance

Actually i have problem on my query to get the running balance , i have debit and credit transaction i need one column to showing the cumulative running balance this is the code i used :
Select * From (
Select D.AccNo, H.[Date], A.AccountName, H.TrxNo,
(Case When ((D.Remark = '') or (D.Remark is Null)) Then H.Trxnote Else D.Remark End) As TrxDetailDescA,
(D.Debit * 1) AS DebitValue, (D.Credit * 1) AS CreditValue,SUM(COALESCE(D.Debit, 0) - COALESCE(D.Credit, 0)) AS Balance
From TblHeadTrans H, TblTransDetails D, TblAccount A
Where H.Guid = D.[LineNo]
And D.AccNo = A.AccountNo
And H.[Date] >= '01-01-2022' And H.[Date] <= '10-07-2022' And D.AccNo >= '1003'
group by AccNo,H.[Date],A.AccountName,H.TrxNo,D.Remark,h.Trxnote,d.Debit,d.Credit
Union All
Select D.AccNo, Null As TrxDate, A.AccountName, Null As TrxNo,
'Opening Balance' As TrxDetailDesc,
Case When (Sum(D.Debit * 1) - Sum(D.Credit *1)) < 0 then 0
Else (Sum(D.Debit * 1) - Sum(D.Credit * 1)) End As DebitValue,
Case When (Sum(D.Credit * 1) - Sum(D.Debit * 1)) < 0 then 0
Else (Sum(D.Credit * 1) - Sum(D.Debit * 1)) End As CreditValue
, SUM(COALESCE(d.Debit, 0) - COALESCE(d.credit, 0)) AS Balance
From TblHeadTrans H, TblTransDetails D, TblAccount A
Where H.guid = D.[LineNo] And D.AccNo = A.AccountNo
And d.[Date] < '01-01-2022' And D.accno = '1003'
Group By D.AccNo, A.AccountName,H.Date,H.TrxNo
) ReportData
WHERE 1=1
Order By AccNo, [Date], TrxNo
and the result showing as the picture:
the result

Limit to two results for each individual ID

How would I return two results for each individual meeting.id?
I've tried things like Row_Count() and Rank() but they seem to cause syntax errors.
This is my query which I need adapting to show the two results per meeting.id.
SELECT meeting_appointment.* FROM `meeting`
INNER JOIN meeting_appointment ON (
meeting_appointment.meeting_id = meeting.id AND meeting_appointment.pupil_id = 0 AND meeting_appointment.guardian_id = 0 AND meeting_appointment.deleted = 0
)
WHERE (
meeting.grade_id = "-1" OR meeting.grade_id IN ('87')
)
AND meeting.startTime < '2016-10-06 14:00:00' + INTERVAL 1 HOUR AND meeting.startTime > '2016-10-06 14:00:00' - INTERVAL 1 HOUR
GROUP by meeting_appointment.id
ORDER BY meeting_appointment.startTime ASC
If your meeting_appointment.id are consecutive then this should show the first two
meeting_appointment.id per meeting.id:
SELECT meeting_appointment.* FROM `meeting`
INNER JOIN meeting_appointment ON (meeting_appointment.meeting_id = meeting.id)
WHERE (meeting.grade_id = "-1" OR meeting.grade_id IN ('87'))
AND meeting.startTime < '2016-10-06 14:00:00' + INTERVAL 1 HOUR AND meeting.startTime > '2016-10-06 14:00:00' - INTERVAL 1 HOUR
AND meeting_appointment.pupil_id = 0
AND meeting_appointment.guardian_id = 0
AND meeting_appointment.deleted = 0
AND NOT EXISTS
(SELECT 1 FROM meeting_appointment ma WHERE ma.id<meeting_appointment.id-1
AND ma.pupil_id = 0
AND ma.guardian_id = 0
AND ma.deleted = 0
)
GROUP by meeting_appointment.id
ORDER BY meeting_appointment.startTime ASC
If you meeting_appointment.id are just random or non-numeric then it might need some tweaks but in order to code those teaks we'll need to know a bit more about the schema.
You don't have an aggregate function, so GROUP BY is redundant. Remove that, and add a LIMIT clause, and you should be good to go:
SELECT ma.*
FROM meeting m
INNER JOIN meeting_appointment ma ON ma.meeting_id = m.id
WHERE m.grade_id IN ('-1','87')
AND ma.pupil_id = 0
AND ma.guardian_id = 0
AND ma.deleted = 0
AND m.startTime < '2016-10-06 14:00:00' + INTERVAL 1 HOUR
AND m.startTime > '2016-10-06 14:00:00' - INTERVAL 1 HOUR
ORDER BY ma.startTime ASC
LIMIT 2;
You could also use BETWEEN for the datetime column.

Mysql IF statement return issues

I have this query where the IF lines are only partially returning correct result:
SELECT
COALESCE((SELECT COUNT(ID) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS ADDED,
COALESCE((SELECT SUM(CASE WHEN USERS_BUCKETS.STATUS='Completed' THEN 1 ELSE 0 END) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS DONE,
COALESCE((SELECT COUNT(ID) FROM USERS_LIKES WHERE USERS_LIKES.USERID = USERS.ID),0) AS NUM_LIKES,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.USER_ID=USERS.ID),0) AS FOLLOWING,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.FOLLOW_ID=USERS.ID),0) AS FOLLOWERS,
(SELECT IF(ADDED >= 5,1,0)) AS IFADDED,
(SELECT IF(DONE >= 3,1,0)) AS IFDONE,
(SELECT IF(NUM_LIKES >= 5,1,0)) AS IFNUM_LIKES,
(SELECT IF(FOLLOWING >= 5,1,0)) AS IFFOLLOWING,
(SELECT IF(FOLLOWERS >= 3,1,0)) AS IFFOLLOWERS,
(SELECT IF(ADDED >= 5,1,0) + IF(DONE >= 3,1,0) + IF(NUM_LIKES >= 5,1,0) + IF(FOLLOWING >= 5,1,0) + IF(FOLLOWERS >= 3,1,0)) AS PROGRESS
FROM USERS
WHERE USERS.ID=?
Result:
ADDED: 20
IFADDED: 1
DONE: 9
IFDONE: 0 //should be 1
NUM_LIKES: 11
IFNUM_LIKES: 1
FOLLOWING: 11
IFFOLLOWING: 0 //should be 1
FOLLOWERS: 10
IFFOLLOWERS: 0 //should be 1
PROGRESS: 2
What's wrong? All IF values should be 1 and PROGRESS should be 5.
Note: I am using PDO in php, but I don't think that matters at all.
I have no idea why your code is working. The columns are definitely not the column aliases defined earlier in the query.
To do what you want, use a subquery and simplify the logic:
SELECT u.*, (ADDED >= 5) AS IFADDED,
(DONE >= 3) AS IFDONE,
(NUM_LIKES >= 5) AS IFNUM_LIKES,
(FOLLOWING >= 5) AS IFFOLLOWING,
(FOLLOWERS >= 3) AS IFFOLLOWERS,
((ADDED >= 5) + (DONE >= 3) + (NUM_LIKES >= 5) + (FOLLOWING >= 5) +
(FOLLOWERS >= 3)
) AS PROGRESS
FROM (SELECT COALESCE((SELECT COUNT(ID) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS ADDED,
COALESCE((SELECT SUM(CASE WHEN USERS_BUCKETS.STATUS='Completed' THEN 1 ELSE 0 END) FROM USERS_BUCKETS WHERE USERS_BUCKETS.USERID = USERS.ID),0) AS DONE,
COALESCE((SELECT COUNT(ID) FROM USERS_LIKES WHERE USERS_LIKES.USERID = USERS.ID),0) AS NUM_LIKES,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.USER_ID=USERS.ID),0) AS FOLLOWING,
COALESCE((SELECT COUNT(ID) FROM FOLLOW WHERE FOLLOW.FOLLOW_ID=USERS.ID),0) AS FOLLOWERS
FROM USERS
WHERE USERS.ID = ?
) u;
This takes advantage of the fact that MySQL treats booleans as integers in a numeric context, with "1" for true and "0" for false.
EDIT: I should also note that the coalesce() is unnecessary because count() will return 0 when there are no matches.

how to get SUM() of COUNT() column in MySQL

i have the following query, tried using case statement
SELECT t.inst_id, t.inst_username, tcm.city_name,
SUM(CASE WHEN psb.pms_student_bucket_id IS NULL THEN 1 ELSE 0 END) AS not_assiged,
SUM(CASE WHEN COUNT(psb.pms_student_bucket_id) BETWEEN 1 AND 50 THEN 1 ELSE 0 END) AS '1-50',
SUM(CASE WHEN COUNT(psb.pms_student_bucket_id) > 50 THEN 1 ELSE 0 END) AS ' > 50'
FROM tbl_si_di t
JOIN tbl_city_master tcm ON tcm.city_id = t.city_ref_id
JOIN tbl_si_students tss ON tss.inst_ref_id = t.inst_id
LEFT JOIN pms_student_bucket psb ON psb.user_ref_id = tss.user_ref_id
GROUP BY t.inst_id;
I need SUM of pms_student_bucket_id column when their COUNT is '1-50' and '>50'. Right now this query is saying Invalid use of group function.
How would I SUM on COUNT of "pms_student_bucket_id" equals/between some value in mysql?
you could put it in a subquery
SELECT inst_id, inst_username, city_name,
SUM(pms_student_bucket_id IS NULL ) AS not_assiged,
SUM(num_bucket >=10 AND num_bucket <= 20) AS '10 - 20'
SUM(num_bucket <= 50) AS '1-50',
SUM(num_bucket > 50) AS ' > 50'
FROM
( SELECT t.inst_id, t.inst_username, tcm.city_name,psb.pms_student_bucket_id,
COUNT(psb.pms_student_bucket_id) as num_bucket
FROM tbl_si_di t
JOIN tbl_city_master tcm ON tcm.city_id = t.city_ref_id
JOIN tbl_si_students tss ON tss.inst_ref_id = t.inst_id
LEFT JOIN pms_student_bucket psb ON psb.user_ref_id = tss.user_ref_id
GROUP BY t.inst_id
)t1
GROUP BY inst_id;
note you can use the boolean values returned to do what you want.. aka you don't need a case statement, the boolean value returns a 1 or 0 which can then be summed..

use of average aggregate function inside if clause

i'm writing a query where i need resultant from select stmt based on conditions on avg() aggregate function,
but in mysql it is giving error as Error Code : 1111 ,
Invalid use of group function
select
if( avg(FLD_TKTS) > 1 and avg(FLD_TKTS) <= 2 , '1-2' , 0) as fld_avg_1_2,
if( avg(FLD_TKTS) > 3 and avg(FLD_TKTS) <= 4 , '3-4' , 0) as fld_avg_3_4
from tbl
group by region;
Is there is any other method to do this?
Query:
select
CASE WHEN avg(FLD_TKTS) > 1 and avg(FLD_TKTS) <= 2 THEN '1-2'
ELSE '0' END as fld_avg_1_2,
CASE WHEN avg(FLD_TKTS) > 3 and avg(FLD_TKTS) <= 4 THEN '3-4'
ELSE '0' END as fld_avg_3_4
from tbl
group by region
select
region,
if(fld_avg > 1 and fld_avg <= 2, '1-2', 0) as fld_avg_1_2,
if(fld_avg > 3 and fld_avg <= 4, '3-4', 0) as fld_avg_3_4
from
(
select
region,
avg(FLD_TKTS) fld_avg
from
tbl
group by
region
) a;
I think a subquery would be a good solution:
SELECT
IF( avg_fld_tkts > 1 and avg_fld_tkts <= 2 , '1-2' , 0) as fld_avg_1_2,
IF( avg_fld_tkts > 3 and avg_fld_tkts <= 4 , '3-4' , 0) as fld_avg_3_4
(
SELECT avg(FLD_TKTS) AS avg_fld_tkts
from tbl
group by region
) t1