Solved

Why am I getting an error with this SELECT statement?

Posted on 2016-08-05
5
32 Views
Last Modified: 2016-08-05
Here's my SELECT query:

 select a.* from
    (
         select b.encountercode, patient.patientid, practicepatientid, practiceaccountid, client.clientcode, practice.practicecode, practicename, patientfname, patientlname, patientdob, dbo.ufn_getSSN(patient.accountid) as patientssn, patientgender, patient.accountid, accountstatuscode, accountstatuscode2, patientadminstatuscode, patientfinancialstatuscode 

        , ( (case when account.practiceaccountid like ('%Johnson%') then 1 else 0 end)  +  (case when patient.patientlname like ('%Johnson%') then 1 else 0 end)  +  (case when patient.patientfname like ('%Johnson%') then 1 else 0 end)  +  (case when account.accountid like ('%Johnson%') then 1 else 0 end)  +  (case when b.encountercode like ('%Johnson%') then 1 else 0 end) ) as hits from 
        account (nolock)
            join patient (nolock) on patient.accountid = account.accountid
            join practice (nolock) on account.practiceid = practice.PracticeID
            join client (nolock) on practice.clientid = client.clientid
			LEFT JOIN (SELECT TOP 1 ChargeGroup.AccountID, ChargeGroup.encountercode FROM ChargeGroup (nolock) 
			where 
			ChargeGroup.encountercode='Johnson'
			AND
			ChargeGroup.clientcode='AIRCARE') b 
			ON Account.AccountID=b.AccountID
        where client.clientcode in('AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE','DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO','LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD','TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC')
        ) a
    where hits > 0
    order by  hits desc, patientlname asc 

Open in new window


When it runs, I get this error:

Msg 102, Level 15, State 1, Line 14
Incorrect syntax near ','.

Line 14 being: ChargeGroup.clientcode='AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE',
                  'DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO',
                  'LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD',
                  'TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC') b

When I run it as ChargeGroup.clientcode='AIRCARE') b - no problem. There appears to be something flawed in the way I'm running it with more than one option, but I don't know how to fix it.

Thoughts/
0
Comment
Question by:brucegust
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 41744610
Perhaps line 14 should use the IN() predicate?

The given syntax
ChargeGroup.clientcode='AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE', ...

is incorrect.


Kent
0
 

Author Comment

by:brucegust
ID: 41744620
So, Kent, I tried this:

  select a.* from
    (
         select b.encountercode, patient.patientid, practicepatientid, practiceaccountid, client.clientcode, practice.practicecode, practicename, patientfname, patientlname, patientdob, dbo.ufn_getSSN(patient.accountid) as patientssn, patientgender, patient.accountid, accountstatuscode, accountstatuscode2, patientadminstatuscode, patientfinancialstatuscode 

        , ( (case when account.practiceaccountid like ('%Johnson%') then 1 else 0 end)  +  (case when patient.patientlname like ('%Johnson%') then 1 else 0 end)  +  (case when patient.patientfname like ('%Johnson%') then 1 else 0 end)  +  (case when account.accountid like ('%Johnson%') then 1 else 0 end)  +  (case when b.encountercode like ('%Johnson%') then 1 else 0 end) ) as hits from 
        account (nolock)
            join patient (nolock) on patient.accountid = account.accountid
            join practice (nolock) on account.practiceid = practice.PracticeID
            join client (nolock) on practice.clientid = client.clientid
			LEFT JOIN (SELECT TOP 1 ChargeGroup.AccountID, ChargeGroup.encountercode FROM ChargeGroup (nolock) 
			where 
			ChargeGroup.encountercode='Johnson'
			AND
			ChargeGroup.clientcode IN ('AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE','DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO','LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD','TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC')) b 
			ON Account.AccountID=b.AccountID
        where client.clientcode in('AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE','DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO','LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD','TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC')
        ) a
    where hits > 0
    order by  hits desc, patientlname asc 

Open in new window


...and I got the same error. Did I do it correctly or do you see something else...?

Let me know.

Thanks!
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 41744645
Hi Bruce,

I've reformatted the original query to make it easier (for me) to read and changed the filter to use IN().

See if this one works.

Kent



select a.* from
(
  select 
    b.encountercode, 
    patient.patientid, 
    practicepatientid, 
    practiceaccountid, 
    client.clientcode, 
    practice.practicecode, 
    practicename, 
    patientfname, 
    patientlname, 
    patientdob, 
    dbo.ufn_getSSN(patient.accountid) as patientssn, 
    patientgender, 
    patient.accountid, 
    accountstatuscode, 
    accountstatuscode2, 
    patientadminstatuscode, 
    patientfinancialstatuscode,
    (
      (case when account.practiceaccountid like ('%Johnson%') then 1 else 0 end)
      +  (case when patient.patientlname like ('%Johnson%') then 1 else 0 end)  
      +  (case when patient.patientfname like ('%Johnson%') then 1 else 0 end)  
      +  (case when account.accountid like ('%Johnson%') then 1 else 0 end)  
      +  (case when b.encountercode like ('%Johnson%') then 1 else 0 end) 
    ) as hits 
  from account (nolock)
  join patient (nolock) on patient.accountid = account.accountid
  join practice (nolock) on account.practiceid = practice.PracticeID
  join client (nolock) on practice.clientid = client.clientid
  LEFT JOIN (SELECT TOP 1 ChargeGroup.AccountID, ChargeGroup.encountercode 
             FROM ChargeGroup (nolock) 
             where ChargeGroup.encountercode='Johnson'
               AND ChargeGroup.clientcode in('AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE','DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO','LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD','TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC')
            ) b 
    ON Account.AccountID=b.AccountID
  where client.clientcode in('AIRCARE','ALLEN','AMG','BCMH','BBGH','CMI','DALE','DHDC','DCC','DMH','ECH','FCH','GAH','DEMO','HRMC','DSCHD','DSCHD2','HOUCOHO','LMCH','MCGH','SDP','MERCY','OVSH','PF','PCMG','RCC','SMC','SMH','TONC','TRAD','TPS','TTT','00077793111','UGH','URMC','UTH','UTS','CPAP','CPAPNew','WCH','WMC')
) a
where hits > 0
order by  hits desc, patientlname asc

Open in new window

0
 

Author Comment

by:brucegust
ID: 41744685
Kent! That did it! Thank you very much! My workplace has just given me my cape and my superhero soundtrack, so rock on!

Just to explain back to you what the problem was and why your fix worked...

As long as you're looking for a single value, the equal sign works just fine. But when you expand the number of values that you're looking for, you now have to incorporate the "IN" operator in order for the SELECT to fire correctly, yes?

Thanks again!
0
 
LVL 45

Expert Comment

by:Kdo
ID: 41744692
Hi Bruce,

That's correct.  The two valid syntaxes are:

  WHERE a = b

  WHERE a in ('A', 'B')

Note that behind the covers, the second form actually evaluates as

  WHERE (a = 'A' or a = 'B')

There is no performance penalty (or gain) using either one.


Good Luck!
Kent
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

919 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

17 Experts available now in Live!

Get 1:1 Help Now