Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

case if sql2008

Posted on 2013-11-14
3
Medium Priority
?
304 Views
Last Modified: 2013-11-15
Hello,
can you please help,
how can I input a case statement to below code.

case if  SameDay is NULL then SameDay_Cost is Null Else SameDay_Cost  end

SELECT Table1.AccountstartDate,Table1.AccountNumber,Table1.AccountCode,Table1.Name,
[OVN] = SUM(Table1.[OVN]), [OVN_Cost] = SUM(Table1.[OVN]) - (SUM(Table1.[OVN])*0.25),[OVN_NET] = SUM(Table1.[OVN]) - (SUM(Table1.[OVN])*0.75),

[SameDay] = SUM(Table1.[SameDAY]),
[SameDay_Cost] = (SUM(Table1.[PUDriverCommission])+SUM(Table1.[DELDriverCommission])+SUM(Table1.[Sales1Commission])+SUM(Table1.[Sales2Commission])),
[SameDay_NET] = (SUM(Table1.[SameDAY]) - (SUM(Table1.[PUDriverCommission])+SUM(Table1.[DELDriverCommission])+SUM(Table1.[Sales1Commission])+SUM(Table1.[Sales2Commission])))

FROM ( SELECT fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,

(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID in (122,123,124)) AS [OVN],
(Select SUM(fo.SubTotalAmount) * 0.25 where fo.ServiceTypeID in (122,123,124)) AS [OVN_Cost],
(Select SUM(fo.SubTotalAmount) * 0.75 where fo.ServiceTypeID in (122,123,124)) AS [OVN_Net],

(Select SUM(fo.SubTotalAmount) where fo.ServiceTypeID NOT in (122,123,124)) AS [SameDay],
(Select (SUM(fo.PUDriverCommission) + SUM(fo.DELDriverCommission) + SUM(fo.Sales1Commission) + SUM(fo.Sales2Commission)) where fo.ServiceTypeID NOT in (122,123,124)) AS [SameDay_Cost],
(Select SUM(fo.SubTotalAmount) - (SUM(fo.PUDriverCommission) + SUM(fo.DELDriverCommission) + SUM(fo.Sales1Commission) + SUM(fo.Sales2Commission)) where fo.ServiceTypeID NOT in (122,123,124))  AS [SameDay_NET],

SUM(fo.TotalAmount) AS [TotalAmount] ,SUM(fo.SubTotalAmount) AS [SubTotalAmount], SUM(fo.FuelAmount) AS [FuelAmount], 
SUM(fo.PUDriverCommission) AS [PUDriverCommission],SUM(fo.DELDriverCommission) AS [DELDriverCommission],SUM(fo.PUDriverCommission) AS [Driver3Commission],SUM(fo.DELDriverCommission) AS [Driver4Commission],
SUM(fo.Sales1Commission) AS [Sales1Commission],SUM(fo.Sales2Commission) AS [Sales2Commission]

FROM  FinalizedOrders fo JOIN Clients cl ON fo.AccountNumber = cl.AccountNumber 
WHERE ((fo.OrderDate >= '10-01-2013')  And (fo.OrderDate <='11-01-2013')) 
AND fo.AccountNumber Not in (9999,715,3335,6,511,818,987,1988,1999,9995,8500,8501,8502,1006,99990,99991,99992,99993,99994,99995,99996,99997,99998,99999,4629,9992,9996,9997,8421894) 
GROUP BY fo.AccountNumber,cl.AccountCode,cl.AccountstartDate, cl.Name,fo.ServiceTypeID 
) Table1 
GROUP BY Table1.AccountstartDate,Table1.AccountNumber,Table1.AccountCode,Table1.Name 
ORDER BY Table1.AccountCode 

Open in new window

thanks,
0
Comment
Question by:W.E.B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 1200 total points
ID: 39650759
How about this:

[SameDay_Cost] = case when SUM(Table1.[SameDAY]) = 0
    then 0
    else (SUM(Table1.[PUDriverCommission])+SUM(Table1.[DELDriverCommission])+SUM(Table1.[Sales1Commission])+SUM(Table1.[Sales2Commission]))
  end

Open in new window

Replace 0 by null if you really meant null...
0
 

Author Closing Comment

by:W.E.B
ID: 39650812
Thank you,
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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