在case语句中使用count

时间:2017-03-06 16:45:19

标签: sql count case

我需要一些帮助,我使用下面的SQL创建一个视图:

SELECT 

         TNTTITLE.Description_VC AS TITLE
        ,TNTCONTACT.Forename_VC AS FORENAME
        ,TNTCONTACT.Surname_VC AS SURNAME
        ,TNTCONTACTEMAIL.Reference_VC AS EMAIL  
        ,TNTHOMENUMBER_GRP.Reference_VC AS GROUP_HOME_NUMBER
       ,RENTACC.ID AS RENT_ACCOUNT_ID
       ,CONVERT(DATE,RENTACC.AccountStartDate) AS RENT_ACCOUNT_START_DT
       ,CASE
            WHEN LETOFFERS.Current_Status_ID = 42 THEN 'Complete'
        ELSE 'OTHER'
        END 
            AS STATUS
        ,ASSETTYPES.Description_VC AS ASSET_NAME
        ,CASE WHEN (SELECT MainAsset FROM RENTACC HAVING COUNT(*) > 1) THEN 1 ELSE 0 END


FROM RENT_ACCOUNTS AS RENTACC

        INNER JOIN LETTING_OFFERS_T AS LETOFFERS
            ON RENTACC.ID = LETOFFERS.Rent_Account_Reference_VC
                AND RENTACC.AccountEndDate IS NULL
                AND RENTACC.AccountType = 'T'

        LEFT JOIN Letting_SYS_Master_T AS LETCODES
            ON LETOFFERS.Current_Status_ID = LETCODES.CODE_ID
                AND LETCODES.Type_ID = '14'

        LEFT JOIN Asset_Assets_T AS ASSETS
            ON RENTACC.MainAsset = ASSETS.Asset_ID

        LEFT JOIN Asset_Types_T AS ASSETTYPES
            ON ASSETS.Asset_TYPE_ID = ASSETTYPES.Asset_Type_ID


        /**** MAIN CONTACT INFORMATION ****/

        LEFT JOIN Shared_Addresses_T AS TNTADDRESS
            ON ASSETS.Address_ID = TNTADDRESS.Address_ID

        LEFT JOIN Contact_Group_Contacts_T AS TNTGROUP
            ON RENTACC.ContactDatabaseReference = TNTGROUP.GROUP_ID
                AND TNTGROUP.Main_Group_Contact_BT = 1
                AND TNTGROUP.Removed_BT = 0

        LEFT JOIN CONTACT_CONTACTS_T AS TNTCONTACT
            ON TNTGROUP.Contact_ID = TNTCONTACT.Contact_ID
                AND TNTCONTACT.Current_Status_ID = 65
                AND TNTCONTACT.Deceased_Date_DT IS NULL 

        LEFT JOIN Shared_Codes_T AS TNTTITLE
            ON TNTCONTACT.Title_Code_ID = TNTTITLE.Code_ID
                AND TNTTITLE.Code_ID IN (103,104,105,106)

        /**** CONTACT EMAIL INFORMATION ****/

        LEFT JOIN Contact_Contact_Details_T AS TNTCONTACTEMAIL
            ON TNTCONTACT.Contact_ID = TNTCONTACTEMAIL.Contact_ID
                AND TNTCONTACTEMAIL.SYS_Number_Code_ID = 75
                AND TNTCONTACTEMAIL.Expiry_Date_DT IS NULL


        /**** GROUP HOME NUMBER INFORMATION  ****/

        LEFT JOIN Contact_Group_Details_T AS TNTHOMENUMBER_GRP
            ON RENTACC.ContactDatabaseReference = TNTHOMENUMBER_GRP.Group_ID
                AND TNTHOMENUMBER_GRP.Expiry_Date_DT IS NULL
                AND TNTHOMENUMBER_GRP.SYS_Number_Code_ID = 71

我试图在case语句中进行计数,以显示rentacc表中出现多次显示'1'的任何MainAsset代码以及只显示一次以显示'0'的任何mainasset代码。但这不起作用。但是我也想在完整的rentacc表(对于rent_accounts表别名)中这样做,因为RENTACC只是视图的别名。

例如:

RENT_ACCOUNTS表

Main Asset- 
1234 
1234 
1000 
1234

我想在视图中显示的内容(RENT VIEW)如下:

main asset, case
1234, 1
1000, 0

无论如何我可以这样做,因为我得到的案例陈述不起作用?

谢谢, 丹

2 个答案:

答案 0 :(得分:0)

您可以将案例重写为

CASE WHEN count(MainAsset) > 1 THEN 1 ELSE 0 END

但是您必须将select中的所有其他列添加到group by子句中。

答案 1 :(得分:0)

您可以使用case编写exists()表达式,如下所示:

, case when exists (
      select 1 
      from RENT_ACCOUNTS as i 
      where i.MainAsset = RENTACC.MainAsset
      group by i.MainAsset
      having count(*) > 1
    ) 
      then 1 
    else 0 
    end as Multi_MainAsset

根据您的平台,最好使用子查询来导出RENTACC而不是count(*) over()

select ...
  , case when MainAssetCount > 1 then 1 else 0 end as Multi_MainAsset
from (
  select *
    , count(*) over (partition by MainAsset) as MainAssetCount
  from RENT_ACCOUNTS 
  ) AS RENTACC
...