Solved

Use multiple CASE statements to get conditional column value in a SELECT query

Posted on 2013-05-17
8
411 Views
Last Modified: 2013-05-31
I have two tables viz. #Exported_Data and User_Details.

#Exported_Data:
(User_Id, User_Name, Status, Office_Id, Dept_Id, Service_Id, Allocation)

User_Details:
(User_Id, Office_ID, Dept_Id, Service_Id, ServiceAllocation)

In #Exported_Data table, Status can be Inactive or Active.

In User_Details table, Allocation can be between 0 and 1.

User_Details table is a transaction table and #Exported_Data table is a temporary table which hold all records which need to be inserted into the Transaction table based on certain criteria.

I have a query:
insert into User_Details (User_Id, Office_ID, Dept_Id, Service_Id, ServiceAllocation)
select 
	User_Id, 
	Office_Id,
	Dept_Id,
	Service_Id,
	Allocation
from #Exported_Data ED
where not exists (select Office_ID, Dept_ID, Service_ID from User_Details UD where UD.User_Id = ED.User_Id)

Open in new window


In the above query, in place of Allocation in select query, I have to insert an Allocation value based on certain conditions. The conditions are:

If Status of the user in #Exported_Data is Inactive then make Allocation 0, else
      (1) Get Allocation of user from #Exported_Data
      (2) Get sum(Allocation) of user from User_Details table
      (3) If Allocation of (1) + sum(Allocation) < 1 then this Allocation value else skip insert

I tried using CASE statements but it is getting too complex to form the correct query.

Please help me get this conditional Allocation value to be used in place of Allocation in select query.
0
Comment
Question by:rpkhare
  • 4
  • 2
  • 2
8 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
Try this

insert into User_Details (User_Id, Office_ID, Dept_Id, Service_Id, ServiceAllocation)
select 
	User_Id, 
	Office_Id,
	Dept_Id,
	Service_Id,
	case 
		when Status = 'InActive' then 
			0
		else
			Allocation
	end
from #Exported_Data ED
inner join (select User_Id, sum(ServiceAllocation) [SumAllocation] 
			from User_Details
			group by User_id) UD2 on UD2.User_id = ED.User_Id
where not exists (select 1 from User_Details UD where UD.User_Id = ED.User_Id)
and ED.Allocation + UD2.SumAllocation  < 1

Open in new window

0
 
LVL 8

Author Comment

by:rpkhare
Comment Utility
I want to clear few this. In CASE, I want something like this:

case
            when Status = 'InActive' then
                  0
            else
                  If (Allocation + sum(Allocation) < 1 then (Allocation + sum(Allocation))
end


The above code is not SQL, I have just illustrated what I want.
Is your code doing the same?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
How can you compare allocation of user from #Exported_Data with the sum of the allocation of user from User_Details when your where condition requires that the user does not exist in both tables? Perhaps you can provide some sample data for the two tables and what you expect to see inserted in the user_details table.
0
 
LVL 8

Author Comment

by:rpkhare
Comment Utility
The exact row should not be duplicated.

A particular user can have different Office_ID, Dept_ID or Service_ID.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
Comment Utility
Do change your test for exists

insert into User_Details (User_Id, Office_ID, Dept_Id, Service_Id, ServiceAllocation)
select 
	User_Id, 
	Office_Id,
	Dept_Id,
	Service_Id,
	case 
		when Status = 'InActive' then 
			0
		else
			Allocation + UD2.SumAllocation
	end
from #Exported_Data ED
inner join (select User_Id, sum(ServiceAllocation) [SumAllocation] 
			from User_Details
			group by User_id) UD2 on UD2.User_id = ED.User_Id
where not exists (select 1 from User_Details UD 
               where UD.User_Id = ED.User_Id
               and UD.Office_Id = ED.Office_Id
               and UD.Service_Id = ED.Service_Id)
and ED.Allocation + UD2.SumAllocation  < 1

Open in new window

0
 
LVL 8

Author Comment

by:rpkhare
Comment Utility
Looks great. I will try and come back to you on this in a day.
0
 
LVL 8

Author Comment

by:rpkhare
Comment Utility
I tried the code today. I have few doubts.

In case a particular user has no records in the User_Details table, the inner join will prevent new record from being inserted from #Exported_Data table.

As of now I changed it to Left Join. Any further suggestions?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Sample data and the expected results would still be of great help.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now