SQL Case When

Hello,
Can you please help,
I'm trying to add the case into my select,

Select OrderID,dtDate,ItemWeight,DimensionalWeight,
(Select Company From Customers where Customers.ID = Tracking.CustomerID) AS [Client],
BasePrice,TotalCost,
(Select CalculatedCost From TrackingModifiers where Tracking.ID = TrackingModifiers.TrackingID
and CASE Name
When Like 'CAN Fuel Surcharge' Then 'CAN Fuel Surcharge'
When Like 'Ad5ress change' Then 'Ad5ress change') END AS [Fuel],
(Select InvoiceNumber From Invoices Where Tracking.InvoiceID = Invoices.ID) AS [Invoice_Number],
(Select InvoiceDate From Invoices Where Tracking.InvoiceID = Invoices.ID) AS [Invoice_Date]
From        Tracking

Your help is appreciated.
W.E.BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Margaret GreenCommented:
Shouldn't the close parenthesis be after the END?

You code:
(Select CalculatedCost From TrackingModifiers where Tracking.ID = TrackingModifiers.TrackingID
^^
and CASE Name
When Like 'CAN Fuel Surcharge' Then 'CAN Fuel Surcharge'
When Like 'Ad5ress change' Then 'Ad5ress change') END
                                                                                          ^^
0
Scott PletcherSenior DBACommented:
Not sure exactly what you're trying to do, but you don't need to just compare to a list of values:

Select OrderID,dtDate,ItemWeight,DimensionalWeight,
    (Select Company From Customers where Customers.ID = Tracking.CustomerID) AS [Client],
    BasePrice,TotalCost,
    (Select CalculatedCost From TrackingModifiers where Tracking.ID = TrackingModifiers.TrackingID
and Name Like 'CAN Fuel Surcharge' or Name Like 'Ad5ress change') AS [Fuel],
    [Invoice_Number],
    [Invoice_Date]
From        Tracking
Outer Apply (
    Select InvoiceNumber, InvoiceDate
    FROM Invoices
    WHERE Tracking.InvoiceID = Invoices.ID
) As oa1
0
grendel777Commented:
"Case x when like" isn't valid, either. If you're trying to say "if [Name] starts with 'CAN Fuel Surcharge' then just use 'CAN Fuel Surcharge'", then use this CASE code ("%" is a wildcard):

SELECT OrderID,
	dtDate,
	ItemWeight,
	DimensionalWeight,
		(Select Company From Customers where Customers.ID = Tracking.CustomerID) 
	AS [Client],
	BasePrice,
	TotalCost,
		(Select CalculatedCost From TrackingModifiers where Tracking.ID = TrackingModifiers.TrackingID 
			AND CASE
					WHEN Name LIKE 'CAN Fuel Surcharge%' Then 'CAN Fuel Surcharge'
					WHEN Name LIKE 'Ad5ress change%' Then 'Ad5ress change' 
				END
		)
	AS [Fuel],
		(Select InvoiceNumber From Invoices Where Tracking.InvoiceID = Invoices.ID) 
	AS [Invoice_Number],
		(Select InvoiceDate From Invoices Where Tracking.InvoiceID = Invoices.ID) 
	AS [Invoice_Date]
FROM Tracking

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

W.E.BAuthor Commented:
Hi Grendel,
I get error message

Msg 4145, Level 15, State 1, Line 14
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'AS'.

Scott,
I'm trying to seperate my charges.

WHEN Name LIKE 'CAN Fuel Surcharge%' Then 'CAN Fuel Surcharge'
WHEN Name LIKE 'Ad5ress change%' Then 'Ad5ress change'

I have 62 case names, so I need to seperate the charges / names.

thanks
0
W.E.BAuthor Commented:
Scott,

Error

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
0
Scott PletcherSenior DBACommented:
Not much to go on, but maybe as below.  I write long CASE / IN statements in alpha/numeric order, so I did that below as well.

Select OrderID,dtDate,ItemWeight,DimensionalWeight,
    (Select Company From Customers where Customers.ID = Tracking.CustomerID) AS [Client],
    BasePrice,TotalCost,
    Case
        When Name LIKE 'Ad5ress change%' Then 'Ad5ress change'  
        When Name LIKE 'CAN Fuel Surcharge%' Then 'CAN Fuel Surcharge'
        --...
        Else 'Unknown' END As [Fuel],
    [Invoice_Number],
    [Invoice_Date]
From        Tracking t
Outer Apply (
    Select i.InvoiceNumber, i.InvoiceDate
    FROM Invoices i
    WHERE t.InvoiceID = i.ID
) As oa1
Inner Join  TrackingModifiers tm On tm.TrackingID = t.ID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W.E.BAuthor Commented:
Thank you,
This worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.