[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

TSql coding for conditional results

Posted on 2013-05-15
2
Medium Priority
?
257 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
[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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

649 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