-1

我正在尝试从Vendor该表中的列中提取一些数据,为我的每个供应商创建新列。理想情况下,每个 1 行ContractDate,然后是 2 个值。但是,我最终得到了 2 行不同的ContractDate.

我相信我可能需要某种临时表查询来执行此操作……但我不确定。

SELECT [ContractDate], 
       CASE WHEN Vendor = 'AirDat' 
            THEN (sum(wf.Temppop) / sum(wf.Population)) END as 'AirDat',
       CASE WHEN Vendor = 'CWG' 
            THEN (sum(wf.Temppop) / sum(wf.Population)) END as 'CWG'
FROM [ECPDB].[dbo].[weather.forecast] as wf 
INNER JOIN ecpdb.[lookup].[newWeatherStation] as ws
ON wf.[Station_ID] = ws.[Station ID]
INNER JOIN ecpdb.[lookup].[CountyNew] as c
ON ws.[County FIPS] = c.[County FIPS]
WHERE tradedate = '7/2/2012'
AND [BENTEK Cell] = 'Northeast'
GROUP BY [ContractDate], vendor
4

1 回答 1

1

您可以使用子查询来执行此操作;

Select ContractDate, 
     max(case when Vendor = 'AirDat' THEN Vendor_Average End) as AirDAT,  
     max(case when Vendor = 'CWG' THEN Vendor_Average End) as CWG
  from (
  SELECT [ContractDate] , Vendor, (sum(wf.Temppop) / sum(wf.Population)) as Vendor_Average
  FROM [ECPDB].[dbo].[weather.forecast] as wf 
  inner join ecpdb.[lookup].[newWeatherStation] as ws
     on wf.[Station_ID] = ws.[Station ID]
  inner join ecpdb.[lookup].[CountyNew] as c
     on ws.[County FIPS] = c.[County FIPS]
  where tradedate = '7/2/2012' and [BENTEK Cell] = 'Northeast' 
group by ContractDate, Vendor
) as Subquery
     group by Contractdate

这样,查询就会运行并找到您需要的值,然后您无需分组即可选择所需的行。

于 2012-07-02T15:46:24.110 回答