Solved

TSql coding for conditional results

Posted on 2013-05-15
2
242 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
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

820 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