February 8, 2016 at 2:21 pm
Thank you so much.
It is worked in some condition.
In below condition, it is not working.
t_mcnot_citgt_trdt t_shft t_wght(No column name)
F01 1010052016-01-12 00:00:00.0004 0.000.00
F01 1010052016-01-12 00:00:00.0003 0.000.00
F01 1010052016-01-12 00:00:00.0002 0.000.00
F01 1010052016-01-12 00:00:00.0001 0.000.00
F01 1010052016-01-11 00:00:00.0004 17.9617.96
F01 1010052016-01-11 00:00:00.0003 0.000.00
F01 1010052016-01-11 00:00:00.0002 0.000.00
F01 1010052016-01-11 00:00:00.0001 0.000.00
I am expecting order by shift as well.
t_mcnot_citgt_trdt t_shft t_wght(No column name)
F01 1010052016-01-12 00:00:00.0004 0.0017.96
F01 1010052016-01-12 00:00:00.0003 0.0017.96
F01 1010052016-01-12 00:00:00.0002 0.0017.96
F01 1010052016-01-12 00:00:00.0001 0.0017.96
F01 1010052016-01-11 00:00:00.0004 17.9617.96
F01 1010052016-01-11 00:00:00.0003 0.000.00
F01 1010052016-01-11 00:00:00.0002 0.000.00
F01 1010052016-01-11 00:00:00.0001 0.000.00
February 8, 2016 at 2:35 pm
Of course it's not working, I made it carry over the weight if the shifts are on the same day. If you are able to understand the query, the correction is simple with some changes in the conditions. If you don't understand the query, you shouldn't use it.
February 8, 2016 at 2:58 pm
I understood the query but I am not able to return the result. I tried my best to get result. Please help me.
February 8, 2016 at 3:02 pm
patla4u (2/8/2016)
I understood the query but I am not able to return the result. I tried my best to get result. Please help me.
Post what you've tried.
February 8, 2016 at 3:04 pm
SELECT o.*,
COALESCE( NULLIF(o.t_wght, 0), x.t_wght, 0)
FROM [TBRTEK002] o
OUTER APPLY (
SELECT TOP 1 max(i.t_wght) as t_wght
FROM [TBRTEK002] i
WHERE i.t_mcno = o.t_mcno
AND i.t_citg = o.t_citg
AND i.t_shft < o.t_shft
-- ORDER BY i.t_mcno,i.t_citg,i.t_trdt desc,i.t_shft DESC
) x
WHERE o.t_mcno = 'F01'
ORDER BY [t_mcno],[t_citg],t_trdt desc, t_shft desc;
February 8, 2016 at 3:37 pm
patla4u (2/8/2016)
SELECT o.*,COALESCE( NULLIF(o.t_wght, 0), x.t_wght, 0)
FROM [TBRTEK002] o
OUTER APPLY (
SELECT TOP 1 max(i.t_wght) as t_wght
FROM [TBRTEK002] i
WHERE i.t_mcno = o.t_mcno
AND i.t_citg = o.t_citg
AND i.t_shft < o.t_shft
-- ORDER BY i.t_mcno,i.t_citg,i.t_trdt desc,i.t_shft DESC
) x
WHERE o.t_mcno = 'F01'
ORDER BY [t_mcno],[t_citg],t_trdt desc, t_shft desc;
Why did you commented the order by? That's important to ensure that the correct row is chosen. Why aren't you comparing the date? You need something to be either on a previous shift the same day or a previous day with any shift. (Just translate this to T-SQL)
February 9, 2016 at 7:41 am
Thanks for your reply.
I was doing in different scenario, that's why i remove order by. I knew, order by is used to select top 1 wght based on asc and des.
I removed the date because, i am thinking, it will pick up previous date.
In this t-sql, coalesce is used to returned the first non-null expression in the list. Also Outer apply returns the matching and not matching rows.
Let me try few more hours and get back to you.
Thanks
February 9, 2016 at 8:24 am
Hi,
I tried again but I am not to produce result as i expected. Please help me and provide query.
Thanks
February 9, 2016 at 9:41 am
Hi,, After doing my best try,
I believe, below query should work.
SELECT o.*,
COALESCE( NULLIF( o.t_wght, 0), x.t_wght,y.t_wght, 0)
FROM [TBRTEK002] o
OUTER APPLY (
SELECT top 1 i.t_wght
FROM [TBRTEK002] i
WHERE i.t_mcno = o.t_mcno
AND i.t_citg = o.t_citg
AND i.t_trdt = o.t_trdt
AND i.t_shft < o.t_shft
AND i.t_wght > 0
ORDER BY i.t_shft DESC
) x
OUTER APPLY (
SELECT top 1 c.t_wght as t_wght
FROM [TBRTEK002] c
WHERE c.t_mcno = o.t_mcno
AND c.t_citg = o.t_citg
AND C.t_trdt < o.t_trdt
AND c.t_wght > 0
ORDER BY c.t_shft desc
) y
WHERE o.t_mcno = 'F01'
ORDER BY [t_mcno],[t_citg],[t_trdt] desc,[t_shft] desc
Thanks
February 10, 2016 at 11:43 am
Did you try something like this?
SELECT o.*,
COALESCE( NULLIF( o.t_wght, 0), x.t_wght, 0)
FROM [TBRTEK002] o
OUTER APPLY (
SELECT TOP 1 i.t_wght
FROM [TBRTEK002] i
WHERE i.t_mcno = o.t_mcno
AND i.t_citg = o.t_citg
AND (i.t_trdt = o.t_trdt
OR i.t_shft < o.t_shft)
AND i.t_wght > 0
ORDER BY i.t_shft DESC
) x
WHERE o.t_mcno = 'F03'
ORDER BY t_trdt desc, t_shft desc;
February 10, 2016 at 12:07 pm
Yes,, I checked but it wan't work.
Thanks
Viewing 11 posts - 16 through 25 (of 25 total)
You must be logged in to reply to this topic. Login to reply