Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Find lowest populated field, not min value Expand / Collapse
Author
Message
Posted Monday, July 1, 2013 3:58 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 18, 2013 9:05 PM
Points: 12, Visits: 161
Hi all,

I have a tricky one.

I have a dataset as below which deals with airport delays.

CREATE TABLE #delaystats
(
airport VARCHAR(10)
,thedate SMALLDATETIME
,delay1 INT
,delay2 INT
,delay3 INT
,delay4 INT
,delay5 INT

)

INSERT INTO #delaystats (airport,thedate,delay1,delay2,delay3,delay4,delay5)

SELECT 'LAX', '01-May-2013',2,0,1,4,1 UNION ALL
SELECT 'LAX', '02-May-2013',0,0,1,2,0 UNION ALL
SELECT 'LAX', '03-May-2013',0,0,0,2,0

SELECT * FROM #delaystats

DROP TABLE #delaystats

The dataset is quite simple and contains an airport code, date and field-names which represent flight delays. So delay1 contains a number representing how many flights where delays by 1 hour on that day, delay2 is 2 hours and so on.

What I need to do is for each date return the field that represents the minimum delay for that day. Note it is not how many flights where delayed but the minimum delay for that day.

So the result on the above dataset would be

Airport Date MinDelay
LAX 01-May-2013 Delay1
LAX 02-May-2013 Delay3
LAX 03-May-2013 Delay4

Any ideas on how to achieve this?

thanks,

derrysql
Post #1469231
Posted Monday, July 1, 2013 4:15 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 4:48 PM
Points: 867, Visits: 7,570
How about:

select airport,
thedate,
case when delay1 <> 0 then 'delay1'
when delay2 <> 0 then 'delay2'
when delay3 <> 0 then 'delay3'
when delay4 <> 0 then 'delay4'
when delay5 <> 0 then 'delay5'
else 'no delays'
end as 'Min Delay'
from #delaystats






And then again, I might be wrong ...
David Webb
Post #1469234
Posted Monday, July 1, 2013 5:53 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 15, 2014 2:26 PM
Points: 5,466, Visits: 7,647
I agree with David here, since it's a relatively simple paradigm and a direct order of evaluation you've got. Anything more complex would be overkill, as well as non optimal, since CASE can short circuit the evaluation once it's true.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1469245
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse