I have asked a similar question previously, but posting a new one as I do not want to confuse other
members, and there is an additional column.
What I am looking for is to update the column ItemActual. This needs to be updated with the difference with ItemValue for the latest CurrentTime for the same StartTime if any.
If there is no other entry for the same StartTime other than the current row, it needs to be the ItemValue itself. The comparisons are only to be done for items with the same name.
For eg, Rownum 283, ItemActual = 347013 (since there is no other row with same StartTime).
This applies to row 235 as well, i.e. ItemActual = 1086054.00
For row 190, this needs to be 664031.00 - 533023.00 (comparing with row 145) = 131008
But for row 10, this will be 532023.00, since there is no earlier entry of same item with same StartTime.
Rownum Name ItemValue CurrentTime StartTime
283 ABC 347013.00 3/05/2012 16:01 29/04/2012 6:29
235 ABC 1086054.00 26/03/2012 14:05 7/03/2012 21:18
190 ABC 664031.00 13/02/2012 13:42 29/01/2012 6:39
145 ABC 533023.00 7/02/2012 14:01 29/01/2012 6:39
100 ABC 532023.00 7/02/2012 13:33 29/01/2012 6:39
55 ABC 532023.00 7/02/2012 12:52 29/01/2012 6:39
10 ABC 532023.00 7/02/2012 12:51 29/01/2012 6:39
310 DEF 351012.00 3/05/2012 16:01 29/04/2012 6:29
261 DEF 1339066.00 26/03/2012 14:05 7/03/2012 21:18
215 DEF 785034.00 13/02/2012 13:42 29/01/2012 6:39
170 DEF 620026.00 7/02/2012 14:01 29/01/2012 6:39
You can try something like this:
;WITH PartData AS
(
SELECT
RowNum, Name, ItemValue, CurrentTime, StartTime,
RX = ROW_NUMBER() OVER(PARTITION BY Name,StartTime ORDER BY CurrentTime DESC)
FROM dbo.YourTable
)
SELECT
p1.RowNum, p1.ItemValue, p1.CurrentTime, p1.StartTime,
RowNumComparedTo = p2.RowNum,
ItemActual = CASE
WHEN p2.RX IS NULL THEN p1.ItemValue
ELSE p1.ItemValue - p2.ItemValue
END
FROM PartData p1
LEFT OUTER JOIN PartData p2 ON p1.StartTime = p2.StartTime
AND p1.Name = p2.Name
AND p2.RX = p1.RX + 1
I get an output something like this:
RowNum ItemValue CurrentTime StartTime RowNumComparedTo ItemActual
190 664031.00 2012-02-13 13:42 2012-01-29 06:39 145 131008.00
145 533023.00 2012-02-07 14:01 2012-01-29 06:39 100 1000.00
100 532023.00 2012-02-07 13:33 2012-01-29 06:39 55 0.00
55 532023.00 2012-02-07 12:52 2012-01-29 06:39 10 0.00
10 532023.00 2012-02-07 12:51 2012-01-29 06:39 NULL 532023.00
215 785034.00 2012-02-13 13:42 2012-01-29 06:39 170 165008.00
170 620026.00 2012-02-07 14:01 2012-01-29 06:39 NULL 620026.00
235 1086054.00 2012-05-03 14:05 2012-03-07 21:18 NULL 1086054.00
261 1339066.00 2012-03-26 14:05 2012-03-07 21:18 NULL 1339066.00
283 347013.00 2012-05-03 16:01 2012-04-29 06:29 NULL 347013.00
310 351012.00 2012-05-03 16:01 2012-04-29 06:29 NULL 351012.00
The solution basically does this:
it creates a CTE (Common Table Expression) and "partitions" your data by Name,StartTime and orders these rows by CurrentTime DESC - so the most recent entry for each Name,StartTime group will get an RX (Row indeX) of 1
it then joins that CTE against itself, shifted by one RX - so I'm comparing RX = 1 to RX = 2 (if present) etc.
if a "shifted" row is present, the difference in the ItemValue values is returned as ItemActual - otherwise the ItemValue from the main row is returned
I hope this solves your problem
Related
I want to find MAX value by the code and my data like below:
id
date
code
price
74
2022-01-04
B
64
91
2022-01-07
A
174
112
2022-01-11
B
128
245
2022-01-12
C
841
550
2022-01-14
A
79
780
2022-01-20
B
55
821
2022-01-23
D
45
868
2022-01-28
C
50
890
2022-02-02
B
467
891
2022-02-03
D
58
892
2022-02-04
A
472
What I expect, it will return like below:
id
date
code
price
245
2022-01-12
C
841
890
2022-02-02
B
467
891
2022-02-03
D
58
892
2022-02-04
A
472
I'm using below query:
select x.id, x.date, x.code, y.yprice
from data AS x
inner join
(
select id, date, code, MAX(price) AS yprice
from data
group by code
) y
on x.id = y.id AND x.code = y.code
and give me below result:
About the result:
The value of MAX is right, however the id and date is wrong.
Any idea to fix the query?
Thank You...
SELECT X.ID,X.DATE,X.CODE,X.PRICE
FROM
(
SELECT C.id,C.date,C.code,C.price,
ROW_NUMBER()OVER(PARTITION BY C.code ORDER BY C.Price DESC)XCOL
FROM test AS C
)X WHERE X.XCOL=1
Could you please try this one
I have a query that I need to SUM the trid column after summing the raised amount
SELECT
`ppm_campaign_id`.`meta_value` AS `campaign_id`,
`trasnlations`.`trid` AS `trid`,
`ppm_campaign_value`.`meta_value` AS `raised`
FROM (((`thf_posts` `posts`
LEFT JOIN `thf_postmeta` `ppm_campaign_id`
ON (((`ppm_campaign_id`.`meta_key` = 'campaign_id')
AND (`ppm_campaign_id`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_postmeta` `ppm_campaign_value`
ON (((`ppm_campaign_value`.`meta_key` = 'campaign_value')
AND (`ppm_campaign_value`.`post_id` = `posts`.`ID`))))
LEFT JOIN `thf_icl_translations` `trasnlations`
ON ((`trasnlations`.`element_id` = `ppm_campaign_id`.`meta_value`)))
WHERE ((`ppm_campaign_id`.`meta_value` IS NOT NULL)
AND (`trasnlations`.`trid` IS NOT NULL))
GROUP BY `ppm_campaign_id`.`meta_value`
Result
campaign_id trid raised
1022 564 4137.5
1031 564 3937.5
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 0.9
2164 682 50
2166 683 200
2168 684 50
Now I want the sum of trid combined so summing trid = 682 the result of raised should be 50.9 in both campaign_id
Expected Result
campaign_id trid raised
1022 564 8075
1031 564 8075
1698 653 3010
1700 655 10
1702 657 750
1712 653 3030
1713 655 20
1727 657 20
2163 682 50.9
2164 682 50.9
2166 683 200
2168 684 50
Considering the output of your query is stored in temptable. Please try the below query. Hope it helps.
SELECT t1.campaign_id, t1.trid, t2.raised_sum
FROM temptable t1 JOIN (
SELECT trid, SUM(raised) as raised_sum
FROM temptable GROUP BY trid) t2
ON t1.trid=t2.trid
GROUP BY t1.campaign_id, t1.trid, t2.raised_sum;
If more details on the tables included in your query and sample data can be given, we can try to get the data directly from them.
I have 2 Tables with following data:
TABLE 1 (dummy_daily)
Entry storenum busidate daily_budget
1 1 2017-07-01 4000
2 1 2017-07-02 3500
3 1 2017-07-03 2000
4 1 2017-07-04 6000
5 1 2017-07-05 1500
TABLE 2 (site_labour)
Lab_id storenum busidate lab_hour
1123 1 2017-07-01 128
1124 1 2017-07-02 103
1125 1 2017-07-03 114
1126 1 2017-07-04 108
1127 1 2017-07-05 118
This is my current query to combine the 2 tables that have the same date to give result of daily_budget and lab_hour
QUERY:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum JOIN
site_labour c ON b.storenum=c.storenum
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
But my current query give me a wrong result :
Wrong Result of Current Query
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 128 1 2017-07-02
2000 128 1 2017-07-03
6000 128 1 2017-07-04
1500 103 1 2017-07-05
4000 103 1 2017-07-01
3500 103 1 2017-07-02
2000 103 1 2017-07-03
6000 103 1 2017-07-04
1500 103 1 2017-07-05
This data will continue until end of Actual 118
Expected Result
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 103 1 2017-07-02
2000 114 1 2017-07-03
6000 108 1 2017-07-04
1500 118 1 2017-07-05
You have missed one more table so its make confusion to create logic,
As per my understanding, I have created a SELECT statement. Please try this:-
SELECT
dummy_daily.daily_budget as Ideal, site_labour.lab_hour as Actual,
site_store.store_name, site_store.storenum, dummy_daily.busidate
FROM dummy_daily
JOIN site_store
ON dummy_daily.storenum = site_store.storenum
JOIN site_labour
ON dummy_daily.storenum = site_labour.storenum
WHERE (dummy_daily.storenum = 1)
AND (dummy_daily.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (site_labour.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (dummy_daily.busidate = site_labour.busidate);
Let's try the SQL below:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum AND b.busidate=a.busidate JOIN
site_labour c ON b.storenum=c.storenum AND b.busidate=c.busidate
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
I have on table, that I record Car#,Date,RunHours as bellow:
Car# Date RunHours
125 2014-01-01 1250
125 2014-02-10 3250
215 2014-02-11 1400
215 2014-03-01 1800
125 2014-03-15 4100
215 2014-04-10 2500
I need the select result as bellow:
Car# Date Runhours Previous_Date Previous_RunHours
125 2014-01-01 1250 N/A N/A
125 2014-02-10 3250 2014-01-01 1250
215 2014-02-11 1400 N/A N/A
215 2014-03-01 1800 2014-02-11 1400
125 2014-03-15 4100 2014-02-10 3250
215 2014-04-10 2500 2014-02-11 1800
How can I do it.
Here is one method, using correlated subqueries to get the previous information:
select t.*,
(select t2.date
from table t2
where t2.car = t.car and t2.date < t.date
order by t2.date desc
limit 1
) as prev_date,
(select t2.runhours
from table t2
where t2.car = t.car and t2.date < t.date
order by t2.date desc
limit 1
) as prev_runhours
from table t;
For performance, you want an index on table(car, date).
I need to get GROUPed BY values,
but I need to see not a random price value (when I select price), but latest price value (price for a row with highest ID within this GROUP)
SELECT ID, price,
ROUND(AVG(price)),
MIN(price),
MAX(price),
ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices
FROM `m-orbitzone`
WHERE dep = 'MOW'
AND returnornot = 1
GROUP BY arv, date1, date2
ORDER BY differenceinprices DESC LIMIT 1000
ID price <-- ROUND(AVG(price)) MIN(price) MAX(price) differenceinprices
122841 834 816 534 834 35
122708 783 790 524 821 34
122754 766 796 529 815 34
28528 810 766 512 810 33
28529 799 765 512 799 33
122603 766 798 534 841 33
122848 766 794 529 810 33
122589 778 765 519 778 32
122591 778 768 519 778 32
122749 766 775 529 814 32
28527 752 749 512 773 32
122744 766 773 529 814 32
122843 766 771 529 802 31
Need 'price' to be latest price for this GROUP (row with highest ID)
May be need to do SELECT and then to do one more SELECT from result?
Thank you!
This should do the trick:
SELECT m.ID, price,
ROUND(AVG(price)),
MIN(price),
MAX(price),
ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices
FROM `m-orbitzone` m
INNER JOIN (
SELECT
ID
FROM
`m-orbitzone` m
WHERE ID = (SELECT MAX(ID) FROM `m-orbitzone` sm WHERE m.arv = sm.arv AND m.date1 = sm.date1 AND m.date2 = sm.date2)
) s ON m.ID = s.ID
WHERE dep = 'MOW'
AND returnornot = 1
GROUP BY arv, date1, date2
ORDER BY differenceinprices DESC LIMIT 1000
A good read about the topic is this manual entry.