Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subquery Question 2

Status
Not open for further replies.
Nov 7, 2002
61
US
Hi,

I'm running the following subquery:

Code:
select *
into #SumDaysTemp 
from (select * from #SumDays 
	join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
	From #Sumdays a
	group by a.AcctNum, a.firstname, a.lastname, a.dob
	Having (Count(AcctNum) > 1)) B on 
	c.AcctNum = B.AcctNum)

The subquery itself runs fine if I run it by itself. All I want to do is put the results of the subquery into a temp table "#SumDaysTemp" to do some date math and I'm getting the following error:

Line 11: Incorrect syntax near ')'.

Any help would be greatly appreciated.

Thanks,
Mike
 
You have an extra paren.

select *
into #SumDaysTemp
from (select * from #SumDays
join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
From #Sumdays a
group by a.AcctNum, a.firstname, a.lastname, a.dob
Having (Count(AcctNum) > 1)) B on
c.AcctNum = B.AcctNum)

try this.

select *
into #SumDaysTemp
from (select * from #SumDays
join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
From #Sumdays a
group by a.AcctNum, a.firstname, a.lastname, a.dob
Having (Count(AcctNum) > 1) B on
c.AcctNum = B.AcctNum)

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
I don't see the alias c anywhere.

--> c.AcctNum

Another thing you'll get reamed for in here, you should never put "SELECT *". You should always specify all fields needed in a query.

I think your query isn't running cause you have to alias the derived table

Code:
select *
into #SumDaysTemp 
from (select * from #SumDays 
    join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
    From #Sumdays a
    group by a.AcctNum, a.firstname, a.lastname, a.dob
    Having (Count(AcctNum) > 1)) B on 
    c.AcctNum = B.AcctNum) [!]d[/!]

[monkey][snake] <.
 
You have an extra paren.

select *
into #SumDaysTemp
from (select * from #SumDays
join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
From #Sumdays a
group by a.AcctNum, a.firstname, a.lastname, a.dob
Having (Count(AcctNum) > 1)) B on
c.AcctNum = B.AcctNum)

try this.

select *
into #SumDaysTemp
from (select * from #SumDays
join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
From #Sumdays a
group by a.AcctNum, a.firstname, a.lastname, a.dob
Having (Count(AcctNum) > 1) B on
c.AcctNum = B.AcctNum)

Now I'm getting the error message: Incorrect Syntax on Line 8 Near "B
 
Sorry about that. I was having a hard time reading the format.

Code:
[COLOR=blue]Select[/color] *
[COLOR=blue]into[/color] #SumDaysTemp
[COLOR=blue]from[/color] ([COLOR=blue]select[/color] * 
       [COLOR=blue]from[/color] #SumDays
        [COLOR=blue]join[/color] ([COLOR=blue]Select[/color] [COLOR=#FF00FF]Max[/color](Acctnum) [COLOR=blue]as[/color] AcctNumID
                  , a.AcctNum
                  ,a.firstname 
                  ,a.lastname
                  ,a.dob
             [COLOR=blue]From[/color] #Sumdays a
             [COLOR=blue]group[/color] [COLOR=blue]by[/color] a.AcctNum
                     ,a.firstname
                     ,a.lastname
                     ,a.dob
              [COLOR=blue]Having[/color] ([COLOR=#FF00FF]Count[/color](AcctNum) > 1) ) B
      [COLOR=blue]on[/color] c.AcctNum = B.AcctNum)

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Have you not tried my solution?
(Look where the **** is)
Code:
select *
into #SumDaysTemp 
from (select * from #SumDays 
    join (Select Max(Acctnum) as AcctNumID,a.AcctNum, a.firstname, a.lastname, a.dob
    From #Sumdays a
    group by a.AcctNum, a.firstname, a.lastname, a.dob
    Having (Count(AcctNum) > 1)) B on 
    c.AcctNum = B.AcctNum) ****d****

[monkey][snake] <.
 
Yes, I tried it, sorry. I got the message:

The column 'acctnum' was specified multiple times for 'd'.
 
I figured it out. I had to alias the columns in the subquery.

Here's the code that worked:

Code:
select * into #SumDaysTemp from
(select c.AcctNum,c.FirstName,c.LastName,c.DOB,c.SSN,c.AdmitDate,c.DischargeDate
,c.CoverageID,c.StartDate,c.EndDate,c.ProgramType,c.FinClassCurrent,c.InsuranceCode1
,c.InsuranceCode2,c.InsuranceCode3,c.ServiceType,c.DOB as DateOfBirth
 from #SumDays c
join (Select Max(Acctnum) as AcctNumID, a.AcctNUm as AN, a.firstname as FN
	, a.lastname as ln, a.dob as db
	From #Sumdays a
	group by a.AcctNum, a.firstname, a.lastname, a.dob
	Having (Count(AcctNum) > 1)) B
on c.AcctNum = B.AN) d

Thanks for all of your help.

Mike
 
Remove all references to 'a':

Code:
    join (Select Max(Acctnum) as AcctNumID, AcctNum, firstname, lastname, dob
    From #Sumdays 
    group by AcctNum, firstname, lastname, dob

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top