帮助!无法得到所需的结果

发布于 2024-11-07 03:16:12 字数 641 浏览 0 评论 0原文

使用 SQL Server 2008(R2)

我有一个需要以不同方式显示的结果集,它当前可用,如下所示:

Resource    Nr  Cost    StartDate   EndDate  
Cable_5m    8   3.5 12/03/2011  13/03/2011  
Cable_5m    2   3.5 13/03/2011  14/03/2011  
Rope125A    1   0   16/03/2011  18/03/2011  
Rope125A    1   0   17/03/2011  17/03/2011  

我需要的是按日期报告发送的每种资源的数量,如下所示:

DateOfEvnt Resource Nr Cost  
2011-03-12 Cable_5m 8 3.50  
2011-03-13 Cable_5m 10 3.50   
2011-03-14 Cable_5m 2 3.50  
2011-03-16 Rope125A 1 0.00  
2011-03-17 Rope125A 2 0.00  
2011-03-18 Rope125A 1 0.00

我有看过枢轴,但我一生都无法让它发挥作用,而且我不确定枢轴是否是我所需要的。所有帮助将不胜感激。

Using SQL Server 2008(R2)

I have a result set that I need displaying in a different way, it is currently available like:

Resource    Nr  Cost    StartDate   EndDate  
Cable_5m    8   3.5 12/03/2011  13/03/2011  
Cable_5m    2   3.5 13/03/2011  14/03/2011  
Rope125A    1   0   16/03/2011  18/03/2011  
Rope125A    1   0   17/03/2011  17/03/2011  

What I need is to report on a date basis how many of each resource are sent, like below:

DateOfEvnt Resource Nr Cost  
2011-03-12 Cable_5m 8 3.50  
2011-03-13 Cable_5m 10 3.50   
2011-03-14 Cable_5m 2 3.50  
2011-03-16 Rope125A 1 0.00  
2011-03-17 Rope125A 2 0.00  
2011-03-18 Rope125A 1 0.00

I have looked at pivot but cannot for the life of me get it to work, and I am not sure pivot is what I need. All help would be gratefully recieved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2024-11-14 03:16:12

对于给定的输入和输出,您根本不需要 PIVOT。一个简单的 UNIONGROUP BY 就足够了。

测试数据

DECLARE @Resources TABLE (
    Resource VARCHAR(32)
    , Nr INTEGER
    , Cost FLOAT
    , StartDate DATE
    , EndDate DATE
)

INSERT INTO @Resources 
    SELECT 'Cable_5m', 8, 3.5, '03/12/2011', '03/13/2011'
    UNION ALL SELECT 'Cable_5m', 2, 3.5, '03/13/2011', '03/14/2011'
    UNION ALL SELECT 'Rope125A', 1, 0,   '03/16/2011', '03/18/2011' 
    UNION ALL SELECT 'Rope125A', 1, 0,   '03/17/2011', '03/17/2011'

SQL语句

SELECT  DateOfEvnt
        , Resource
        , Nr = SUM(Nr)
        , Cost      
FROM    (           
            SELECT  Resource
                    , Nr
                    , Cost
                    , [DateOfEvnt] = StartDate
            FROM    @Resources
            UNION ALL       
            SELECT  Resource
                    , Nr
                    , Cost
                    , [DateOfEvnt] = EndDate
            FROM    @Resources
        ) r         
GROUP BY
        Resource
        , Cost
        , DateOfEvnt

编辑
虽然 UNIONGROUP BY 满足结果,但我开始怀疑您更需要这样的东西,其中 WITH 语句返回 Start-EndDate 之间每一天的记录。

;WITH q AS (
    SELECT  Resource
            , Nr
            , Cost
            , StartDate
            , DateOfEvnt = StartDate            
    FROM    @Resources
    UNION ALL
    SELECT  q.Resource
            , q.Nr
            , q.Cost
            , q.StartDate
            , DATEADD(day, 1, q.DateOfEvnt)         
    FROM    q
            INNER JOIN @Resources s ON s.Resource = q.Resource
                                       AND s.Nr = q.Nr
                                       AND s.StartDate = q.StartDate
    WHERE   q.DateOfEvnt < s.EndDate
)
SELECT  DateOfEvnt
        , Resource
        , Nr = SUM(Nr)
        , Cost
FROM    q
GROUP BY
        DateOfEvnt
        , Resource
        , Cost

For given in and outputs, you don't need a PIVOT at all. A simple UNION and GROUP BY would suffice.

Test data

DECLARE @Resources TABLE (
    Resource VARCHAR(32)
    , Nr INTEGER
    , Cost FLOAT
    , StartDate DATE
    , EndDate DATE
)

INSERT INTO @Resources 
    SELECT 'Cable_5m', 8, 3.5, '03/12/2011', '03/13/2011'
    UNION ALL SELECT 'Cable_5m', 2, 3.5, '03/13/2011', '03/14/2011'
    UNION ALL SELECT 'Rope125A', 1, 0,   '03/16/2011', '03/18/2011' 
    UNION ALL SELECT 'Rope125A', 1, 0,   '03/17/2011', '03/17/2011'

SQL Statement

SELECT  DateOfEvnt
        , Resource
        , Nr = SUM(Nr)
        , Cost      
FROM    (           
            SELECT  Resource
                    , Nr
                    , Cost
                    , [DateOfEvnt] = StartDate
            FROM    @Resources
            UNION ALL       
            SELECT  Resource
                    , Nr
                    , Cost
                    , [DateOfEvnt] = EndDate
            FROM    @Resources
        ) r         
GROUP BY
        Resource
        , Cost
        , DateOfEvnt

EDIT
While the UNION and GROUP BY satisfy the results, I'm starting to suspect you are more in need of something like this where the WITH statement returns a record for each day between Start- and EndDate.

;WITH q AS (
    SELECT  Resource
            , Nr
            , Cost
            , StartDate
            , DateOfEvnt = StartDate            
    FROM    @Resources
    UNION ALL
    SELECT  q.Resource
            , q.Nr
            , q.Cost
            , q.StartDate
            , DATEADD(day, 1, q.DateOfEvnt)         
    FROM    q
            INNER JOIN @Resources s ON s.Resource = q.Resource
                                       AND s.Nr = q.Nr
                                       AND s.StartDate = q.StartDate
    WHERE   q.DateOfEvnt < s.EndDate
)
SELECT  DateOfEvnt
        , Resource
        , Nr = SUM(Nr)
        , Cost
FROM    q
GROUP BY
        DateOfEvnt
        , Resource
        , Cost
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文