Solved

SQL Query - Specify which record to join to

Posted on 2014-11-06
7
347 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

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

In this article I will describe the Detach & Attach 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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

863 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

21 Experts available now in Live!

Get 1:1 Help Now