[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TSql coding for conditional results

Posted on 2013-05-15
2
Medium Priority
?
259 Views
Last Modified: 2013-05-15
I need to pass the records of a table, record by record, to another set of logic that will pull certain results based on various conditions.

In Table1 I have two cols.  1)  StoreCode  2)JobCode.  This table gets updated via another process.  Typically no more than 10 records.  

I need to take each record (each StoreCode and JobCode combination) and pull from say an Employees table to get a list of each employee that matches that StoreCode and JobCode.

The catch is this:  In theory there should only be one record in the Employees table for each StoreCode and Jobe Code.  If only one record does exist I want to pull 3 fields from the employess table (Fname, Lname, Rank).

If no records are found then I just want to return one record with NULL for each of those 3 fields.

If multiple records are found I want to return one record with the values 'Duplicates', NULL, NULL

In the end result I need a table that has the same number of records that were in Table1 with the results found in the conditional logic above.

Results table would look similar to this:  

Result Set based on conditional results
0
Comment
Question by:d1cjm1ex
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 39167727
Hi,
only limited time therefore it might not look good (and there are perhaps better options) but the result is as expected:
SELECT
	EPT.FName,
	EPT.LName,
	EPT.[Rank],
	CBT.StoreCode,
	CBT.JobCode
FROM tblCombo CBT
LEFT JOIN tblEmployee AS EPT
ON EPT.FKJobCode = CBT.JobCode AND EPT.FKStoreCode = CBT.StoreCode
LEFT JOIN (SELECT FKStoreCode,FKJobCode, 1 AS SQR FROM tblEmployee GROUP BY FKStoreCode,FKJobCode HAVING COUNT(1) < 2) AS EPT3
ON EPT3.FKJobCode = CBT.JobCode AND EPT3.FKStoreCode = CBT.StoreCode
WHERE EPT3.SQR = 1 OR EPT.FKStoreCode IS NULL
UNION
SELECT
	'Duplicate' AS FName,
	NULL AS LName,
	NULL AS [Rank],
	CBT.StoreCode,
	CBT.JobCode
FROM tblCombo CBT
JOIN (SELECT FKStoreCode,FKJobCode FROM tblEmployee GROUP BY FKStoreCode,FKJobCode HAVING COUNT(1) > 1) AS EPT2
ON EPT2.FKJobCode = CBT.JobCode AND EPT2.FKStoreCode = CBT.StoreCode

Open in new window

HTH
Rainer
0
 

Author Closing Comment

by:d1cjm1ex
ID: 39168549
works as advertised.  Thanks.  I have a logic twist I need to add but will post that as another question as you did answer this specific question.  Hopefully you can look at it as I want to keep the code you sent.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
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 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…
Suggested Courses

873 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