Solved

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

Posted on 2013-05-17
8
433 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

778 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