?
Solved

Using a case statement to pull minuimum number

Posted on 2005-04-26
8
Medium Priority
?
256 Views
Last Modified: 2010-03-19
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
Comment
Question by:th1434
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 28

Expert Comment

by:rafrancisco
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

by:
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

by:Brian Crowe
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

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

Expert Comment

by:Brian Crowe
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

by:th1434
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

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

Expert Comment

by:Brian Crowe
ID: 13872564
Lol....the details are killer.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

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.

Join & Ask a Question