How to check two conditions in mysql sum - mysql

select cust_code,occu_name
, SUM(CASE WHEN VoucherType = 'S' THEN Amount ELSE 0 END) AS salesSum
,SUM(CASE WHEN VoucherType = 'I' THEN Amount ELSE 0 END) AS interestSum
,SUM(CASE WHEN VoucherType = '' THEN Amount ELSE 0 END) AS interest_sum
,SUM(CASE WHEN VoucherType = 'P' THEN Amount ELSE 0 END) AS chequereturn_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'N' THEN Amount ELSE 0 END) AS credit_sum
,SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum
from bmwregistration, ledger_transactions
where bmwregistration.ledger_id = ledger_transactions.OccupierID and
VoucherDate >= '2012-04-01' and
VoucherDate <= '2013-02-01'
group by cust_code
How to check two conditions in sum function SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum

You are doing it in correct way.
See this Example
You just did a little mistake. All you need is to add END after that conditions like this:
SUM(CASE WHEN DrCrType = 'DR'
and VoucherDate <= '2012-04-01'
THEN Amount
ELSE (Amount*-1) END) AS opening_sum
^^^
So your whole query should be like this:
select cust_code,occu_name
, SUM(CASE WHEN VoucherType = 'S' THEN Amount ELSE 0 END) AS salesSum
,SUM(CASE WHEN VoucherType = 'I' THEN Amount ELSE 0 END) AS interestSum
,SUM(CASE WHEN VoucherType = '' THEN Amount ELSE 0 END) AS interest_sum
,SUM(CASE WHEN VoucherType = 'P' THEN Amount ELSE 0 END) AS chequereturn_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'N' THEN Amount ELSE 0 END) AS credit_sum
SUM(CASE WHEN DrCrType = 'DR'
and VoucherDate <= '2012-04-01'
THEN Amount
ELSE (Amount*-1) END) AS opening_sum
from bmwregistration, ledger_transactions
where bmwregistration.ledger_id = ledger_transactions.OccupierID and
VoucherDate >= '2012-04-01' and
VoucherDate <= '2013-02-01'
group by cust_code

WHEN PAT_DrCrType = 'DR' and VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1)) AS opening_sum

try this way
SUM(CASE WHEN DrCrType = 'DR' AND VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1) END)

Related

MYSQl using a variable to do sum

How can I use the loanAmount and amountPaid to get the balance.
SELECT (SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='DR')loanAmount,
(SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='CR')amountPaid,
(loanAmount-amountPaid)balance
SELECT SUM(CASE WHEN transactionType = 'DR'
THEN amount
ELSE 0
END) loanAmount,
SUM(CASE WHEN transactionType = 'CR'
THEN amount
ELSE 0
END) amountPaid,
SUM(CASE WHEN transactionType = 'DR'
THEN amount
ELSE -amount
END) balance
FROM loanRepayment
WHERE loanNumber='MMSE22062311'
AND transactionType IN ('DR', 'CR')

How to build a table which content becoming row

I would like to make a query on table as below:
enter image description here
b.sostatus required to become column title, and summarise the count of orderno
My SQL as below:
`
select
b.orderTime as orderDate,
sum( case when b.soStatus='00' then count(a.orderNo) else 0 end ) as Not_Allocated,
sum( case when b.soStatus='30' then count(a.orderNo) else 0 end ) as Partially_Allocated,
sum( case when b.soStatus='40' then count(a.orderNo) else 0 end ) as Allocated,
sum(case when b.soStatus='50' then count(a.orderNo) else 0 end ) as Partially_Picked ,
sum(case when b.soStatus='60' then count(a.orderNo) else 0 end ) as Picked,
sum(case when b.soStatus='63' then count(a.orderNo) else 0 end ) as Cartonized,
sum(case when b.soStatus='99' then count(a.orderNo) else 0 end ) as Closed,
case
when datediff(NOW(),b.orderTime) >=2 then '>=H-2'
when datediff(NOW(),b.orderTime) =1 then 'H-1'
when datediff(NOW(),b.orderTime) <1 then 'H' end as status
from DOC_ORDER_HEADER b
left join DOC_ORDER_DETAILS a
on
b.organizationId='ID_8COM'
and b.warehouseId='WHCPT01'
AND a.orderno = b.orderno
where b.organizationId='ID_8COM'
and b.warehouseId='WHCPT01'
`
However, it is popping error: Invalid use of group function
You don't need to use count() function on this requirement. sum(1) is enough to get the count based on your criteria.
select
b.orderTime as orderDate,
sum(case when b.soStatus='00' then 1 else 0 end) as Not_Allocated,
sum(case when b.soStatus='30' then 1 else 0 end) as Partially_Allocated,
sum(case when b.soStatus='40' then 1 else 0 end) as Allocated,
sum(case when b.soStatus='50' then 1 else 0 end) as Partially_Picked ,
sum(case when b.soStatus='60' then 1 else 0 end) as Picked,
sum(case when b.soStatus='63' then 1 else 0 end) as Cartonized,
sum(case when b.soStatus='99' then 1 else 0 end) as Closed,
case when datediff(NOW(),b.orderTime) >=2 then '>=H-2'
when datediff(NOW(),b.orderTime) = 1 then 'H-1'
when datediff(NOW(),b.orderTime) < 1 then 'H' end as status
from DOC_ORDER_HEADER b
left join DOC_ORDER_DETAILS a on b.organizationId='ID_8COM' and b.warehouseId='WHCPT01'
and a.orderno = b.orderno
where b.organizationId='ID_8COM'
and b.warehouseId='WHCPT01'
group by b.orderTime
You can also do this.
sum(b.soStatus='00')

Concat 2 queries in mysql for inventory report

I have 2 queries in my sql:
Query 1
SELECT
product_id,
product_name,
SUM(case when type = "I" then quantity else 0 end) as import,
SUM(case when type = "O" then quantity else 0 end) as export,
SUM(case when type = "R" then quantity else 0 end) as refund,
(
SUM(case when type = "R" then quantity else 0 end)
- SUM(case when type = "O" then quantity else 0 end)
+SUM(case when type = "I" then quantity else 0 end)
) as available
FROM
`i1ntl_ldn_soft_inventory_detail`
WHERE
product_id = 2772
and date(refdate) < "2018-03-09"
and date(refdate)>= "2018-03-01"
GROUP by
product_id
ORDER BY
`import` DESC
The result is
Query 2
SELECT
product_id,
product_name,
(
SUM(case when type = "R" then quantity else 0 end)
- SUM(case when type = "O" then quantity else 0 end)
+SUM(case when type = "I" then quantity else 0 end)
) as available_start
FROM
`i1ntl_ldn_soft_inventory_detail`
WHERE
product_id = 2772
and date(refdate) < "2018-03-01"
GROUP by
product_id
The result is:
Can I combine 2 those queries into 1 to get the result
Product_id, product_name, available_start, import, export, refund, available
Use conditional aggregation, and move the check on the refdate into the CASE expressions. Then, you may use a single query.
SELECT
product_id,
product_name,
SUM(CASE WHEN type = 'I' AND DATE(refdate) >= '2018-03-01' AND
DATE(refdate) < '2018-03-09' THEN quantity ELSE 0 END) AS import,
SUM(CASE WHEN type = 'O' AND DATE(refdate) >= '2018-03-01' AND
DATE(refdate) < '2018-03-09' THEN quantity ELSE 0 END) AS export,
SUM(CASE WHEN type = 'R' AND DATE(refdate) >= '2018-03-01' AND
DATE(refdate) < '2018-03-09' THEN quantity ELSE 0 END) AS refund,
SUM(CASE WHEN type IN ('R', 'I') AND DATE(refdate) >= '2018-03-01' AND
DATE(refdate) < '2018-03-09' THEN quantity ELSE 0 END) -
SUM(CASE WHEN type = 'O' AND DATE(refdate) >= '2018-03-01' AND
DATE(refdate) < '2018-03-09' THEN quantity ELSE 0 END) AS available,
SUM(CASE WHEN type IN ('R', 'I') AND DATE(refdate) < '2018-03-01'
THEN quantity ELSE 0 END) -
SUM(CASE WHEN type = 'O' AND DATE(refdate) < '2018-03-01'
THEN quantity ELSE 0 END) AS available_start
FROM i1ntl_ldn_soft_inventory_detail
WHERE product_id = 2772
GROUP by product_id
ORDER BY import DESC

how to sum sql column value into a separate column ? here to sum up NPM,NPF,NPC to a new column

SELECT DATE_FORMAT(e.`date_created`,'%d-%b-%Y') AS DATE,
SUM(CASE WHEN ps.Gender = 'M'
AND e.encounter_type<='6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS **NPM**,
SUM(CASE WHEN ps.Gender = 'F'
AND e.encounter_type<='6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381
THEN 1 ELSE 0 END) AS **NPF**,
SUM(CASE WHEN DATEDIFF(CURDATE(), ps.birthdate)<=4380
AND e.encounter_type<='6'
THEN 1 ELSE 0 END) AS **NPC**,
SUM(CASE WHEN ps.Gender = 'M'
AND e.encounter_type>'6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS OPM,
SUM(CASE WHEN ps.Gender = 'F'
AND e.encounter_type>'6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS OPF,
SUM(CASE WHEN DATEDIFF(CURDATE(), ps.birthdate)<=4380
AND e.encounter_type>'6' THEN 1 ELSE 0 END) AS OPC,
COUNT(e.`patient_id`) AS total
FROM patient_search ps
LEFT JOIN
(SELECT e.`encounter_id`,
e.`encounter_type`,
e.`patient_id`,
e.`date_created`,
e.`creator`
FROM encounter AS e
GROUP BY e.`creator`,
e.`date_created`) AS e ON e.patient_id=ps.patient_id
LEFT JOIN
(SELECT oo.`concept_id`,
oo.`value_text`,
oo.`encounter_id`
FROM obs AS oo
WHERE oo.`concept_id`='4086')AS eId ON e.`encounter_id`=eId.encounter_id
GROUP BY DATE_FORMAT(e.`date_created`,'%Y-%m-%d')
As you said that your query is working you just need to wrap it as a subquery and sum those three columns as this
Side note: DATE is a reserved word so you need to backtick it.
SELECT `DATE`,
NPM+NPF+NPC AS SUM_TREE_COLS,
OPM,
OPF,
OPC,
total
FROM (
SELECT DATE_FORMAT(e.`date_created`,'%d-%b-%Y') AS `DATE`,
SUM(CASE WHEN ps.Gender = 'M'
AND e.encounter_type<='6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS NPM,
SUM(CASE WHEN ps.Gender = 'F'
AND e.encounter_type<='6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381
THEN 1 ELSE 0 END) AS NPF,
SUM(CASE WHEN DATEDIFF(CURDATE(), ps.birthdate)<=4380
AND e.encounter_type<='6'
THEN 1 ELSE 0 END) AS NPC,
SUM(CASE WHEN ps.Gender = 'M'
AND e.encounter_type>'6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS OPM,
SUM(CASE WHEN ps.Gender = 'F'
AND e.encounter_type>'6'
AND DATEDIFF(CURDATE(), ps.birthdate)>4381 THEN 1 ELSE 0 END) AS OPF,
SUM(CASE WHEN DATEDIFF(CURDATE(), ps.birthdate)<=4380
AND e.encounter_type>'6' THEN 1 ELSE 0 END) AS OPC,
COUNT(e.`patient_id`) AS total
FROM patient_search ps
LEFT JOIN
(SELECT e.`encounter_id`,
e.`encounter_type`,
e.`patient_id`,
e.`date_created`,
e.`creator`
FROM encounter AS e
GROUP BY e.`creator`,
e.`date_created`) AS e ON e.patient_id=ps.patient_id
LEFT JOIN
(SELECT oo.`concept_id`,
oo.`value_text`,
oo.`encounter_id`
FROM obs AS oo
WHERE oo.`concept_id`='4086')AS eId ON e.`encounter_id`=eId.encounter_id
GROUP BY DATE_FORMAT(e.`date_created`,'%Y-%m-%d')
) BIGTAB

using sum,count on the same column in mysql

I have a table where i have actions and messages as columns i want to count of particular actions on particular datetime i was able to sum on every action but not count on particular action.
SELECT DATE(datetime),carpark_name,
Sum(CASE action when '2' then 1 else 0 end) as AcceptedIMG,
Sum(CASE action when '3' then 1 else 0 end) as RejectedIMG,
Sum(CASE action when '4' then 1 else 0 end) as ChangeIMG,
sum(CASE action when '23' then 1 else 0 end) as AcceptedViolation,
sum(CASE action when '24' then 1 else 0 end) as RejectedViolation,
sum(CASE action when '25' then 1 else 0 end) as SkippedViolation,
FROM customer_1.audit_trail inner join customer_1.carparks on
customer_1.audit_trail.location_id = customer_1.carparks.id
where DATE(datetime)> '2013-12-01'and DATE(datetime)< '2013-12-03' and location_id = '146'
But this is what I need adding it count(AcceptedIMG, RejectedIMG,ChangeIMG,) or (count(action(2,3,4) as review. I am not able to do this.
To get the SUM of action 2,3 and 4, as column REVIEW and 23,24,25 as CONTRAVENTIO you could do:
SELECT DATE(datetime),
carpark_name,
Sum(CASE action when '2' then 1 else 0 end) as AcceptedIMG,
Sum(CASE action when '3' then 1 else 0 end) as RejectedIMG,
Sum(CASE action when '4' then 1 else 0 end) as ChangeIMG,
SUM(CASE WHEN action IN ('2','3','4') then 1 else 0 end) as REVIEW
sum(CASE action when '23' then 1 else 0 end) as AcceptedViolation,
sum(CASE action when '24' then 1 else 0 end) as RejectedViolation,
sum(CASE action when '25' then 1 else 0 end) as SkippedViolation,
SUM(CASE WHEN action IN ('23','24','25') then 1 else 0 end) as CONTRAVENTION
FROM customer_1.audit_trail
INNER join customer_1.carparks
ON customer_1.audit_trail.location_id = customer_1.carparks.id
WHERE DATE(datetime) > '2013-12-01'
AND DATE(datetime) < '2013-12-03'
AND location_id = '146'
GROUP BY DATE(datetime),carpark_name
I just added the two columns to your query. If you don't need the individual ones, you can remove them.