错误:当EXISTS未引入子查询时,只能在选择列表中指定一个表达式

时间:2014-08-07 18:40:22

标签: sql sql-server

我有以下查询给出了错误"当子查询未引入EXISTS"时,只能在选择列表中指定一个表达式。我无法确定这在技术上是错误的。

select clinicid,
       DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )) as YEAR,
       DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )) as MONTH,
       REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-') AS SalMonYY,
       (select rpTotal.clinicid,SUM(rpTotal.rpPackage) as rpPackage from 
        (
        select clinicid, count(1) as rpPackage
              from PackagePatients
              WHERE PackagePatients.clinicid = clinicid
              AND convert(date,patientdate) <  convert(date,saledate)
              group by clinicid
        union all
        select s.homeClinicId as clinicid,COUNT(1) as rpPackage
            from subscriptionHistory s 
            join patients p on s.ptientId = p.id 
            join products pl on s.productId = pl.id
            WHERE s.HomeClinicId = clinicid
            and pl.expDuration > 1
            group by s.homeClinicId) rpTotal group by rpTotal.clinicid) AS rpPackagesSold 
from PackagePatients

group by clinicid,
         DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )),
         DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )),
         REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-')

2 个答案:

答案 0 :(得分:1)

没有什么可以合作的,我想你可以按照这些方式进行一些修改。这应该很接近。至少它会告诉你如何使这项工作。老实说,它可能需要完全重写。我没有表格,但我确信这可以完全没有子查询。

select clinicid,
       DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )) as YEAR,
       DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )) as MONTH,
       REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-') AS SalMonYY,
       rp.rpPackagesSold

from PackagePatients
join
(select SUM(rpTotal.rpPackage) as rpPackagesSold, clinicid
     from 
        (
        select clinicid, count(1) as rpPackage
              from PackagePatients
              WHERE PackagePatients.clinicid = clinicid
              AND convert(date,patientdate) <  convert(date,saledate)
              group by clinicid
        union all
        select s.homeClinicId as clinicid,COUNT(1) as rpPackage
            from subscriptionHistory s 
            join patients p on s.ptientId = p.id 
            join products pl on s.productId = pl.id
            WHERE s.HomeClinicId = clinicid
            and pl.expDuration > 1
            group by s.homeClinicId) rpTotal group by rpTotal.clinicid
) AS rp on rp.clinicid = PackagePatients.clinicid
group by clinicid,
         DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )),
         DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )),
         REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-')

答案 1 :(得分:1)

您需要将派生表移动到from子句,如下所示:

select clinicid,
       DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )) as YEAR,
       DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )) as MONTH,
       REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-') AS SalMonYY,
       SUM(rpPackagesSold.rpPackage) AS rpTotal

from PackagePatients
join 
(
        select clinicid, count(1) as rpPackage
              from PackagePatients
              WHERE PackagePatients.clinicid = clinicid
              AND convert(date,patientdate) <  convert(date,saledate)
              group by clinicid
        union all
        select s.homeClinicId as clinicid,COUNT(1) as rpPackage
            from subscriptionHistory s 
            join patients p on s.ptientId = p.id 
            join products pl on s.productId = pl.id
            WHERE s.HomeClinicId = clinicid
            and pl.expDuration > 1
            group by s.homeClinicId
) rpPackagesSold ON (rpPackagesSold.clinicid = PackagePatients.clinicid)


group by clinicid,
         DATEPART(YEAR,CONVERT(date,PackagePatients.SaleDate )),
         DATEPART(MONTH,CONVERT(date,PackagePatients.SaleDate )),
         REPLACE(RIGHT(CONVERT(VARCHAR(9),PackagePatients.SaleDate, 6), 6), ' ', '-')