| Author |
Topic |
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 11:58:47
|
| I have the following table and am trying to pull the status date for status POD only. When status POD isn't available I want to pull the status date for staus REC. Is that possible? ThanksShip--Status--Date1--PUC--1/1/20101--DEL--1/2/20101--POD--1/2/20101--REC--1/2/20102--PUC--1/3/20102--DEL--1/4/20102--REC--1/4/2010 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:04:07
|
yesSELECT Ship,COALESCE(MAX(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))FROM TableGROUP BY Ship ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-24 : 12:08:11
|
Ah yes that's a little more succinct than mine  SET DATEFORMAT DMYDECLARE @foo TABLE ( [Ship] INT , [Status] CHAR(3) , [Date] DATETIME PRIMARY KEY ([Ship], [Status]) )INSERT @foo ([Ship], [Status], [Date]) SELECT 1, 'PUC', '1/1/2010'UNION SELECT 1, 'DEL', '1/2/2010'UNION SELECT 1, 'POD', '1/2/2010'UNION SELECT 1, 'REC', '1/2/2010'UNION SELECT 2, 'PUC', '1/3/2010'UNION SELECT 2, 'DEL', '1/4/2010'UNION SELECT 2, 'REC', '1/4/2010'SELECT * FROM @fooSELECT [Ship] , [Date]FROM ( SELECT [Ship] AS [Ship] , [Date] AS [Date] , ROW_NUMBER() OVER ( PARTITION BY [Ship] ORDER BY [Status] ASC) AS [Pos] FROM @foo WHERE [status] IN ('POD', 'REC') ) fWHERE f.[pos] = 1Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:08:37
|
| One more thing I forgot to include. What if Ship 1 has 2 POD status dates and I want the earliest date?Ship--Status--Date1--PUC--1/1/20101--DEL--1/2/20101--POD--1/2/20101--REC--1/2/20101--POD--1/10/2010 |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-24 : 12:09:59
|
| Change Visakh's MAX to MINCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:11:13
|
quote: Originally posted by TPie9 One more thing I forgot to include. What if Ship 1 has 2 POD status dates and I want the earliest date?Ship--Status--Date1--PUC--1/1/20101--DEL--1/2/20101--POD--1/2/20101--REC--1/2/20101--POD--1/10/2010
SELECT Ship,COALESCE(MIN(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))FROM TableGROUP BY Ship ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:19:23
|
| How would I pull the info just for status POD? When I run the query I get all the status' showing up, but only the date associated with status POD and all I want to return is one line with statu POD and the date associated with it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:22:35
|
quote: Originally posted by TPie9 How would I pull the info just for status POD? When I run the query I get all the status' showing up, but only the date associated with status POD and all I want to return is one line with statu POD and the date associated with it.
if you use my query you will get only one row per Ship value with associated date values of status POD if present or status REC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:42:11
|
| I must be doing something wrong because I am using your query and I'm getting all the status' per ship, but only the date associated with POD when present. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:44:36
|
quote: Originally posted by TPie9 I must be doing something wrong because I am using your query and I'm getting all the status' per ship, but only the date associated with POD when present.
why dont you show your used query then?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:46:46
|
| Here's the query:SELECT h1.hawbnum, s1.stsgroupcode as Status,COALESCE(MIN(CASE WHEN s1.stsgroupcode='DST' THEN (convert (varchar(10), s1.stsdatetime, 101)) END), MIN(CASE WHEN s1.stsgroupcode='REC' THEN (convert (varchar(10), s1.stsdatetime, 101)) END)) as DateFROM hawb as h1 left join statushistory as s1 on h1.hawbnum=s1.docnumlongwhere h1.hawbnum=' 11796389'GROUP BY h1.hawbnum, s1.stsgroupcodeThe results: 11796389 NULL 11796389 AMB NULL 11796389 COB NULL 11796389 DST 11/30/2009 11796389 OCN NULL 11796389 OFD NULL 11796389 POD NULL 11796389 PUC NULL 11796389 REC 12/08/2009 |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-24 : 12:47:56
|
Visakh's solutions works fine on the data set I posted. Here it is againSET DATEFORMAT DMYDECLARE @foo TABLE ( [Ship] INT , [Status] CHAR(3) , [Date] DATETIME PRIMARY KEY ([Ship], [Status], [Date]) )INSERT @foo ([Ship], [Status], [Date]) SELECT 1, 'PUC', '1/1/2010'UNION SELECT 1, 'DEL', '1/2/2010'UNION SELECT 1, 'POD', '1/2/2010'UNION SELECT 1, 'REC', '1/2/2010'UNION SELECT 1, 'POD', '1/10/2010'UNION SELECT 2, 'PUC', '1/3/2010'UNION SELECT 2, 'DEL', '1/4/2010'UNION SELECT 2, 'REC', '1/4/2010'-- Raw DataSELECT * FROM @foo-- Row_Number MethodSELECT [Ship] , [Date]FROM ( SELECT [Ship] AS [Ship] , [Date] AS [Date] , ROW_NUMBER() OVER ( PARTITION BY [Ship] ORDER BY [Status] ASC, [Date] ASC) AS [Pos] FROM @foo WHERE [status] IN ('POD', 'REC') ) fWHERE f.[pos] = 1-- COALESCE MethodSELECT Ship,COALESCE(MIN(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))FROM @fooGROUP BY ShipAre you doing something different?The results from the ROW_NUMBER and the COALESCE are the same. They look like:Ship Date1 2010-02-01 00:00:00.0002 2010-04-01 00:00:00.000 Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:50:22
|
quote: Originally posted by TPie9 Here's the query:SELECT h1.hawbnum, s1.stsgroupcode,COALESCE(MIN(CASE WHEN s1.stsgroupcode='DST' THEN (convert (varchar(10), s1.stsdatetime, 101)) END), MIN(CASE WHEN s1.stsgroupcode='REC' THEN (convert (varchar(10), s1.stsdatetime, 101)) END)) as DateFROM hawb as h1 left join statushistory as s1 on h1.hawbnum=s1.docnumlongwhere h1.hawbnum=' 11796389'GROUP BY h1.hawbnum, s1.stsgroupcodeThe results: 11796389 NULL 11796389 AMB NULL 11796389 COB NULL 11796389 DST 11/30/2009 11796389 OCN NULL 11796389 OFD NULL 11796389 POD NULL 11796389 PUC NULL 11796389 REC 12/08/2009
modify as above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:51:20
|
| also you use DST instead of POD as condition for first date, is this intentional?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:51:59
|
| I just posted my exact query and results. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:53:14
|
quote: Originally posted by TPie9 I just posted my exact query and results.
and i modified it ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-24 : 12:53:23
|
| I'm just confusing things here. We're all posting at once. Does Visakh's new solution work for you?Bowing out......Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:56:10
|
| Yes using DST was intentional. How do I know what status the date is for then if I don't select the status in the query? |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 12:57:27
|
| So far Visakh's solution seems to be working, but now I need to figure out how to include the status that is associated with the date showing. If it's for status REC or not. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:57:54
|
quote: Originally posted by TPie9 Yes using DST was intentional. How do I know what status the date is for then if I don't select the status in the query?
You missed my point. you showed it as POD in your initial requirement not DST------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
TPie9
Yak Posting Veteran
67 Posts |
Posted - 2010-03-24 : 13:04:34
|
| I did and sorry about that. Is there a way I can pull the status that's associated with the date from the query result. |
 |
|
|
Next Page
|