Concat 2 queries in mysql for inventory report - mysql

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

Related

How to get sum of value for specific field

-------------------------------------------------
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
(SELECT
(SUM( tshares ) AS total WHERE trtm = 'TM'),
(SUM( tshares ) AS total1 WHERE trtm = 'TR'))
FROM transfer_file
where t_date between '10/1/1992' and '10/2/1992'
-----------------------------------------------
How to get the sum of values for specific fields,i am getting the count of value but not getting the sum of value,what to do? help me if any body knows
You should use conditional aggregation inside the SUM too:
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
SUM(CASE WHEN trtm = 'TM' THEN tshares END) AS Total_Transmission,
SUM(CASE WHEN trtm = 'TR' THEN tshares END) AS Total_Transfer
FROM transfer_file
WHERE t_date between '10/1/1992' and '10/2/1992'

Grouping data by type and displaying

I have a table called 'product', with a 'created_at' date, and a 'type' which is a varchar.
Essentially what I want is an output of the COUNT(*) of each product for every day, for all of the product types. I know that there are only 5 different values for 'type', so that would narrow it down a bit with the query I imagine.
Example:
I have been playing around with this with no success, can't seem to wrap my mind around it. Any ideas?
Try this query
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
GROUP BY created_at
For specific date
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
WHERE created_at = '2015-12-21'
GROUP BY created_at
With date format
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
DATE_FORMAT(created_at,'%Y/%m/%d') AS created_date
GROUP BY created_at
HAVING created_date = '2015/12/21'

Calculate date difference in sql for specific year

I have two dates, say start_date is 20141215 and end_date = 20150115. I would like to use SQL DATEDIFF to only count the dates within the year 2015 which I will specify in the query. Here is the current SQL I have written:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(end_date, start_date)+1) as datetotals
FROM employee_leave WHERE
((YEAR(start_date) = :year) OR (YEAR(end_date) = :year))
AND employee_id = :emp_id
Thanks
You need to fix datediff() to only consider dates during the year. I think this does what you want:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(least(end_date, date(concat_ws('-', :year, 12, 31))),
greatest(start_date, date(concat_ws('-', :year, 1, 1)))
) + 1) as datetotals
FROM employee_leave
WHERE ((YEAR(start_date) = :year) OR (YEAR(end_date) = :year)) AND
employee_id = :emp_id
Make it a AND condition rather like
WHERE
((YEAR(start_date) = :year) AND (YEAR(end_date) = :year))

How to check two conditions in mysql sum

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)

SQL query - How to exclude WHERE for specific field

See the SQL query below, it work fine. It calculate the number of Yes, NOT, Other and the number of matching mobile number [Sales field] (D.MobileNo = S.mobile)
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC
I want to exclude WHERE for the Sales field - it should not be part from CheckDate. Meaning it should check any record in the dairy table without CheckDate for the Sales field.
How can that be done?
If you really want those results in only one query, this might do the trick.
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' AND UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL,
SUM(CASE WHEN UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 THEN 1 ELSE 0 END) AS TOTALINCHECKDATE
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
GROUP BY D.Username
ORDER BY TOTAL DESC
Note that "TOTAL" will count all rows (including those who where not within your CheckDate range), TOTALINCHECKDATE return the same value as in your previous query.
Obviously, this can still be optimized.
Assuming username exists also in SALES table
SELECT Username,SUM(Yes) As Yes, SUM(`Not`) As `Not`
, SUM(Other) As Other, SUM(sales) Sales, SUM(total)
FROM (
-- get diary data
SELECT username,mobileNo As mobile,
CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END as Yes,
CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END as `Not`,
CASE WHEN D.type = '' THEN 1 ELSE 0 END as Other,
0 As sales, 1 as total
FROM dairy as D
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
UNION ALL
-- get all sales
SELECT DISTINCT username,mobile, 0 as Yes, 0 as `Not`, 0 As Other, 1 As sales, 0 As total
FROM sales
WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
) AS a
GROUP BY Username
Also try your original query with date addition
SELECT D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'Not' THEN 1 ELSE 0 END) as Not,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales WHERE UNIX_TIMESTAMP(CheckDate) >= 1309474800 AND UNIX_TIMESTAMP(CheckDate) <= 1311894000 ) as S on D.MobileNo = S.mobile
WHERE source = 'Network'
AND UNIX_TIMESTAMP(CheckDate) >= 1309474800
AND UNIX_TIMESTAMP(CheckDate) <= 1311894000
GROUP BY D.Username
ORDER BY TOTAL DESC