0

@BlueFeetの助けを借りてこのクエリを取得しました。しかし、以下のクエリにはインスタンスタイプがあり、それに加えて、別のテーブルからのインスタンスタイプの説明が必要です。したがって、追加の2つのテーブルで結合する必要があります。

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で過去6週間の日曜日の日付までの各週でグループ化する方法は?

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 に答える