0

我在@Blue Feet 的帮助下得到了这个查询。但是下面的查询我有实例类型,除此之外我需要来自另一个表的实例类型描述。所以我需要与额外的两个表进行连接。

Table - RelationShips:
------------------------
Table Name                          Column Name                 Key
CSPGI50_VALID_INSTANCE_TYPE         CSPGI50_INSTANCE_TYPE_C     Primary
CSPGJ02_MSG_OBJ                     CSPGJ02_MSG_K               Primary 
CSPGI03_MANUAL_ASN                  CSPGI03_ASN_C                primary
CSPGJ03_MSG_TRANSLN                 CSPGJ03_LOCALE_C              primary   

Column Having tables:
------------------------------
CSPGI50_VALID_INSTANCE_TYPE(CSPGJ02_MSG_K,CSPGI50_INSTANCE_TYPE_C)
CSPGJ03_MSG_TRANSLN(CSPGJ02_MSG_K)
CSPGI03_MANUAL_ASN(CSPGI03_INSTANCE_TYPE_C)
CSPGJ02_MSG_OBJ(CSPGJ02_MSG_K)

我有以下查询,

SET DATEFIRST 1  
SELECT 
    case when InstanceType is not null then InstanceType else 'Sum' End InstanceType , 
    sum([39]) AS FirstWeek, 
    sum([38]) AS SecondWeek,   
    sum([37]) AS ThirdWeek, 
    sum([36]) AS FourthWeek, 
    sum([35]) AS FifthWeek, 
    sum([34]) AS SixthWeek  
FROM 
(
    SELECT [SPGI03_INSTANCE_TYPE_C] AS InstanceType, 
        [39], [38], [37], [36], [35], [34]  
    FROM 
    ( 
        SELECT I03.[SPGI03_INSTANCE_TYPE_C],  
            DatePart(wk, I03.[SPGI03_CREATE_S]) WeekNo, 
            DATEADD(DAY, 7 -DATEPART(WEEKDAY,I03.[SPGI03_CREATE_S]),
            I03.[SPGI03_CREATE_S]) WeekEnd  
        FROM [SUPER-G].[dbo].[CSPGI03_MANUAL_ASN] I03, [SUPER-G].[dbo].[CSPGI50_VALID_INSTANCE_TYPE] I50  
        where I50.[SPGI50_INSTANCE_TYPE_C] = I03.[SPGI03_INSTANCE_TYPE_C] 
            and  I50.[SPGA04_RATING_ELEMENT_D] = 2 
            and I03.[SPGI03_EXCEPTIONED_F] = 'N'  
            and I03.[SPGI03_DISPUTED_F] != 'Y'  
            and I03.[SPGI03_CREATE_S] between '08-19-2012 12:00:00.000' AND DATEADD(day , 7, '09-30-2012 11:59:59.000' 
    )) x 
    pivot 
    ( 
        count(WeekEnd) 
        FOR weekno IN ([39], [38], [37], [36], [35], [34])  
    ) p 
) x1 
GROUP BY  InstanceType WITH ROLLUP

我想要的是:

我想从

Table 2-column name([SPGJ03_MSG_TRANSLN_X])

如果您有疑问,请参考我之前的查询链接 如何在 sql 中按每周到最后六个星期日的日期分组?

4

1 回答 1

0

很难说这些表是如何相关的,但看起来你想做这样的事情:

SET DATEFIRST 1  
SELECT 
    case when InstanceType is not null then InstanceType else 'Sum' End InstanceType , 
    sum([39]) AS FirstWeek, 
    sum([38]) AS SecondWeek,   
    sum([37]) AS ThirdWeek, 
    sum([36]) AS FourthWeek, 
    sum([35]) AS FifthWeek, 
    sum([34]) AS SixthWeek,
    max(InstanceDescription)
FROM 
(
    SELECT [SPGI03_INSTANCE_TYPE_C] AS InstanceType, 
        [39], [38], [37], [36], [35], [34], InstanceDescription
    FROM 
    ( 
        SELECT I03.[SPGI03_INSTANCE_TYPE_C],  
            DatePart(wk, I03.[SPGI03_CREATE_S]) WeekNo, 
            DATEADD(DAY, 7 -DATEPART(WEEKDAY,I03.[SPGI03_CREATE_S]),
            I03.[SPGI03_CREATE_S]) WeekEnd,
            J03.SPGJ03_MSG_TRANSLN_X InstanceDescription
        FROM [SUPER-G].[dbo].[CSPGI03_MANUAL_ASN] I03
        INNER JOIN [SUPER-G].[dbo].[CSPGI50_VALID_INSTANCE_TYPE] I50  
          ON I50.[SPGI50_INSTANCE_TYPE_C] = I03.[SPGI03_INSTANCE_TYPE_C] 
        LEFT JOIN CSPGJ02_MSG_OBJ J02
          ON I50.SPGJ02_MSG_K = J02.SPGJ02_MSG_K
        LEFT JOIN CSPGJ03_MSG_TRANSLN J03
          ON J02.SPGJ02_MSG_K = J03.SPGJ02_MSG_K
        where I50.[SPGA04_RATING_ELEMENT_D] = 2 
            and I03.[SPGI03_EXCEPTIONED_F] = 'N'  
            and I03.[SPGI03_DISPUTED_F] != 'Y'  
            and I03.[SPGI03_CREATE_S] between '08-19-2012 12:00:00.000' AND DATEADD(day , 7, '09-30-2012 11:59:59.000' 
    )) x 
    pivot 
    ( 
        count(WeekEnd) 
        FOR weekno IN ([39], [38], [37], [36], [35], [34])  
    ) p 
) x1 
GROUP BY  InstanceType WITH ROLLUP
于 2012-10-12T13:43:58.857 回答