Solved

SQL Query - Specify which record to join to

Posted on 2014-11-06
7
351 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

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
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!

 
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 142

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 142

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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