如何在同一列上使用group by时明智地计算列日期

时间:2015-03-02 11:50:31

标签: sql-server

我遇到的情况是我有6张不同的桌子和在每个表中都有一列CreatedBy。我必须为每张桌子计算总CreatedBy个DateWise&在网格中显示它们。 例如,情况就像,我想显示一个员工为每个表做了多少创建操作,这些操作将在日期显示。

请注意CreatedBy列包含一些整数值(id)。我有一个单独的表格,每个CreatedBy在加入后获取他的名字。

假设一位员工在2015年2月2日说Xyz在表1中创建了3行,在表2中创建了2行& asume其余表的任何值。 结果表就像:

03/02/XYZ 3 2 anyValue Anyvalue AnyValue anyvalue
04/02/XYZ 6 8 anyValue  Anyvalue AnyValue  anyvalue

所以我试着编写一个截至目前的查询。但是当我试图获得结果日期时得到错误的值。所以请帮助我。

    SELECT A.CreatedBy, COUNT1 AS CreatedMasterClass1,COUNT2 AS CreatedMasterClass2,ISNULL(COUNT3,0) AS CreatedPropertyValue ,COUNT4 AS CreatedBasicProperty,COUNT5 AS CreatedVariationClass1,COUNT6 AS CreatedVariationClass2 FROM
    (SELECT DISTINCT M1.CreatedBy, COUNT (M1.CreatedBy) AS COUNT1 FROM [dbo].[Master_Class1] M1 GROUP BY M1.CreatedBy)
    A LEFT JOIN
    (SELECT DISTINCT M2.CreatedBy ,COUNT (M2.CreatedBy) AS  COUNT2 FROM [dbo].[Master_Class2] M2  GROUP BY M2.CreatedBy)
    B ON A.CreatedBy=B.CreatedBy
    LEFT JOIN
    (SELECT DISTINCT M3.CreatedBy, COUNT (M3.CreatedBy) AS COUNT3 FROM [dbo].[PropertyValue] M3 GROUP BY M3.CreatedBy)
    C ON A.CreatedBy=C.CreatedBy
    LEFT JOIN(SELECT DISTINCT M4.CreatedBy ,COUNT (M4.CreatedBy) AS COUNT4 FROM [dbo].[BasicProperty] M4  GROUP BY M4.CreatedBy)
    D ON A.CreatedBy=D.CreatedBy
    LEFT JOIN
(SELECT DISTINCT M5.CreatedBy ,COUNT (M5.CreatedBy) AS COUNT5 FROM [dbo].[Variation_Class1] M5  GROUP BY M5.CreatedBy )
    E ON A.CreatedBy=E.CreatedBy LEFT JOIN
    (SELECT DISTINCT M6.CreatedBy , COUNT(M6.CreatedBy) AS COUNT6 FROM [dbo].[Variation_Class2] M6  GROUP BY M6.CreatedBy )
    F ON A.CreatedBy=F.CreatedBy

1 个答案:

答案 0 :(得分:0)

如果要包含日期,请修改您的查询以将其返回;我想这就是你想要的:

SELECT 
    A.CreatedBy, 
    A.CreatedDate, 
    COUNT1 AS CreatedMasterClass1,
    COUNT2 AS CreatedMasterClass2,
    ISNULL(COUNT3,0) AS CreatedPropertyValue ,
    COUNT4 AS CreatedBasicProperty,
    COUNT5 AS CreatedVariationClass1,
    COUNT6 AS CreatedVariationClass2 
FROM (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT1 
    FROM [dbo].[Master_Class1] GROUP BY CreatedBy, CreatedDate
    ) A 
LEFT JOIN (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT2 
    FROM [dbo].[Master_Class2] GROUP BY CreatedBy, CreatedDate
    ) B ON A.CreatedBy = B.CreatedBy AND A.CreatedDate = B.CreatedDate
LEFT JOIN (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT3 
    FROM [dbo].[PropertyValue] GROUP BY CreatedBy, CreatedDate
    ) C ON A.CreatedBy = C.CreatedBy AND A.CreatedDate = C.CreatedDate
LEFT JOIN (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT4 
    FROM [dbo].[BasicProperty] GROUP BY CreatedBy, CreatedDate
    ) D ON A.CreatedBy = D.CreatedBy AND A.CreatedDate = D.CreatedDate
LEFT JOIN (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT5 
    FROM [dbo].[Variation_Class1] GROUP BY CreatedBy, CreatedDate
    ) E ON A.CreatedBy = E.CreatedBy AND A.CreatedDate = E.CreatedDate
LEFT JOIN (
    SELECT CreatedBy, CreatedDate, COUNT(CreatedBy) AS COUNT6 
    FROM [dbo].[Variation_Class2] GROUP BY CreatedBy, CreatedDate
    ) F ON A.CreatedBy = F.CreatedBy AND A.CreatedDate = E.CreatedDate
ORDER BY A.CreatedDate

此外,执行distinct

时,不需要group by关键字