使用SQL Server查询SSRS

时间:2016-11-09 03:14:00

标签: sql-server ssrs-2012

我正在开发SSRS报告,需要有关数据集查询的帮助。我不知道如何编写查询来实现所需的数据。

查询1:

SELECT DISTINCT FullDate,
  SUM([Gross]) AS Gross
  ,StoreName

FROM [T1] inner join [T2]
on T1.StoreKey = T2.StoreKey

inner join T3
on T1.DateKey = T3.DateKey

where [Store] = 'B'
and FullDate = '2016-10-24'

group by FullDate,
StoreName

结果是:

 FullDate     Gross      StoreName
2016-10-24   2621.89          B

QUERY2:

SELECT DISTINCT FullDate,SUM(TotalCost) AS TotalCost,
  SUM([Gross]) AS Gross
  ,StoreName

FROM [T1] inner join [T2]
on T1.StoreKey = T2.Storekey

inner join T3
on T1.DateKey = T3.DateKey

inner join T4
on T1.DateKey = T4.DateKey
and T1.StoreKey = T4.StoreKey


where [Store] = 'B'
and FullDate = '2016-10-24'

group by FullDate,
StoreName

结果是:

FullDate      TotalCost     Gross        StoreName
2016-10-24       5         20060.12         B

我想用query2实现2621.89,这样我就可以在一个表中使用SSRS。

1 个答案:

答案 0 :(得分:0)

试试这个..我在Left Join

中分别获取了TotalCost列查询
    SELECT DISTINCT FullDate SUM([Gross]) AS Gross
        ,StoreName
        ,SUBSET.TotalCost
    FROM [T1]
    INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
    INNER JOIN T3 ON T1.DateKey = T3.DateKey
    INNER JOIN T4 ON T1.DateKey = T4.DateKey
        AND T1.StoreKey = T4.StoreKey
    LEFT JOIN (
        SELECT DISTINCT FullDate SUM(TotalCost) AS TotalCost
            ,
            ,StoreName
        FROM [T1]
        INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
        INNER JOIN T3 ON T1.DateKey = T3.DateKey
        INNER JOIN T4 ON T1.DateKey = T4.DateKey
            AND T1.StoreKey = T4.StoreKey
        WHERE [Store] = 'B'
            AND FullDate = '2016-10-24'
        GROUP BY FullDate
            ,StoreName
        ) AS SUBSET ON FullDate = SUBSET.FullDate
        AND [Store] = SUBSET.FullDate
    WHERE [Store] = 'B'
        AND FullDate = '2016-10-24'
    GROUP BY FullDate
        ,StoreName
        ,SUBSET.TotalCost
        ,
    SELECT DISTINCT FullDate SUM([Gross]) AS Gross
        ,StoreName
        ,SUBSET.TotalCost
    FROM [T1]
    INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
    INNER JOIN T3 ON T1.DateKey = T3.DateKey
    INNER JOIN T4 ON T1.DateKey = T4.DateKey
        AND T1.StoreKey = T4.StoreKey
    LEFT JOIN (
        SELECT DISTINCT FullDate SUM(TotalCost) AS TotalCost
            ,
            ,StoreName
        FROM [T1]
        INNER JOIN [T2] ON T1.StoreKey = T2.Storekey
        INNER JOIN T3 ON T1.DateKey = T3.DateKey
        INNER JOIN T4 ON T1.DateKey = T4.DateKey
            AND T1.StoreKey = T4.StoreKey
        WHERE [Store] = 'B'
            AND FullDate = '2016-10-24'
        GROUP BY FullDate
            ,StoreName
        ) AS SUBSET ON FullDate = SUBSET.FullDate
        AND [Store] = SUBSET.FullDate
    WHERE [Store] = 'B'
        AND FullDate = '2016-10-24'
    GROUP BY FullDate
        ,StoreName
        ,SUBSET.TotalCost
        ,