Solved

SQL Query - Specify which record to join to

Posted on 2014-11-06
7
354 Views
Last Modified: 2014-11-06
Hi All,

I have a table of unique codes which has a one to many relationship with a table of statuses against these codes.

Most of the time, one code has one status, but it is possible to have two statuses for one code. When there are two statuses, I want to join to the status that does not start 'xxx'.

Thanks
0
Comment
Question by:James Elliott
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40425806
can you show some data samples? maybe something to play with on sqlfiddle?
I have some ideas, but without having concrete data (and respective required output) it will rather be guesswork
just a couple of records
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40425818
How about this (This assumes a "dbo.status" table with values "Status" and "Code":

with MyStatus1 as (
select *, row_number() over(partition by code order by case substring(Status,1,3) when 'xxx' then 1 else 0 end, status) as MyRow
from dbo.status),
MyStatus2 as
(Select * from MyStatus1 where MyRow = 1)
select *
from MyStatus2

Open in new window

0
 
LVL 12

Author Comment

by:James Elliott
ID: 40425847
Ok, sorry for the vague question.

I have the following SQL which gives me a list of codes with status, in which there are duplicates. Basically, where there are two entries, I want to select the entry that does not start "Closed".

Thanks

SELECT
	a.Charge_Code,
	CASE WHEN d.Category_1 = 'CLOSED' AND a.Eligible = 1 THEN 'Closed - Accepted'
		 WHEN d.category_1 = 'CLOSED' AND a.Eligible = 0 THEN 'Closed - Rejected'
		 ELSE d.category_1
	END as status	
	
FROM
	tblRequestLines a
	
INNER JOIN
	tblStatusLines b
ON b.Request_Line_ID = a.ID

INNER JOIN
	tblStatuses c
ON b.Status_ID = c.ID

INNER JOIN
	tblStatusCategoryMap d
ON d.Status_ID = c.ID

WHERE b.End_Date IS NULL

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 200 total points
ID: 40425858
With d1 as
(Select *, row_number() over(partition by d.Status_ID order by case substring(d.Category_1,1,6) when 'CLOSED' then 1 else 0 end, d.Category_1) as MyRow
FROM tblStatusCategoryMap d),
d2 as
(Select * from d1 where MyRow = 1)
SELECT
	a.Charge_Code,
	CASE WHEN d.Category_1 = 'CLOSED' AND a.Eligible = 1 THEN 'Closed - Accepted'
		 WHEN d.category_1 = 'CLOSED' AND a.Eligible = 0 THEN 'Closed - Rejected'
		 ELSE d.category_1
	END as status	
	
FROM
	tblRequestLines a
	
INNER JOIN
	tblStatusLines b
ON b.Request_Line_ID = a.ID

INNER JOIN
	tblStatuses c
ON b.Status_ID = c.ID

INNER JOIN
	d2 d
ON d.Status_ID = c.ID

WHERE b.End_Date IS NULL

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 40425860
you can do like this:

select * from ( SELECT
	a.Charge_Code,
	CASE WHEN d.Category_1 = 'CLOSED' AND a.Eligible = 1 THEN 'Closed - Accepted'
		 WHEN d.category_1 = 'CLOSED' AND a.Eligible = 0 THEN 'Closed - Rejected'
		 ELSE d.category_1
	END as status	
  , row_number() over (partition by a.Charge_Code order by case when d.Category_1 = 'CLOSED'  then 2 else 1 end) rn
	
FROM
	tblRequestLines a
	
INNER JOIN
	tblStatusLines b
ON b.Request_Line_ID = a.ID

INNER JOIN
	tblStatuses c
ON b.Status_ID = c.ID

INNER JOIN
	tblStatusCategoryMap d
ON d.Status_ID = c.ID

WHERE b.End_Date IS NULL
              ) sq
where sq.rn = 1                            

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40425861
along the same lines :)
0
 
LVL 12

Author Closing Comment

by:James Elliott
ID: 40426016
Thanks both. I went with Guy's solution in the end as it returned the correct number of records.
0

Featured Post

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.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

733 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