2014-12-04 82 views
0

我已经显示了错误,然后SQL与我添加注释掉的位,在这种状态下它工作正常。如果我在UNION部分回复我发现错误。 IT的所有作品和PremiseMeteredPricings是相同的PremisePricings领域明智等,可以很容易地做一个正常的联盟。任何人都知道为什么这个UNION给了我一个错误,我没有看到任何错误

出现错误: 消息207,级别16,状态1,行31 无效的列名称PremiseId“。 消息207,级别16,状态1,行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

你需要一个第二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 
+0

即使是这样说PremiseID是前提ID无效的列名。某种根本性错误:选择PremiseId,COUNT(*) 从PremisePricings 组由PremiseId UNION 选择PremiseId,COUNT(*)从PremiseMeteredPricings 组由PremiseId – John 2014-12-04 04:16:57

+0

@约翰 - 做你的'PremiseMeteredPricings'表有一个'PremiseId'列? – sgeddes 2014-12-04 04:17:39

+0

必须是明显的东西,我猜,今天早上缺乏咖啡,谢谢 – John 2014-12-04 04:39:48

相关问题