在sql server中使用group by子句时,计数函数出错

时间:2015-10-12 06:26:00

标签: sql sql-server

我正在计算每个类别中提交的链接数量。我为星期一分配了四个类别 - 分类,事件,文章,社会书签。用户提交了15个分类的链接。但它提交了15 * 4 = 60个链接。 输出就像这样

Categoryname    DayTarget   LnkSubmsnDate   LnkSubmtdBy submittedLinks  performance
Classifieds         15        10/12/2015        swapna       60             Reached

但我想这样

 Categoryname   DayTarget   LnkSubmsnDate   LnkSubmtdBy submittedLinks  performance
  Classifieds   15       10/12/2015       swapna         15             Reached
  Events        2        10/12/2015       swapna         0              not Reached
  Articles      2        10/12/2015        swapna         0             not Reached
Socialbookmarking 10     10/12/2015        swapna         0             not Reached

sql查询

select c.Categoryname,DayTarget,l.LnkSubmsnDate,l.LnkSubmtdBy,COUNT(LinkId) as submittedLinks,
   (CASE
  WHEN DayTarget=COUNT(LinkId)     THEN 'Reached'  
  WHEN DayTarget-COUNT(LinkId)   < 1  THEN 'Reached' 
  WHEN DayTarget-COUNT(LinkId)  >= 1 THEN 'Not Reached' 
END ) as performance
   from tbl_Link as l
    join Tbl_DaySubmission ds on l.ProjectId=ds.projectid and l.CategoryId=ds.CatId 
    join tbl_Category c on l.CategoryId=c.CategoryId
    where LnkSubmsnDate='2015-10-12'and l.ProjectId='109'
    group by c.Categoryname,ds.DayTarget,LnkSubmsnDate,l.LnkSubmtdBy 

表格定义

TABLE [dbo].[Tbl_DaySubmission](
    [DayId] [bigint] IDENTITY(100,1) NOT NULL,
    [DayName] [varchar](50) NOT NULL,
    [DayNumber] [int] NOT NULL,
    [CatId] [int] NULL,
    [DayTarget] [int] NOT NULL,
    [ProjectID] [int] NULL,
    [status] [bit] NULL

tbl_link

TABLE [dbo].[tbl_Link](
    [LinkId] [bigint] IDENTITY(1,1) NOT NULL,
    [LinkName] [nvarchar](255) NULL,
    [ReportLinks] [nvarchar](255) NULL,
    [CreatedDate] [nvarchar](255) NULL,
    [CategoryId] [int] NULL,
    [KeywordID] [int] NULL,
    [ProjectId] [int] NULL,
    [LnkSubmsnDate] [date] NULL,
    [LnkSubmtdBy] [nvarchar](255) NULL,

tbl_category

TABLE [dbo].[tbl_Category](
    [CategoryId] [int] IDENTITY(1,1) NOT NULL,
    [Categoryname] [varchar](50) NOT NULL

2 个答案:

答案 0 :(得分:0)

如果您获得预期值的倍数的答案,则表示您的联接无法正常工作(数据不符合您在其中一个表格中的预期)。

我们无法在没有看到所有表中的实际数据的情况下指导您,因为您没有使用主键来连接表。

我建议您将查询分解为多个步骤,并在加入另一个表之前评估每个步骤的结果,这将有助于您修复它。

答案 1 :(得分:0)

您无法从未提交的tbl_Links中检索LnkSubmsnDate和LnkSubmtdBy。如果未提交,则必须将默认值设置为2列,否则它们将显示为空。

SELECT
c.Categoryname
, ds.DayTarget 
, CASE WHEN (COUNT(LinkId) > 0) THEN l.LnkSubmsnDate
ELSE NULL END AS [LnkSubmsnDate]
, CASE WHEN (COUNT(LinkId) > 0) THEN l.LnkSubmtdBy
ELSE NULL END AS [LnkSubmtdBy]
, COUNT(LinkId) AS submittedLinks
, (CASE
  WHEN DayTarget=COUNT(LinkId)     THEN 'Reached'  
  WHEN DayTarget-COUNT(LinkId)   < 1  THEN 'Reached' 
  WHEN DayTarget-COUNT(LinkId)  >= 1 THEN 'Not Reached' 
END ) AS performance
FROM tbl_Category AS c
INNER JOIN Tbl_DaySubmission AS ds ON ds.CatId = c.CategoryId
LEFT JOIN (SELECT * FROM tbl_Link WHERE LnkSubmsnDate='2015-10-12') AS l ON l.ProjectId = ds.ProjectID AND l.CategoryId = ds.CatId
WHERE ds.ProjectID = 109
GROUP BY c.Categoryname, ds.DayTarget, l.LnkSubmsnDate, l.LnkSubmtdBy
ORDER BY performance DESC

http://sqlfiddle.com/#!6/548a26/7