| Author |
Topic |
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-19 : 12:43:05
|
| Need help on GroupingGroup Seq Dest1 1 311 2 1111 3 1211 4 412 5 1412 6 122 7 612 8 512 9 413 10 1433 11 133 12 42 If In a Group the Max Sequence Number and Min Sequence Numbers have the same last 2 digits, then the result should indicate 1 else 0Example the abobe list give the below resultGroup Status1 02 13 0Because , In Group 2 the max Seq Number is 9 which has 41 and the min Seq Number is 5 which has 141, the last 2 digits of both are same. Where as other groups have diffferent Numbers |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-10-19 : 13:30:00
|
| [code]SELECT GROUPS,CASE WHEN RIGHT(MAXVALUE,2) = RIGHT(M.MINVALUE,2) THEN 1 ELSE 0 END AS STATUSFROM(Select Groups,MAX(CASE WHEN MAXID = 1 THEN DEST ELSE NULL END)AS MAXVALUE,MAX(CASE WHEN MINID = 1 THEN DEST ELSE NULL END)AS MINVALUEFROM(SELECT ROW_NUMBER() OVER (PARTITION BY GROUPS ORDER BY SEQ DESC) AS MAXID,ROW_NUMBER()OVER (PARTITION BY GROUPS ORDER BY SEQ)AS MINID,GROUPS,DESTfrom TABLE) TWHERE T.MAXID = 1 OR T.MINID = 1GROUP BY GROUPS)MORDER BY GROUPS[/code] |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-21 : 06:46:03
|
| Thank You, Can I have the DestID of the last and first seqquences included in the list likeGroup Status Start End1 0 31 412 1 141 413 0 143 42 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 13:17:59
|
| [code]SELECT [Group],CASE WHEN MaxSeq%100=MinSeq%100 THEN 1 ELSE 0 END,MinDest,MaxDestFROM(SELECT [Group],MAX(Seq) AS MaxSeq,MIN(Seq) AS MinSeq,MAX(Dest) AS MaxDest,MIN(Dest) AS MinDestFROM TableGROUP BY [Group])t[/code] |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-23 : 05:18:44
|
| Thank you , But there is a small concern I need the below:Table:List_JobsJobID SeqNo GroupID TimeToPick DestID Completed1 1 1 14:23:00 31 01 2 1 14:23:00 61 01 3 1 14:23:00 62 01 4 1 14:23:00 141 01 5 2 14:43:00 41 01 6 2 14:43:00 11 01 7 2 14:43:00 12 01 8 2 14:43:00 141 01 9 3 14:53:00 41 01 10 3 14:53:00 142 01 11 4 14:59:00 42 01 12 4 14:59:00 61 01 13 4 14:59:00 11 01 14 4 14:59:00 143 02 1 1 14:27:00 31 02 2 1 14:27:00 61 02 3 1 14:27:00 62 02 4 1 14:27:00 141 02 5 2 14:29:00 41 02 6 2 14:29:00 11 02 7 2 14:29:00 12 02 8 2 14:29:00 142 02 9 3 15:53:00 42 02 10 3 15:53:00 142 02 11 4 15:59:00 42 02 12 4 15:59:00 61 02 13 4 15:59:00 11 02 14 4 15:59:00 142 0Result Needed:DestID of the Max and Min SeqNo of a GroupID sorted on TimeToPick where Completed is 0Job_ID FiRST Last GroupID TimeToPick Status1 31 141 1 14:23:00 02 31 141 1 14:27:00 02 41 142 2 14:29:00 01 41 141 2 14:43:00 11 41 142 3 14:53:00 01 42 143 4 14:59:00 02 42 142 3 15:53:00 12 42 142 4 15:59:00 1 |
 |
|
|
roshana
Starting Member
31 Posts |
Posted - 2009-10-23 : 06:34:24
|
| select distinct YourTable.[Group] ,case when right(T2.minDest,2) = right(T2.maxDest,2) then 1else 0end Status ,minDest,maxDestfrom YourTableinner join (select YourTable.[Group],Dest minDest,T1.maxDest maxDest from YourTable inner join (select YourTable.[Group],Dest maxDest,minimum from YourTable inner join (select [Group], max(Seq) maximum ,min(Seq)minimum from YourTable group by [Group]) T on YourTable.[Group] = T.[Group] and YourTable.Seq = T.maximum) T1on YourTable.[Group] = T1.[Group] and YourTable.Seq = T1.minimum ) T2 on YourTable.[Group] = T2.[Group] |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-23 : 07:21:10
|
| How can I get the TimeToPick |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-23 : 12:48:58
|
| [code]select JobID,MIN(SeqNo) AS First,MAX(SeqNo) AS Last,GroupID,TimeToPick,<your Status value> -- not sure how u get itfrom yourtablegroup by JobID,GroupID,TimeToPickOrder by TimeToPick[/code] |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2009-10-24 : 01:16:21
|
| ;with cte_testas(select JobID, GroupID, max(TimeToPick) TimeToPick, Min(SeqNo) minseq, Max(SeqNo) maxseq from <your table> group by JobID, GroupID)select JobID, mi.DestID as First, ma.DestID as Last, GroupID, TimeToPick, case when Right(cast(mi.DestID as varchar), 2) = Right(cast(ma.DestID as varchar), 2) then 1 else 0 end Statusfrom cte_test t1 cross apply (select DestID from <your table> m where m.JobID = t1.JobID and m.GroupID = t1.GroupID and m.SeqNo = t1.minseq) mi cross apply (select DestID from <your table> m where m.JobID = t1.JobID and m.GroupID = t1.GroupID and m.SeqNo = t1.maxseq) ma--------------------------------------------------S.Ahamed |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-24 : 04:55:43
|
quote: Originally posted by pbguy ;with cte_testas(select JobID, GroupID, max(TimeToPick) TimeToPick, Min(SeqNo) minseq, Max(SeqNo) maxseq from <your table> group by JobID, GroupID)select JobID, mi.DestID as First, ma.DestID as Last, GroupID, TimeToPick, case when Right(cast(mi.DestID as varchar), 2) = Right(cast(ma.DestID as varchar), 2) then 1 else 0 end Statusfrom cte_test t1 cross apply (select DestID from <your table> m where m.JobID = t1.JobID and m.GroupID = t1.GroupID and m.SeqNo = t1.minseq) mi cross apply (select DestID from <your table> m where m.JobID = t1.JobID and m.GroupID = t1.GroupID and m.SeqNo = t1.maxseq) ma--------------------------------------------------S.Ahamed
is cte necessary here? |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-26 : 06:21:35
|
| Thank you so much.How can I make this Result set Conditional on First (DestID) or Last (DestID) and Sort it by say TimeToPick? |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-10-26 : 06:48:54
|
| Sorry, I still have trouble in using this query.Can I please get answer to this problem :JobType SeqNo GID Wait DestID SubPrg Status1 1 1 0 31 0 False1 2 1 0 11 1 False1 3 1 0 21 151 False1 4 1 0 41 0 False1 5 2 20 141 0 False1 6 2 20 12 51 False1 7 2 20 61 201 False1 8 2 20 42 0 False1 9 3 20 142 0 True1 10 3 20 12 51 False1 11 3 20 63 301 False1 12 3 20 42 0 False1 13 4 20 142 0 True1 14 4 20 12 51 False1 15 4 20 63 301 False1 16 4 20 42 0 False1 17 5 20 142 0 False1 18 5 20 12 51 False1 19 5 20 62 252 False1 20 5 20 43 0 False1 21 6 20 143 251 FalseI need to update Status based on the same logic I had earlire mentioned. nI think this would solve my problem. |
 |
|
|
|