Solved

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

Posted on 2013-05-17
8
447 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
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
Ephraim Wangoya 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 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