Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

update from select

Posted on 2013-05-30
8
211 Views
Last Modified: 2013-05-31
Can't seem to get this to work. Any ideas?

UPDATE    WW.ClientOrders
SET              TotalPrice = T.TP, ToatalGST = T.TG, TotalPST = T.TP, TotalIncludingTax = T.TotalIncludingTaxes
SELECT     SUM(TotalPrice) AS TP, SUM(TotalGST) AS TG, SUM(TotalPST) AS TP, SUM(TotalPrice+TotalGST+TotalPST) AS TotalIncludingTaxes
FROM         (SELECT   ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity,2) AS TotalPrice
,ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity*GST,2) AS TotalGST
,ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity*PST,2) AS TotalPST
FROM  (SELECT po.PaymentDiscount, 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.TaxCategoryID, tc.GST, 
	  tc.PST
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 = @OrderID)) AS p) AS T

Open in new window

0
Comment
Question by:Shawn
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39210114
re-looking - sorry
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 39210117
How about this:

WITH T (TP, TG, TPST, TotalIncludingTaxes) AS
(
SELECT SUM(TotalPrice) AS TP, SUM(TotalGST) AS TG, SUM(TotalPST) AS TPST, SUM(TotalPrice+TotalGST+TotalPST) AS TotalIncludingTaxes
FROM (SELECT   ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity,2) AS TotalPrice
,ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity*GST,2) AS TotalGST
,ROUND(((p.ProductPrice-ProductPrice*VolumeDiscount)-((p.ProductPrice-p.ProductPrice*VolumeDiscount)*PaymentDiscount))*Quantity*PST,2) AS TotalPST
FROM (SELECT po.PaymentDiscount, 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.TaxCategoryID, tc.GST, 
	  tc.PST
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 = @OrderID)) AS p
)
UPDATE    WW.ClientOrders
SET              TotalPrice = T.TP, ToatalGST = T.TG, TotalPST = T.TPST, TotalIncludingTax = T.TotalIncludingTaxes
FROM T;

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39210119
This is your existing query - reformatted

there is a FROM ( at line 6 missing

but also at lines 7-10 refer to SUM()s but there is no corresponding GROUP BY
(and I don't know hat it should be)

additionally there are more SUM()s at lines 12-14, but again there is not matching GROUP BY (and I don't know hat it should be)

do you know what those group bys should be?
UPDATE WW.ClientOrders
SET TotalPrice = T.TP
	, ToatalGST = T.TG
	, TotalPST = T.TP
	, TotalIncludingTax = T.TotalIncludingTaxes

SELECT SUM(TotalPrice) AS TP
	, SUM(TotalGST) AS TG
	, SUM(TotalPST) AS TP
	, SUM(TotalPrice + TotalGST + TotalPST) AS TotalIncludingTaxes
FROM (
	SELECT ROUND(((p.ProductPrice - ProductPrice * VolumeDiscount) - ((p.ProductPrice - p.ProductPrice * VolumeDiscount) * PaymentDiscount)) * Quantity, 2) AS TotalPrice
		, ROUND(((p.ProductPrice - ProductPrice * VolumeDiscount) - ((p.ProductPrice - p.ProductPrice * VolumeDiscount) * PaymentDiscount)) * Quantity * GST, 2) AS TotalGST
		, ROUND(((p.ProductPrice - ProductPrice * VolumeDiscount) - ((p.ProductPrice - p.ProductPrice * VolumeDiscount) * PaymentDiscount)) * Quantity * PST, 2) AS TotalPST
	FROM (
		SELECT po.PaymentDiscount
			, 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.TaxCategoryID
			, tc.GST
			, tc.PST
		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 = @OrderID)
		) AS p
	) AS T

Open in new window

{edit for typos - sorry}
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:Shawn
ID: 39210780
PortletPaul:

I'm getting the same error as before:
The multi-part identifier "T.TP" could not be bound.

looking into group by
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39211707
Did you try the post# 39210117 by pkwan?
0
 
LVL 1

Author Comment

by:Shawn
ID: 39211808
yes, I got an error
Msg 319, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Msg 156, Level 15, State 1, Line 23
Incorrect syntax near the keyword 'UPDATE'.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39211982
try this.
;WITH CTE 
     AS (SELECT SUM(TotalPrice)                       AS TP, 
                SUM(TotalGST)                         AS TG, 
                SUM(TotalPST)                         AS TP, 
                SUM(TotalPrice + TotalGST + TotalPST) AS TotalIncludingTaxes 
           FROM (SELECT ROUND(( ( p.ProductPrice - ProductPrice * VolumeDiscount ) - ( ( p.ProductPrice - p.ProductPrice * VolumeDiscount ) * PaymentDiscount ) ) * Quantity, 2)       AS TotalPrice,
                        ROUND(( ( p.ProductPrice - ProductPrice * VolumeDiscount ) - ( ( p.ProductPrice - p.ProductPrice * VolumeDiscount ) * PaymentDiscount ) ) * Quantity * GST, 2) AS TotalGST,
                        ROUND(( ( p.ProductPrice - ProductPrice * VolumeDiscount ) - ( ( p.ProductPrice - p.ProductPrice * VolumeDiscount ) * PaymentDiscount ) ) * Quantity * PST, 2) AS TotalPST
                   FROM (SELECT po.PaymentDiscount, 
                                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.TaxCategoryID, 
                                tc.GST, 
                                tc.PST 
                           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 = @OrderID )) AS p) AS T) 
UPDATE T1 
   SET TotalPrice = T.TP, 
       ToatalGST = T.TG, 
       TotalPST = T.TP, 
       TotalIncludingTax = T.TotalIncludingTaxes 
  FROM WW.ClientOrders T1 
       CROSS JOIN CTE T 
 WHERE T1.OrderID = @OrderID 

Open in new window

0
 
LVL 1

Author Comment

by:Shawn
ID: 39212031
nice! thank you. that did it
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

792 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