Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Case When???

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? Thanks

Ship--Status--Date

1--PUC--1/1/2010
1--DEL--1/2/2010
1--POD--1/2/2010
1--REC--1/2/2010

2--PUC--1/3/2010
2--DEL--1/4/2010
2--REC--1/4/2010

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-24 : 12:04:07
yes

SELECT Ship,
COALESCE(MAX(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))
FROM Table
GROUP BY Ship


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 DMY

DECLARE @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 @foo

SELECT
[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')
)
f
WHERE
f.[pos] = 1



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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--Date

1--PUC--1/1/2010
1--DEL--1/2/2010
1--POD--1/2/2010
1--REC--1/2/2010
1--POD--1/10/2010
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-03-24 : 12:09:59
Change Visakh's MAX to MIN


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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--Date

1--PUC--1/1/2010
1--DEL--1/2/2010
1--POD--1/2/2010
1--REC--1/2/2010
1--POD--1/10/2010



SELECT Ship,
COALESCE(MIN(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))
FROM Table
GROUP BY Ship


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 Date
FROM hawb as h1 left join statushistory as s1 on h1.hawbnum=s1.docnumlong
where h1.hawbnum=' 11796389'
GROUP BY h1.hawbnum, s1.stsgroupcode

The 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
Go to Top of Page

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 again

SET DATEFORMAT DMY

DECLARE @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 Data
SELECT * FROM @foo

-- Row_Number Method
SELECT
[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')
)
f
WHERE
f.[pos] = 1

-- COALESCE Method
SELECT Ship,
COALESCE(MIN(CASE WHEN Status='POD' THEN date END),MAX(CASE WHEN Status='REC' THEN date END))
FROM @foo
GROUP BY Ship

Are you doing something different?

The results from the ROW_NUMBER and the COALESCE are the same. They look like:

Ship Date
1 2010-02-01 00:00:00.000
2 2010-04-01 00:00:00.000



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 Date
FROM hawb as h1 left join statushistory as s1 on h1.hawbnum=s1.docnumlong
where h1.hawbnum=' 11796389'
GROUP BY h1.hawbnum, s1.stsgroupcode

The 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

TPie9
Yak Posting Veteran

67 Posts

Posted - 2010-03-24 : 12:51:59
I just posted my exact query and results.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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?
Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page
    Next Page

- Advertisement -