Solved

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

Posted on 2013-05-17
8
421 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
ID: 39176989
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
ID: 39177051
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 32

Expert Comment

by:awking00
ID: 39177386
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
ID: 39177495
The exact row should not be duplicated.

A particular user can have different Office_ID, Dept_ID or Service_ID.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 39178174
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
ID: 39178181
Looks great. I will try and come back to you on this in a day.
0
 
LVL 8

Author Comment

by:rpkhare
ID: 39182169
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 32

Expert Comment

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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

919 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

18 Experts available now in Live!

Get 1:1 Help Now