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
Solved

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

Posted on 2013-05-17
8
439 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

792 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