问题转出数据(导致问题的货币)

时间:2018-05-31 13:15:54

标签: sql

我正在创建一个视图,它将为一组数据显示值类型。问题是我有5个唯一的货币值(EUR,USD,MYR,CYN,NULL),是的我知道NULL不是一个值,但NULL是个问题。虽然我确实想要不同货币的不同数据行,但我不希望NULL的行不同,因为NULL仅表示在该特定值类型(SalesVolume)中未使用Currency。 UOM也是如此。我只想要一行数据(对于每种货币(只有一个UOM))。这是我的疑问:

SELECT Year, Period, [Year-Period], Region, [Op Co] AS OpCo, PMC, [PMC Description] AS PMCDescription, Currency, UOM,
MAX(CASE WHEN [Value Type] = 'Net Revenue' THEN Value END) AS NetRevenue,
MAX(CASE WHEN [Value Type] = 'Sales Volume' THEN Value END) AS SalesVolume,
MAX(CASE WHEN [Value Type] = 'Std Gross Margin' THEN Value END) AS StdGrossMargin,
[Customer Relationship] AS CustomerRelationship
FROM gdw.FactBudgetSummaryNew
GROUP BY Year, Period, [Year-Period], Region, [Op Co], PMC, [PMC Description], Currency, UOM, [Customer Relationship]

我目前得到这个: enter image description here

但是,正如您所看到的,对于Sales Volume,Currency的值为NULL,这会导致一个额外的列,我希望它只是组合。

2 个答案:

答案 0 :(得分:1)

似乎将不同的数据混合在一起。尝试按UOM计算分组(如果它只是1个不同的值)与Currency分组分开计算。

;WITH UOMData AS
(
    SELECT 
        Year, 
        Period, 
        [Year-Period], 
        Region, 
        [Op Co] AS OpCo, 
        PMC, 
        [PMC Description] AS PMCDescription, 
        UOM,
        MAX(CASE WHEN [Value Type] = 'Sales Volume' THEN Value END) AS SalesVolume,
        [Customer Relationship] AS CustomerRelationship
    FROM 
        gdw.FactBudgetSummaryNew
    WHERE
        UOM IS NOT NULL
    GROUP BY 
        Year, 
        Period, 
        [Year-Period], 
        Region, 
        [Op Co], 
        PMC, 
        [PMC Description], 
        UOM, 
        [Customer Relationship]
),
CurrencyData AS
(
    SELECT 
        Year, 
        Period, 
        [Year-Period], 
        Region, 
        [Op Co] AS OpCo, 
        PMC, 
        [PMC Description] AS PMCDescription, 
        Currency, 
        MAX(CASE WHEN [Value Type] = 'Net Revenue' THEN Value END) AS NetRevenue,
        MAX(CASE WHEN [Value Type] = 'Std Gross Margin' THEN Value END) AS StdGrossMargin,
        [Customer Relationship] AS CustomerRelationship
    FROM 
        gdw.FactBudgetSummaryNew
    WHERE
        Currency IS NOT NULL
    GROUP BY 
        Year, 
        Period, 
        [Year-Period], 
        Region, 
        [Op Co], 
        PMC, 
        [PMC Description], 
        Currency, 
        [Customer Relationship]
)
SELECT
    M.Year, 
    M.Period, 
    M.[Year-Period], 
    M.Region, 
    M.OpCo, 
    M.PMC, 
    M.PMCDescription, 
    M.UOM, 
    C.Currency,
    M.CustomerRelationship,
    M.SalesVolume,
    C.StdGrossMargin,
    C.NetRevenue
FROM
    UOMData AS M
    INNER JOIN CurrencyData AS C ON
        M.Year = C.Year AND
        M.Period = C.Period AND
        M.[Year-Period] = C.[Year-Period] AND
        M.Region = C.Region AND
        M.OpCo = C.OpCo AND
        M.PMC = C.PMC AND
        M.PMCDescription = M.PMCDescription AND
        M.CustomerRelationship = C.CustomerRelationship

答案 1 :(得分:0)

这会有用吗?

MAX(CASE
        WHEN [Value Type] = 'Net Revenue' AND UOM IS NOT NULL THEN Value
    END) AS NetRevenue,
MAX(CASE
        WHEN [Value Type] = 'Sales Volume' AND Currency IS NOT NULL THEN Value
    END) AS SalesVolume,
MAX(CASE
        WHEN [Value Type] = 'Std Gross Margin' AND UOM IS NOT NULL THEN Value
    END) AS StdGrossMargin,
[Customer Relationship] AS CustomerRelationship
FROM gdw.FactBudgetSummaryNew
GROUP BY YEAR,
         Period,
         [Year-Period],
         Region,
         [Op Co],
         PMC,
         [PMC Description],
         Currency,
         UOM,
         [Customer Relationship]