如何在SQL Server中使用嵌套大小写

时间:2018-03-08 02:37:32

标签: sql sql-server

我有以下查询,需要一些调整。我编辑了我的问题。 我在 Data_table 中有一个名为 FileID

的列

此列FileID具有从1开始的增量编号。因此,以下脚本适用于 FileID< = 54 。但对于FileID> 55,我想更改以下代码以使用' DataDescription2 '而不是1.

        with data as ( SELECT  A.[WalkAwayFileID],
                A.[SubmissionNumber],       -- check DD1/DD2
                A.[MasterPolicyNumber],
                MAX(CASE when  DataDescription1 = 'Total Bound Premium ($)' then ivalue else NULL end) as WalkawayBoundPremium,
                MAX(CASE when  DataDescription1 = 'Underwriter Adjusted Benchmark Premium ($) for Layer Bound' then iValue else NULL end) as UAB_Premium, 
                MAX(case when DataDescription1 = 'Currency' then svalue else NULL end ) as Currency


        FROM Tool_Record A 
        JOIN Data_table B ON A.[WalkAwayFileID] = B.FileID 

        group by a.WalkAwayFileID, a.SubmissionNumber, a.MasterPolicyNumber
        ) 

        select   *From data 

以下是我强调如何捕获​​数据的基础表。

enter image description here

1 个答案:

答案 0 :(得分:0)

你能不能简单地添加逻辑来处理这个问题:

WITH data AS (
    SELECT
        A.[WalkAwayFileID],
        A.[SubmissionNumber],
        A.[MasterPolicyNumber],
        MAX(CASE WHEN FileID <= 54 AND DataDescription1 = 'Total Bound Premium ($)'
            OR
                FileID > 54 AND DataDescription2 = 'Total Bound Premium ($)'
                 THEN ivalue ELSE NULL END) AS WalkawayBoundPremium,
        MAX(CASE WHEN FileID <= 54 AND
            DataDescription1 = 'Underwriter Adjusted Benchmark Premium ($) for Layer Bound'
            OR
                FileID > 54 AND
            DataDescription2 = 'Underwriter Adjusted Benchmark Premium ($) for Layer Bound'
                 THEN iValue ELSE NULL END) AS UAB_Premium, 
        MAX(CASE WHEN FileID <= 54 AND DataDescription1 = 'Currency' OR
                      FileID > 54 AND DataDesription2 = 'Currency'
                 THEN svalue ELSE NULL END) AS Currency
    FROM Tool_Record A 
    INNER JOIN Data_table B
        ON A.[WalkAwayFileID] = B.FileID 
    GROUP BY
        a.WalkAwayFileID, a.SubmissionNumber, a.MasterPolicyNumber
 )