Featured post
if statement - MySQL - subtracting certain rows of a table and presenting the results alongside other rows -
i have been struggling getting query right hours now. have huge amount of data , want show departments ids 10,15,18 , 25. here, want subtract profits each dept id 18 15, i.e. 15-18.
i filter data following query:
select * depttable id in(10,15,18,25) , date = '2009-01-25'
dept ---------- date ---------------- id ----------------- profit
uk ---------- 2009-01-25 ---------- 10 ---------------- 2000
brazil ------- 2009-01-25 ---------- 10 ---------------- 1300
japan------- 2009-01-25 --------- 10 ---------------- 2500
spain------- 2009-01-25 ---------- 10 ---------------- 3200
uk ---------- 2009-01-25 ---------- 15 ---------------- 4000
brazil ------- 2009-01-25 ---------- 15 ---------------- 1700
japan------- 2009-01-25 ---------- 15 ---------------- 3500
spain-------- 2009-01-25 ---------- 15 --------------- 1200
uk ---------- 2009-01-25 ---------- 18 ---------------- 2500
brazil ------- 2009-01-25 ---------- 18 ---------------- 1300
japan------- 2009-01-25 --------- 18 ---------------- 2120
spain------- 2009-01-25 ---------- 18 ---------------- 800
uk ---------- 2009-01-25 ---------- 25 ---------------- 3000
brazil ------- 2009-01-25 ---------- 25 ---------------- 1850
japan------- 2009-01-25 --------- 25 ---------------- 1580
spain-------- 2009-01-25 ---------- 25 --------------- 1070
what want subtract each row id 18 rows id 15. taking uk example:
4000 - 2500 = 1500, doing rest of dept regions give desired result is:
dept ---------- date ---------------- id ----------------- profit
uk ---------- 2009-01-25 ---------- 10 ---------------- 2000
brazil ------- 2009-01-25 ---------- 10 ---------------- 1300
japan------- 2009-01-25 --------- 10 ----------------- 2500
spain------- 2009-01-25 ---------- 10 ---------------- 3200
uk ---------- 2009-01-25 ---------- 15-18 ------------ 1500
brazil ------- 2009-01-25 ---------- 15-18 ------------ 400
japan------- 2009-01-25 ---------- 15-18 ----------- 1380
spain-------- 2009-01-25 ---------- 15-18 ----------- 400
uk ---------- 2009-01-25 ---------- 25 ---------------- 3000
brazil ------- 2009-01-25 ---------- 25 ---------------- 1850
japan------- 2009-01-25 ---------- 25 ---------------- 1580
spain-------- 2009-01-25 ---------- 25 --------------- 1070
2 points:
1. calculated rows id column doesn't have read '15-18', i've typed '15-18' explain issue
2. italics/bold calculated rows, other rows remain same
surely possible?
thanks,
i think work...
select a.dept, a.date, if(a.id=15,'15-18',a.id) id, if(b.profit null,a.profit,a.profit-b.profit) profit depttable left join depttable b on a.id=15 , b.id=18 , a.dept=b.dept a.id in(10,15,25) , a.date = '2009-01-25'
tested result:
+--------+------------+-------+--------+ | dept | date | id | profit | +--------+------------+-------+--------+ | uk | 2009-01-25 | 10 | 2000 | | brazil | 2009-01-25 | 10 | 1300 | | japan | 2009-01-25 | 10 | 2500 | | spain | 2009-01-25 | 10 | 3200 | | uk | 2009-01-25 | 15-18 | 1500 | | brazil | 2009-01-25 | 15-18 | 400 | | japan | 2009-01-25 | 15-18 | 1380 | | spain | 2009-01-25 | 15-18 | 400 | | uk | 2009-01-25 | 25 | 3000 | | brazil | 2009-01-25 | 25 | 1850 | | japan | 2009-01-25 | 25 | 1580 | | spain | 2009-01-25 | 25 | 1070 | +--------+------------+-------+--------+
- Get link
- X
- Other Apps
Comments
Post a Comment