如何使重复列数据为空

时间:2014-01-10 10:39:31

标签: sql sql-server sql-server-2008 tsql

以下是我在应用几个连接后得到的查询结果。

enter image description here

我想首先显示VendorName列的值,并为Exp1列的每个相似值显示为NULL。

代表:

2011   Air Canada    9
2012   NULL          9
2016   NULL          9

依旧......

我用来生成结果的查询是:

SELECT     d.ForecastAccountID, d.Portfolio, d.Name, d.AccountNumber, d.AccountName, d.CashGAAP, d.OriginalDate, d.Amount, d.Grouping, d.TheDate, d.CashGAAPYear, 
                      a.VendorName, a.ForecastAccountID AS Expr1
FROM         (SELECT     Forecast_Account.ForecastAccountID, Portfolio.LegalEntityName AS Portfolio, Forecast.Name, Forecast_Account.AccountNumber, CoA.AccountName, 
                                              Forecast_Data.CashGAAP, Forecast_Data.TheDate AS OriginalDate, SUM(Forecast_Data.Amount) AS Amount, Attribute_Set.AttributeSetName AS Grouping, 
                                              DATEADD(m, DATEDIFF(m, 0, Forecast_Data.TheDate), 0) AS TheDate, YEAR(Forecast_Data.TheDate) AS CashGAAPYear
                       FROM          Portfolio INNER JOIN
                                              Forecast ON Portfolio.PortfolioID = Forecast.PortfolioID INNER JOIN
                                              Forecast_Account ON Forecast.ForecastID = Forecast_Account.ForecastID INNER JOIN
                                              Forecast_Data ON Forecast_Account.ForecastAccountID = Forecast_Data.ForecastAccountID INNER JOIN
                                              CoA ON CoA.AccountNumber = Forecast_Account.AccountNumber INNER JOIN
                                              Attribute_Set ON Forecast_Account.AttributeSetID = Attribute_Set.AttributeSetID
                       GROUP BY Forecast_Account.ForecastAccountID, Portfolio.LegalEntityName, Forecast.Name, Forecast_Account.AccountNumber, CoA.AccountName, 
                                              Forecast_Data.CashGAAP, Forecast_Data.TheDate, Attribute_Set.AttributeSetName) AS d LEFT OUTER JOIN
                          (SELECT     Vendor.VendorName, Forecast_Attribute.ForecastAccountID
                            FROM          Forecast_Attribute INNER JOIN
                                                   Vendor ON Forecast_Attribute.Value = Vendor.VendorName
                            GROUP BY Forecast_Attribute.ForecastAccountID, Vendor.VendorName) AS a ON d.ForecastAccountID = a.ForecastAccountID

2 个答案:

答案 0 :(得分:1)

尝试这样

Select CashGAApYear,
       Case
            when Row > 1 then Null
            else VendorName end AS VendorName,
       Exp1 
From
(
   SELECT CashGAApYear, 
          VendorName,
          Exp1,
          ROW_NUMBER() OVER(PARTITION BY VendorName,CashGAApYear  ORDER BY VendorName  ASC) AS Row
    from TableName
)  AS T

答案 1 :(得分:0)

select c1,
CASE rn
WHEN 1 THEN c2
ELSE NULL
END
,c3 from 
(
select Row_number() over (order by c1 asc ) as rn ,c1,c2,c3 from mytable 
)ttable