I need to calculate the value of indebtedness column so that when openingBalance !=0 then indebtedness = openingBalnce+SalesTotal-SalesReturn. But, when openingBalnce = 0 then indebtedness = indebtedness of the previous monthSales with the same SalesID. If the previous value = 0 get the previous value and continue get previous value till have value in this column:
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
---------- ------- -------------- ---------- ----------- ------------
1 1 352200 0 5600 Null
------------------------------------------------------------------------
1 2 50000 1100 0 Null
------------------------------------------------------------------------
1 3 9500 6000 0 Null
------------------------------------------------------------------------
2 1 0 0 1200 Null
------------------------------------------------------------------------
2 2 0 300 0 Null
------------------------------------------------------------------------
2 3 0 500 1000 Null
------------------------------------------------------------------------
3 1 0 600 0 NULL
------------------------------------------------------------------------
3 2 0 200 0 NULL
-----------------------------------------------------------------------
3 3 0 0 10 NULL
-----------------------------------------------------------------------
.
.
.
12 1 0 0 0 NULL
----------------------------------------------------------------------
12 2 0 0 0 NULL
----------------------------------------------------------------------
12 3 0 0 0 NULL
And Output like this:
when openingBalance !=0 then Indebtednes=openingBalnce+SalesTotal-SalesReturn
when openingBalnce =0 then Indebtednes=Indebtednes (of the previous
month of the same SalesID)+SalesTotal-SalesReturn.
And this is the output i want.
SalesMonth SalesID openingBalance SalesTotal SalesReturn Indebtednes
---------- ------- -------------- ---------- ----------- ------------
1 1 352200 0 5600 346600
------------------------------------------------------------------------
1 2 50000 1100 0 51100
------------------------------------------------------------------------
1 3 9500 6000 0 15500
------------------------------------------------------------------------
2 1 0 0 1200 345400
------------------------------------------------------------------------
2 2 0 300 0 51400
------------------------------------------------------------------------
2 3 0 500 1000 15000
------------------------------------------------------------------------
3 1 0 600 0 346000
------------------------------------------------------------------------
3 2 0 200 0 51600
-----------------------------------------------------------------------
3 3 0 0 10 14990
-----------------------------------------------------------------------
.
.
.
12 1 0 0 0 NULL
----------------------------------------------------------------------
12 2 0 0 0 NULL
----------------------------------------------------------------------
12 3 0 0 0 NULL
you could try like below cte query
declare #tb table(SalesMonth int,SalesID int,
openingBalance money,SalesTotal money,SalesReturn money,Indebtednes money)
insert into #tb(SalesMonth,SalesID,openingBalance,SalesTotal,SalesReturn,Indebtednes)
values (1,1,352200,0,5600,Null)
,(1,2,50000,1100,0,Null)
,(1,3,9500,6000,0,Null)
,(2,1,0,0,1200,Null)
,(2,2,0,300,0,Null)
,(2,3,0,500,1000,Null)
,(3,1,0,600,0,NULL)
,(3,2,0,200,0,NULL)
,(3,3,0,0,10,NULL)
;with t1 as (select *, row_number() over
(order by salesid,SalesMonth) as rno from #tb),
t2(inde,rno,salid,mnth)as
(select case when openingBalance !=0 then
openingBalance+SalesTotal-SalesReturn
when openingBalance =0 then 0 end as inde,
rno,SalesID,SalesMonth from t1 where rno=1
union all
select case when openingBalance !=0 then
openingBalance+SalesTotal-SalesReturn
when openingBalance =0 then
case when SalesID=salid then inde+SalesTotal-SalesReturn
else 0 end end,
t1.rno,t1.SalesID,SalesMonth from t2 join t1 on t2.rno+1=t1.rno )
select SalesMonth,SalesID,openingBalance,
SalesTotal,SalesReturn,inde as Indebtednes from t1
inner join t2 on t1.SalesID=t2.salid and
t1.SalesMonth=t2.mnth order by mnth,salid
Related
I'm trying to count the number of occurences based severity level (1-5) on distinct dates. Note I have another table but severity levels are words (High, Medium and Low...not 1 to 5).
Example of DB:
DATE LEVEL COUNT
---- ----- -----
05/11/2018 3 14
05/11/2018 5 11
05/11/2018 5 11
05/12/2018 3 14
05/12/2018 2 14
05/13/2018 2 11
05/13/2018 1 12
Expected output
Date 1 2 3 4 5
--------- -- -- -- -- --
05/11/2018 0 0 14 0 22
05/12/2018 0 14 14 0 0
05/13/2018 12 11 0 0 0
Expected output 2
Level 05/11/2018 05/12/2018 05/13/2018
--------- ---------- ---------- ----------
1 0 0 12
2 0 14 11
3 14 14 0
4 0 0 0
5 22 0 0
I tried
SELECT CONCAT(DAY(`DATE`) ,MONTH(`DATE`) , YEAR(`DATE`)) AS DDMMYYYY ,
COUNT(DISTINCT LEVEL) as NumCount
FROM `myDatabase`
GROUP BY CONCAT(DAY(`DATE`),MONTH(`DATE`), YEAR(`DATE`) )
but I'm getting the number of different counts..
Any guidance would be appreciated! Thx!
You can't really do pivot tables in MySQL. However with a fixed number of columns (such as expected output #1) you can simulate them with CASE statements e.g.
select date_format(date, '%d%m%Y') as Date,
sum(case when level=1 then count else 0 end) as `1`,
sum(case when level=2 then count else 0 end) as `2`,
sum(case when level=3 then count else 0 end) as `3`,
sum(case when level=4 then count else 0 end) as `4`,
sum(case when level=5 then count else 0 end) as `5`
from table1
group by Date
Output:
Date 1 2 3 4 5
11052018 0 0 14 0 22
12052018 0 14 14 0 0
13052018 12 11 0 0 0
I want to combine 2 rows with the same date but have different values in different columns. like this:
tgl qty_NEW MIO M3 125 CW-OTR qty_NEW-SOUL-GT-125-OTR
---------- ------------------------- -------------------------
2016-05-01 0 0
2016-05-02 0 0
2016-05-05 0 0
2016-05-09 0 0
2016-05-10 2 0
2016-05-10 0 1
There is the same 2016-05-10 with the values are 2 - 0, and 0 - 1.
What I want is like this:
tgl qty_NEW MIO M3 125 CW-OTR qty_NEW-SOUL-GT-125-OTR
---------- ------------------------- -------------------------
2016-05-01 0 0
2016-05-02 0 0
2016-05-05 0 0
2016-05-09 0 0
2016-05-10 2 1
And the query I use is like this:
SELECT DISTINCT DATE(m.tgl_fj) AS tgl ,
CASE brg.nama_brg WHEN 'NEW MIO M3 125 CW-OTR' THEN SUM(d.qty) ELSE '0' END AS 'qty_NEW MIO M3 125 CW-OTR' ,
CASE brg.nama_brg WHEN 'NEW-SOUL-GT-125-OTR' THEN SUM(d.qty) ELSE '0' END AS 'qty_NEW-SOUL-GT-125-OTR'
FROM tb_mt_fj m
LEFT OUTER JOIN tb_dt_fj_brg d
ON m.ucode_fj=d.ucode_fj
LEFT OUTER JOIN tb_m_brg brg
ON d.ucode_brg=brg.ucode_brg
LEFT OUTER JOIN tb_m_grp_brg grp
ON brg.ucode_grp_brg=grp.ucode_grp_brg
WHERE brg.ucode_grp_brg='11040000000089' AND MONTH(m.tgl_fj)=MONTH(NOW())
GROUP BY DATE(m.tgl_fj), brg.nama_brg
ORDER BY DATE(m.tgl_fj)
So, how to make the query result the same as I want?
This query should do it
select tgl, sum(qty_NEW MIO M3 125 CW-OTR) as `left`, sum(qty_NEW-SOUL-GT-125-OTR) as `right` from `table` group by tgl
First table :
UserId UserName
1 User1
2 User2
3 User3
4 User4
Second Table
Userid Mark Aptitude English Technical Status
1 40 1 0 0 S
1 30 0 1 0 F
2 60 0 0 1 S
2 75 0 1 0 F
2 25 0 1 0 F
3 45 1 0 0 F
3 45 1 0 0 D
3 50 0 0 1 F
3 50 0 0 1 F
I have this two table. I need a query to get the each user average mark in English, Aptitude and Technical. The average should be calculated only for status F. The result should be like this
UserId AptitudeAverage EnglishAverage TechnicalAverage
1 0 30 0
2 0 50 0
3 45 0 50
4 0 0 0
Try this:-
SELECT userID, IFNULL(AVG(case when Aptitude = 1 then Mark * Aptitude end), 0) AS AptitudeAverage,
IFNULL(AVG(case when English = 1 then Mark * English end), 0) AS EnglishAverage,
IFNULL(AVG(case when Technical = 1 then Mark * Technical end), 0) AS TechnicalAverage
FROM YOUR_TAB
WHERE Status = 'F'
GROUP BY userID;
This might help you.
Here is the fiddle.
http://sqlfiddle.com/#!9/e449f/21
Here i am trying to get the count of products by using case when in mysql .
My sample data is
ID | Proname | led | lcd | hd | fullhd | 3d | displaysize (inches) | brandID
1 tv1 1 0 0 1 0 22 3
2 tv2 0 1 1 0 0 26 3
3 tv3 1 0 1 0 0 32 3
4 tv4 1 0 0 1 1 55 3
5 tv5 1 0 0 1 0 42 3
Now my expected out put
lcdcnt | ledcnt | hdcnt | fullhdcnt | 3dcnt | dispcntlessthan32 | displaycntbetwwen32and42 | displaycntabove42
1 4 2 3 1
Here is my Query . but i am not getting the correct output as i expected
select
sum(lcdtv) lcdcnt,
sum(ledtv) ledcnt,
sum(3dtv) 3dcnt,
sum(plasmatv) plasmacnt,
sum(smarttv) smatcnt,
sum(hdtv) hdnt,
sum(fullhdtv) fullhdcnt,
sum(ultrahdtv) ultrahdcnt,
sum(4ktv) 4kcnt,
sum(8ktv) 8kcnt,
sum(oledtv) oledcnt,
case
when (displayinches between 1 and 32) then count(displayinches)
end as dispcntlessthan32
case
when (displayinches between 32 and 42) then count(displayinches)
end as displaycntbetwwen32and42
from
tv
where
brandID = 3 and (ledtv = 1) and price != 0
Here is the SQLFiddel Demo
Below is the Approach :
select
sum(lcd) lcdcnt,
sum(led) ledcnt,
sum(3d) 3dcnt,
sum(hd) hdnt,
sum(fullhd) fullhdcnt,
sum(3d) 3dcnt,
sum(case when displaysize between 1 and 32 then 1 else 0 end) as dispcntlessthan32,
sum(case when displaysize between 33 and 42 then 1 else 0 end) as displaycntbetween32and42
from table1
where brandID = 3
Product Table
id name description price
1 AAA AAAAAA 10.00
2 BBB BBBBBB 12.00
3 CCC CCCCCC 15.00
4 DDD DDDDDD 8.00
5 EEE EEEEEE 12.50
Sales Table
trackid productid affiliateid paymentstatus refundid
1 2 1 COMPLETED 1
2 2 0 DONE null
3 3 1 COMPLETED null
4 3 0 COMPLETED null
5 3 0 COMPLETED null
6 5 5 DONE null
7 5 0 COMPLETED 2
8 5 2 COMPLETED null
9 2 0 DONE null
10 3 1 COMPLETED 3
For Sales table
If there is no affiliate for a particular sale, then affiliateid would be 0 otherwise affiliateid (I have another table for user which lists vendors and affiliates)
If a particular sales is been refunded (for any reason), then refundid would be set to some value, otherwise it would be null
paymentstatus can have any 1 of 2 values, COMPLETED and DONE
Now I need query that list following data for each product in product table
productid name totalsales affsales refunds
1 AAA 0 0 0
2 BBB 1 1 1
3 CCC 4 2 1
4 DDD 0 0 0
5 EEE 2 1 1
totalsales: sales with paymentstatus as "COMPLETED"
affsales: sales with paymentstatus as "COMPLETED" and affiliateid NOT EQUAL TO 0
refunds: sales with paymentstatus as "COMPLETED" and refundid NOT null
How can I form this particular query?
select
p.name,
SalesSummary.productid,
COALESCE( SalesSummary.TotalSales, 0 ) TotalSales
COALESCE( SalesSummary.AffSales, 0 ) AffSales
COALESCE( SalesSummary.Refunds, 0 ) Refunds
from
product p
left join
( select s.productid,
count(*) TotalSales
sum( if( s.affiliateID > 0, 1, 0 )) affSales,
sum( if( ifnull( s.RefundID, 0 ) > 0, 1, 0 )) Refunds
from
sales s
where
s.PaymentStatus = 'COMPLETED'
group by
s.productid
) SalesSummary
on p.id = SalesSummary.productid