Author |
Topic |
seeker62
Starting Member
40 Posts |
Posted - 2013-02-18 : 11:57:57
|
The following query has many calculations. I would like to use one calculation (GrossAmount) in another calculation in the same query. When i do the query below it says that it cannot find the column GrossAmount.SELECT *, (ISNULL(InvoiceVolume,0)+ISNULL(PendingVolume,0)+ISNULL(ScheduledVolume,0)-ISNULL(ContractedVolume,0)) as Difference, (ISNULL(ContractedVolume,0)/2000) as ContractedTons, (ISNULL(InvoiceVolume,0)/2000) as InvoicedTons, (ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) as GrossAmount, ((([GrossAmount]) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0)) as NetDue, ((((ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0))/(ISNULL(InvoiceVolume,0)/2000)) as NetBack FROM [EP_DataWarehouse].[dbo].[RptPortfolioCubeData] WITH (NOLOCK) INNER JOIN [EP_Main].[dbo].[Product] WITH (NOLOCK) ON [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[ProductType] = [EP_Main].[dbo].[Product].[ProductName] WHERE [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[LoadDate] BETWEEN '01/01/2013' AND '01/31/2013' AND [EP_Main].[dbo].[Product].[ProductID] = 2 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-18 : 12:14:32
|
You cannot use the alias you created in the select list (in this case GrossAmount) in another column in the select list. Either you have to repeat all the same computations or, you have to make the whole query into a sub-query like shown below:-- repeat the calculationsSELECT *, (ISNULL(InvoiceVolume,0)+ISNULL(PendingVolume,0)+ISNULL(ScheduledVolume,0)-ISNULL(ContractedVolume,0)) as Difference, (ISNULL(ContractedVolume,0)/2000) as ContractedTons, (ISNULL(InvoiceVolume,0)/2000) as InvoicedTons, (ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) as GrossAmount, ((((ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0))) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0)) as NetDue, ((((ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0))/(ISNULL(InvoiceVolume,0)/2000)) as NetBackFROM [EP_DataWarehouse].[dbo].[RptPortfolioCubeData] WITH (NOLOCK) INNER JOIN [EP_Main].[dbo].[Product] WITH (NOLOCK) ON [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[ProductType] = [EP_Main].[dbo].[Product].[ProductName]WHERE [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[LoadDate] BETWEEN '01/01/2013' AND '01/31/2013'AND [EP_Main].[dbo].[Product].[ProductID] = 2-- or make it into a subquerySELECT *, ((([GrossAmount]) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0)) as NetDueFROM(SELECT *, (ISNULL(InvoiceVolume,0)+ISNULL(PendingVolume,0)+ISNULL(ScheduledVolume,0)-ISNULL(ContractedVolume,0)) as Difference, (ISNULL(ContractedVolume,0)/2000) as ContractedTons, (ISNULL(InvoiceVolume,0)/2000) as InvoicedTons, (ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) as GrossAmount, ((((ISNULL(InvoiceVolume, 0)*ISNULL(GrossPrice,0)) + ISNULL(FuelSurchargeAmount,0)) - ISNULL(FreightAmount,0) - ISNULL(MktgFeeAmount,0) - ISNULL(ValueAddedAmount,0))/(ISNULL(InvoiceVolume,0)/2000)) as NetBackFROM [EP_DataWarehouse].[dbo].[RptPortfolioCubeData] WITH (NOLOCK) INNER JOIN [EP_Main].[dbo].[Product] WITH (NOLOCK) ON [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[ProductType] = [EP_Main].[dbo].[Product].[ProductName]WHERE [EP_DataWarehouse].[dbo].[RptPortfolioCubeData].[LoadDate] BETWEEN '01/01/2013' AND '01/31/2013'AND [EP_Main].[dbo].[Product].[ProductID] = 2) s |
|
|
seeker62
Starting Member
40 Posts |
Posted - 2013-02-18 : 12:17:52
|
ok thanks. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-18 : 12:18:51
|
You are welcome - glad to help. |
|
|
|
|
|