Solved

using field in select statement

Posted on 2013-05-29
7
303 Views
Last Modified: 2013-05-29
in the select statement below I set up a field AS VolumeDiscount. I would like to use VolumeDiscount in the same select statement right after. How would I do this?

SELECT     cod.OrderDetailsID, cod.OrderID, cod.DeliveryDateRequest, cod.GrowthStageID, cod.StandingOrder, cod.Comments, cod.Quantity, cod.ClientSpecialDiscount, 
                      cod.ResellerSpecialDiscount, cod.ProductID, 
                      CASE cod.GrowthStageID WHEN 3 THEN p.ProductPrice - 2 WHEN 2 THEN p.ProductPrice - 1 ELSE p.ProductPrice END AS ProductPrice
,CASE WHEN cod.Quantity < 5 THEN 0 WHEN cod.Quantity < 10 THEN 0.05 ELSE 0.1 END AS VolumeDiscount
,(p.ProductPrice-p.ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*po.PaymentDiscount) AS UnitPrice
FROM         WW.ClientOrderDetails AS cod INNER JOIN
                      WW.Products AS p ON cod.ProductID = p.ProductID INNER JOIN
                      WW.TaxCategories AS tc ON p.TaxCategoryID = tc.TaxCategoryID INNER JOIN
                      WW.ClientOrders AS co ON cod.OrderID = co.OrderID INNER JOIN
                      WW.PaymentOptions AS po ON co.PaymentOptionID = po.PaymentOptionID
WHERE     (cod.OrderID = 401001)

Open in new window

0
Comment
Question by:Shawn
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39206080
Unfortunately, you'll have to duplicate your CASE statement.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 39206087
You cannot use it with your current query, you need some thing like this
SELECT * ,(p.ProductPrice-p.ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*po.PaymentDiscount) AS UnitPrice

FROM (
SELECT     cod.OrderDetailsID, cod.OrderID, cod.DeliveryDateRequest, cod.GrowthStageID, cod.StandingOrder, cod.Comments, cod.Quantity, cod.ClientSpecialDiscount,
                      cod.ResellerSpecialDiscount, cod.ProductID,
                      CASE cod.GrowthStageID WHEN 3 THEN p.ProductPrice - 2 WHEN 2 THEN p.ProductPrice - 1 ELSE p.ProductPrice END AS ProductPrice
,CASE WHEN cod.Quantity < 5 THEN 0 WHEN cod.Quantity < 10 THEN 0.05 ELSE 0.1 END AS VolumeDiscount
FROM         WW.ClientOrderDetails AS cod INNER JOIN
                      WW.Products AS p ON cod.ProductID = p.ProductID INNER JOIN
                      WW.TaxCategories AS tc ON p.TaxCategoryID = tc.TaxCategoryID INNER JOIN
                      WW.ClientOrders AS co ON cod.OrderID = co.OrderID INNER JOIN
                      WW.PaymentOptions AS po ON co.PaymentOptionID = po.PaymentOptionID
WHERE     (cod.OrderID = 401001)
)A
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39206112
You can also define the inner select as a CTE, or a view, or like above, it remains the same. Only by using some intermediate table you can reuse results of expressions.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 1

Author Comment

by:Shawn
ID: 39206120
aneeshattingal
looks good but I'm getting the following error:
The multi-part identifier "p.ProductPrice" could not be bound.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39206132
Remove the p.'s from the first select column list.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39206135
Or change the alias A to p

WHERE     (cod.OrderID = 401001)
)A
 ------------------>
WHERE     (cod.OrderID = 401001)
)P

also remove the 'PO.'
0
 
LVL 1

Author Comment

by:Shawn
ID: 39206179
that worked niceley. just had to add po.PaymentDiscount in the other select query.
thank you!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question