Solved

TSql coding for conditional results

Posted on 2013-05-15
2
231 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 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

762 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

11 Experts available now in Live!

Get 1:1 Help Now