Solved

sql sub query

Posted on 2010-08-23
5
255 Views
Last Modified: 2012-05-10
I have duplicate records except date in a table.. Each record has the case# four times.. I want to put the latest case# in a temp table..
How can I get the case numbers with the max date inserted into a temp table? See my code below..

The error i get is:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Select * INTO ##tblCB

From

(SELECT * FROM CORE.tblStatusTracking ST 

WHERE nCaseID IN (	select distinct st.nCaseID, Max(st.dtCreatedDate)	from CTS.CORE.tblCase c			inner join CTS.CORE.tblStatusTracking st

				On c.nCaseID = t.nCaseID

				inner join CTS.CORE.tblCD_AR_CaseInfo ci on c.nCaseID = ci.nCaseID



				inner join CTSApp.app.tblOracleCustomers oc

				on ci.sCustID = oc.ACCOUNT_NUMBER



				where c.nUserID_PrimaryAgent = 5402  

				and c.bClosed = 0

				and CUSTOMER_TYPE = 'NP & SS'

				group by st.nCaseID)

											)tt

Open in new window

0
Comment
Question by:cheryl9063
5 Comments
 
LVL 3

Assisted Solution

by:mnachu
mnachu earned 250 total points
ID: 33502811
Try getting the Case ID and Max Date into a temp table first (or CTE) and then use that to join on the tblSTatusTracking to get that particular row.

Try whether the following would work:

select 

	distinct st.nCaseID, Max(st.dtCreatedDate)	as dtMaxCreatedDate

INTO

	#tmpMaxDt

from 

	CTS.CORE.tblCase c			

	inner join CTS.CORE.tblStatusTracking st On c.nCaseID = t.nCaseID

	inner join CTS.CORE.tblCD_AR_CaseInfo ci on c.nCaseID = ci.nCaseID

	inner join CTSApp.app.tblOracleCustomers oc on ci.sCustID = oc.ACCOUNT_NUMBER

where 

	c.nUserID_PrimaryAgent = 5402  

and c.bClosed = 0

and CUSTOMER_TYPE = 'NP & SS'

group by 

	st.nCaseID



SELECT 

	* 

FROM 

	CORE.tblStatusTracking ST 

	INNER JOIN #tmpMaxDt AS T ON T.nCaseID = ST.nCaseID AND ST.dtCreatedDAte = T.dtMaxCreatedDate

Open in new window

0
 
LVL 3

Expert Comment

by:mnachu
ID: 33502829
The reason you get the error is the Sub query next to the WHERE nCaseID IN, has more than one column in it.
0
 
LVL 9

Accepted Solution

by:
valkyrie_nc earned 250 total points
ID: 33502840
Try this instead (changes are in the WHERE clause)::

SELECT * INTO ##tblCB FROM (SELECT * FROM CORE.tblStatusTracking ST
WHERE EXISTS (SELECT TOP 1 * FROM CTS.CORE.tblCase c INNER JOIN CTS.CORE.tblCD_AR_CaseInfo ci ON c.nCaseID = ci.nCaseID INNER JOIN CTSApp.app.tblOracleCustomers oc ON ci.sCustID = oc.ACCOUNT_NUMBER WHERE c.nUserID_PrimaryAgent = 5402 AND c.bClosed = 0 AND CUSTOMER_TYPE = 'NP & SS'))

hth

valkyrie_nc
0
 
LVL 12

Expert Comment

by:mcv22
ID: 33502871

;WITH RankedCase(nCaseID, OrderID) AS
(
	select st.nCaseID,,ROW_NUMBER() OVER (PARTITION BY st.nCaseID ORDER BY st.dtCreatedDate DESC)
	from CTS.CORE.tblCase c	
	inner join CTS.CORE.tblStatusTracking st
		On c.nCaseID = t.nCaseID
	inner join CTS.CORE.tblCD_AR_CaseInfo ci
		on c.nCaseID = ci.nCaseID
	inner join CTSApp.app.tblOracleCustomers oc
		on ci.sCustID = oc.ACCOUNT_NUMBER
	where c.nUserID_PrimaryAgent = 5402  
	and c.bClosed = 0
	and CUSTOMER_TYPE = 'NP & SS'
)
SELECT
	ST.*
INTO
	##tblCB
FROM
	RankedCase rc
JOIN
	CORE.tblStatusTracking ST
ON
	rc.nCaseID = ST.nCaseID
WHERE
	rc.OrderID = 1

Open in new window

0
 
LVL 6

Expert Comment

by:LCSandman8301
ID: 33503105
if there aren't many columns you could just group the columns together and get the max date
insert into newtable(col1, col2, MaxDate)

select col1, col2, max(dtCreatedDate)

from table1

group by col1, col2

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

22 Experts available now in Live!

Get 1:1 Help Now