SQL Server:Select和Join语句中的Sum(列)和count(列)

时间:2014-06-09 19:00:02

标签: sql-server select join count sum

我试图查询每个loyaltyID列在AnthemTxns_Jr中的交易次数以及所有交易总数的总和。现在,当我运行这个脚本时,它说,

  

找不到任何一列" dbo"或用户定义的函数或聚合" dbo.AnthemTxns_JR.count",或名称含糊不清

代码:

Select DISTINCT 
   dbo.ANTHEM_IDS_JR.loyaltyID,   
   dbo.ANTHEM_IDS_JR.Military_Type,,
   dbo.ANTHEM_IDS_JR.Military_Date, 
   dbo.AnthemTxns_JR.count(CheckTotal), 
   dbo.AnthemTxns_JR.sum(CheckTotal)
From 
   dbo.ANTHEM_IDS_JR
JOIN 
   dbo.AnthemTxns_JR ON dbo.ANTHEM_IDS_JR.loyaltyID = dbo.AnthemTxns_JR.loyaltyID
WHERE  
   Military_Type = 'Active Duty' 
   AND ACTIVE = 1 
   AND datalength(dbo.ANTHEM_IDS_JR.Military_Date) > 0 
ORDER BY 
   loyaltyID;

1 个答案:

答案 0 :(得分:0)

试试这个...看起来你在选择语句中混淆了一下

Select DISTINCT 
       dbo.ANTHEM_IDS_JR.loyaltyID,   
       dbo.ANTHEM_IDS_JR.Military_Type,
       dbo.ANTHEM_IDS_JR.Military_Date, 
       count(dbo.AnthemTxns_JR.CheckTotal) as CheckTotalCount, 
       sum(dbo.AnthemTxns_JR.CheckTotal) as CheckTotalSum
    From 
       dbo.ANTHEM_IDS_JR
    JOIN 
       dbo.AnthemTxns_JR ON dbo.ANTHEM_IDS_JR.loyaltyID = dbo.AnthemTxns_JR.loyaltyID
    WHERE  
       Military_Type = 'Active Duty' 
       AND ACTIVE = 1 
       AND datalength(dbo.ANTHEM_IDS_JR.Military_Date) > 0 
    Group by 
    dbo.ANTHEM_IDS_JR.loyaltyID, 
    dbo.ANTHEM_IDS_JR.Military_Type,    
    dbo.ANTHEM_IDS_JR.Military_Date  
    ORDER BY 
       loyaltyID;