Solved

Why am I getting an error with this SELECT statement?

Posted on 2016-08-05
5
37 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: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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query help 3 24
Convert VBA UDF to SQl SERVER UDF 4 46
user database (login sql or login windows) 3 18
Sql server Error message 3 13
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 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