2

合計を表示する 2 つのテーブルを結合no. of appointmentsし、キーワードをleads使用するクエリを作成unionしました。このクエリの結果を 2 つの異なる列に表示したいのですが、難しい説明があります。

SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredAppointment 
    ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
UNION
SELECT COUNT(FilteredLead.fullname) AS Lead
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredLead 
    ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (@Branch))

私の望ましい結果は次のとおりです。

CRITERIA | Appointment | Leads
Total    | 200         | 123
4

2 に答える 2

0

ユニオンは正しいアプローチではありません。ユニオンは、同じ数の列とデータ型を持つ結果セットに結合またはマージします。代わりにこのようなことをします。

SELECT 
    (   SELECT COUNT(FilteredAppointment.createdbyname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Appointment ,
    (   SELECT COUNT(FilteredLead.fullname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Lead

数字を他のブランチと比較できるようにするというあなたのコメントに応えて、私はこのようなことを試みます.

SELECT  FilteredBusinessUnit.name , 
        SUM(CASE WHEN FilteredAppointment.createdbyname IS NOT NULL THEN 1 ELSE 0 END) AS Appointments,
        SUM(CASE WHEN FilteredAppointment.fullname IS NOT NULL THEN 1 ELSE 0 END) AS Leads
FROM    FilteredBusinessUnit 
        INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
        LEFT OUTER JOIN FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        LEFT OUTER JOIN FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE   (FilteredBusinessUnit.name IN (@Branch1, @Branch2)) 
GROUP BY FilteredBusinessUnit.name

すべてのブランチを調べたい場合は、where 句を省略できます。

于 2013-09-11T15:21:33.550 に答える
0

cross joinではなくa でこれを行いますunion:

select 'Total', Appointment, Leads
from (SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
     ) c1 cross join
     (SELECT COUNT(FilteredLead.fullname) AS Leads
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
      WHERE (FilteredBusinessUnit.name IN (@Branch))
     ) t
于 2013-09-11T15:21:49.760 に答える