Solved

Using a case statement to pull minuimum number

Posted on 2005-04-26
Medium Priority
256 Views
I hope I can explain this -- and don't know if it's even possible -- but here goes:

I have a statement

SELECT     CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100) AS JobNo,
CASE WHEN ((ResourceID in ('4', '42', '5', '6', '8', '44', '45', '12', '13') AND
TranType = 'O')) THEN (Qty) ELSE '0' End
FROM         dbo.ttJobTran

but instead of returning all lines for the ResourceIDs, I'd like to have it return a single line per JobId with the Minimum Qty -- and throwing a min in front of the QTY doesn't seem to work.

th1434
0
Question by:th1434
• 3
• 2
• 2
• +1

LVL 28

Expert Comment

ID: 13870445
Try this one:

SELECT     CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100) AS JobNo,
SUM(CASE WHEN ((ResourceID in ('4', '42', '5', '6', '8', '44', '45', '12', '13') AND
TranType = 'O')) THEN (Qty) ELSE 0) AS QTY End
FROM         dbo.ttJobTran
GROUP BY CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100)
0

LVL 70

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 13870500
SELECT CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100) AS JobNo,
MIN(CASE WHEN ((ResourceID in ('4', '42', '5', '6', '8', '44', '45', '12', '13') AND
TranType = 'O')) THEN (Qty) ELSE NULL End) AS Qty
FROM         dbo.ttJobTran
GROUP BY CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100)
0

LVL 34

Expert Comment

ID: 13870517
SELECT CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100) AS JobNo,
MIN(CASE WHEN ((ResourceID in ('4', '42', '5', '6', '8', '44', '45', '12', '13') AND
TranType = 'O')) THEN (Qty) ELSE '0' End) AS Qty
FROM dbo.ttJobTran
GROUP BY JobID
0

LVL 28

Expert Comment

ID: 13870521
Why oh why did I use SUM... I don't know.
0

LVL 34

Expert Comment

ID: 13870523
correction...

SELECT CONVERT(VARCHAR(15), CONVERT(MONEY, JobID) / 100) AS JobNo,
MIN(CASE WHEN ((ResourceID in ('4', '42', '5', '6', '8', '44', '45', '12', '13') AND
TranType = 'O')) THEN (Qty) ELSE 0 End) AS Qty
FROM dbo.ttJobTran
GROUP BY JobID
0

Author Comment

ID: 13870598
Thanks guys -- I guess I know just enough to be dangerous :-)

I guess the Group By makes a difference -- BriCrowe's answer didn't give me anything but zero in Qty.

0

LVL 70

Expert Comment

ID: 13870758
Yep: you can use 0 with *MAX*, but it kinda' defeats the purpose when used with MIN :-)
0

LVL 34

Expert Comment

ID: 13872564
Lol....the details are killer.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witā¦
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.