473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Having Issues with sql Case statement in query.

************************************************
This code runs fine

with these results

InsurerID Total SubAccounts Total Contracts
WSL 3752 3653

*************************************************

Expand|Select|Wrap|Line Numbers
  1. With t1 as
  2. (Select x.InsurerID, Count(Distinct x.SubaccountID) as 'Total SubAccount', 
  3. Count(Distinct x.Contractno) as 'Total Contracts'
  4. from
  5. (
  6. Select InsurerID, SubAccountID, Contractno, SubAccountNoUnits
  7. from VApositionsHistoryAll
  8. where (Historyasofdate = '09/14/2010' or Historyasofdate = '04/30/2010'
  9. and SubAccountNoUnits > 0) and InsurerID = 'WSL'
  10. Union all
  11. select InsurerID, SubAccountId, ContractNo, ISNULL(numberofunits, 0) * 
  12. ( case when ISNULL(activitysign, '+') = '-' THEN ( -1 ) ELSE 1 END)
  13. from VAActivity
  14. where (filedate >'05/01/2010' and filedate <'09/15/2010') and InsurerID = 'WSL'
  15. ) as x
  16. Group by InsurerId, ContractNo)
  17. Select InsurerID, sum("Total SubAccount") as 'Total SubAccounts', sum("Total Contracts") as 'Total Contracts'
  18. from t1
  19. group by InsurerID
*************************************************
The below code is the problem Code

Trying to add Case statements.

The desired result is:

InsurerID TotalSubAccounts TotalContracts Match NonMatch
WSL 3752 3653 3600 152

The matches are by subaccountID

Help:

I am struggling with my sql query and in need of help. I have included information below and in the code box.

Matches =

If Historyasofdate = 04/30/2010
then add subaccountnounits from vapositionshistoryall table to numberofunits from the vaactivity table to get "Begin Position"

If Historyasofdate = 09/14/2010 then "Ending Position"

"Beginning position" - Ending Position = 0 would be a "Match"

Non Matches:

Anything else would be a "NonMatch"

I thought that I could use case to come up with this but I am stuggling.


I am not sure if I am approaching this properly. Help any suggestions appreciated.


Expand|Select|Wrap|Line Numbers
  1. *************************************************
  2. With t1 as
  3. (Select x.InsurerID, Count(Distinct x.SubaccountID) as 'Total SubAccount', 
  4. Count(Distinct x.Contractno) as 'Total Contracts',
  5.  
  6. (Case when (a.Historyasofdate = '04/30/2010') then (a.SubAccountNoUnits + x.numberofunits)else 0 end) as 'Start_POS_Act',
  7. (Case when (a.Historyasofdate = '09/14/2010') then (a.SubAccountNoUnits)else 0 end) as 'End_Position',
  8. (Case when ('Start_POS_Act' - 'End_Position' = 0) Then 1 Else 0 end) as 'Matches_Y_or_N'
  9.  
  10. From
  11.  
  12. (Select InsurerID, SubAccountID, Contractno, SubAccountNoUnits
  13. from VApositionsHistoryAll a
  14. where (Historyasofdate = '04/30/2010' or Historyasofdate = '09/14/2010')
  15. and SubAccountNoUnits > 0 and InsurerID = 'WSL'
  16.  
  17. Union all
  18.  
  19. (select InsurerID, SubAccountId, ContractNo, ISNULL(numberofunits, 0) * 
  20. (case when ISNULL(activitysign, '+') = '-' THEN ( -1 ) ELSE 1 END)
  21. from VAActivity 
  22. where (filedate >'05/01/2010' and filedate <'09/15/2010') and InsurerID = 'WSL') as x
  23. Group by InsurerId, ContractNo)
  24.  
  25. Select InsurerID, sum("Total SubAccount") as 'Total SubAccounts', 
  26. sum("Total Contracts") as 'Total Contracts', 
  27. (Case when "Matches_Y_or_N" = 1 then Count("Matches_Y_or_N") else 0 end) as 'Number of Matches',
  28. (Case when "Matches_Y_or_N" = 0 then Count("Matches_Y_or_N") else 1 end) as 'Number of NonMatches'
  29. from t1
  30. group by InsurerID
Oct 21 '10 #1
0 1201

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bryan | last post by:
I am executing a case statement list below, USE Northwind SELECT MONTH(OrderDate) AS OrderMonth, SUM(CASE YEAR(OrderDate) WHEN 1996 THEN 1 ELSE 0 END) AS c1996,
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
6
by: ryan.mclean | last post by:
Hi all, first, let me preface this by saying that I am very new to sql server, coming from oracle. Here is my problem: I would like to have a case statement (similar to decode in oracle) that...
2
by: deanclowe | last post by:
Hi I have a query that is trying to use a field that is created "AS" based on a case statement like this: SELECT CASE WHEN IPOVH = 'Y' THEN '01' WHEN IPHSI = 'Y' ...
2
by: Mark Mullins | last post by:
have code below: Function ClassColl(strClass As Variant, strColl As Variant) As String ' Comments : ' Parameters : strClass ' strColl ' Returns : String Description '...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
2
by: elpeak | last post by:
I have a stored proc that contains 10 other stored procs. When i pass in the variable i want it to find the stored proc that matches and execute. I am having issues w/ the case statement and am...
2
by: pintu | last post by:
Hi Friends, I want to use "in" clause in case statement as shown below. e.g select * from employee where empid in case i_desgn when 'HR' then (1,2,3,4,5) when 'MD' then 1 else 7 end
5
by: RiddleMeThis | last post by:
Im trying to use 2 CASE statements together in a SELECT query without much luck. The second CASE uses the output from the first CASE as it’s expression (well it’s supposed to). When the 2nd CASE...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.