Solved

Why am I getting an error with this SELECT statement?

Posted on 2016-08-05
5
40 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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:
Kent Olsen 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:Kent Olsen
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

631 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