任何人都知道为什么这个UNION给了我一个错误,我没有看错

时间:2014-12-04 04:04:32

标签: sql sql-server tsql

我首先显示了错误,然后使用我添加的位注释了SQL,在这种状态下它运行正常。如果我在UNION部分发表评论,我会收到错误消息。所有的工作和PremiseMeteredPricings都与PremisePricings相同,并且可以很容易地做一个普通的UNION。

错误: Msg 207,Level 16,State 1,Line 31 列名称'PremiseId'无效。 Msg 207,Level 16,State 1,Line 18 列名称'PremiseId'无效。

SQL

select PremiseId,
  sum(case when PricingCategory = 'Water' and WholesalePricing=0 then FixedCharge end) RetailWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=0 then VolumetricCharge end) RetailWaterVar,
  sum(case when PricingCategory = 'Waste' and WholesalePricing=0 then FixedCharge end) RetailWasteFixed,sum(case when PricingCategory = 'Waste' and WholesalePricing=0 then VolumetricCharge end) RetailWasteVar,
  sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=0 then FixedCharge end) RetailRoadsFixed,sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=0 then VolumetricCharge end) RetailRoadsVar,
  sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=0 then FixedCharge end) RetailPropertyFixed,sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=0 then VolumetricCharge end) RetailPropertyVar,

  sum(case when PricingCategory = 'Water' and WholesalePricing=1 then FixedCharge end) WholesaleWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=1 then VolumetricCharge end) WholesaleWaterVar,
  sum(case when PricingCategory = 'Waste' and WholesalePricing=1 then FixedCharge end) WholesaleWasteFixed,sum(case when PricingCategory = 'Waste' and WholesalePricing=1 then VolumetricCharge end) WholesaleWasteVar,
  sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=1 then FixedCharge end) WholesaleRoadsFixed,sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=1 then VolumetricCharge end) WholesaleRoadsVar,
  sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=1 then FixedCharge end) WholesalePropertyFixed,sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=1 then VolumetricCharge end) WholesalePropertyVar

from PremisePricings 
where UserId = 'cdb370f7-b995-4d99-adc9-c00c7e837bb4'
group by PremiseId

UNION 

 select PremiseId,
  sum(case when PricingCategory = 'Water' and WholesalePricing=0 then FixedCharge end) RetailWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=0 then VolumetricCharge end) RetailWaterVar,
  sum(case when PricingCategory = 'Waste' and WholesalePricing=0 then FixedCharge end) RetailWasteFixed,sum(case when PricingCategory = 'Waste' and WholesalePricing=0 then VolumetricCharge end) RetailWasteVar,
  sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=0 then FixedCharge end) RetailRoadsFixed,sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=0 then VolumetricCharge end) RetailRoadsVar,
  sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=0 then FixedCharge end) RetailPropertyFixed,sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=0 then VolumetricCharge end) RetailPropertyVar,

  sum(case when PricingCategory = 'Water' and WholesalePricing=1 then FixedCharge end) WholesaleWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=1 then VolumetricCharge end) WholesaleWaterVar,
  sum(case when PricingCategory = 'Waste' and WholesalePricing=1 then FixedCharge end) WholesaleWasteFixed,sum(case when PricingCategory = 'Waste' and WholesalePricing=1 then VolumetricCharge end) WholesaleWasteVar,
  sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=1 then FixedCharge end) WholesaleRoadsFixed,sum(case when PricingCategory = 'Roads Drainage' and WholesalePricing=1 then VolumetricCharge end) WholesaleRoadsVar,
  sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=1 then FixedCharge end) WholesalePropertyFixed,sum(case when PricingCategory = 'Property Drainage' and WholesalePricing=1 then VolumetricCharge end) WholesalePropertyVar

 from PremiseMeteredPricings
 where UserId = 'cdb370f7-b995-4d99-adc9-c00c7e837bb4'
group by PremiseId

1 个答案:

答案 0 :(得分:1)

您的group by

需要第二个union声明
SELECT ...
FROM premises p
INNER JOIN
(
select PremiseId,
  sum(case when PricingCategory = 'Water' and WholesalePricing=0 then FixedCharge end) RetailWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=0 then VolumetricCharge end) RetailWaterVar,
  ...
from PremisePricings 
where UserId = 'cdb370f7-b995-4d99-adc9-c00c7e837bb4'    <--Here
group by PremiseId                                       <--Here 
union
select PremiseId,
  sum(case when PricingCategory = 'Water' and WholesalePricing=0 then FixedCharge end) RetailWaterFixed,sum(case when PricingCategory = 'Water' and WholesalePricing=0 then VolumetricCharge end) RetailWaterVar,
  ...
from PremiseMeteredPricings
where UserId = 'cdb370f7-b995-4d99-adc9-c00c7e837bb4'    <--And again here
group by PremiseId                                       <--And again here
) x on x.PremiseId = p.PremiseId 
order by CoreSPID