Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mtannir50
Starting Member
6 Posts |
Posted - 2010-03-15 : 10:45:23
|
| I have 2 tables #Products and #PrICE:CREATE TABLE #Products(Product_Name VARCHAR(20),Qty INT,Price DECIMAL(10,2))CREATE TABLE #PrICE(Product_Name VARCHAR(20),Pr DECIMAL(10,2))INSERT INTO #PrICE SELECT 'Prod1',2INSERT INTO #PrICE SELECT 'Prod2',4INSERT INTO #Products SELECT 'Prod1',2,2.5UNIONSELECT 'Prod2',3,6.5UNIONSELECT 'Prod1',3,2.5I want to update only the first row (or one row for each product)in the #Products table for each product name.The following command updates all the records. Update #ProductsSET Price = PRFROM (#Products INNER JOIN #PRICE ON #Products.Product_Name = #PrICE.Product_Name )Help please. |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-15 : 11:04:48
|
quote: Originally posted by mtannir50 I have 2 tables #Products and #PrICE:CREATE TABLE #Products(Product_Name VARCHAR(20),Qty INT,Price DECIMAL(10,2))CREATE TABLE #PrICE(Product_Name VARCHAR(20),Pr DECIMAL(10,2))INSERT INTO #PrICE SELECT 'Prod1',2INSERT INTO #PrICE SELECT 'Prod2',4INSERT INTO #Products SELECT 'Prod1',2,2.5UNIONSELECT 'Prod2',3,6.5UNIONSELECT 'Prod1',3,2.5I want to update only the first row (or one row for each product)in the #Products table for each product name.The following command updates all the records. Update #ProductsSET Price = PRFROM (#Products INNER JOIN #PRICE ON #Products.Product_Name = #PrICE.Product_Name )Help please.
i wasn't getting you proprly,pl show the expected output. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-15 : 11:38:20
|
| [code]UPDATE pSET p.Price = pr.PrFROM(SELECT ROW_NUMBER() OVER(PARTITION BY Product_Name ORDER BY Qty) AS Seq,Price,Product_Name FROM #Products)pJOIN #Price prON pr.Product_Name = p.Product_NameAND p.Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mtannir50
Starting Member
6 Posts |
Posted - 2010-03-16 : 02:24:41
|
| Thank you visakh16. It worked out fine. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 03:57:51
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mtannir50
Starting Member
6 Posts |
Posted - 2010-03-16 : 07:28:41
|
| Things got a little more complicated. I want to include a case when clause with the sum function in the Set statement as follows (disregard the field names): UPDATE pSET p.Price =(CASE WHEN SRND01 <> 0 AND Month('2010.01.28') = 1 THEN SUM(SRBP01)/SUM(SRND01)*C8SPT/C8PWD END )FROM(SELECT ROW_NUMBER() OVER(PARTITION BY Product_Name ORDER BY Qty) AS Seq,Price,Product_Name FROM #Products)pJOIN #Price prON pr.Product_Name = p.Product_NameAND p.Seq=1But I am having an error when trying to run it. Any help please? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 07:37:04
|
quote: Originally posted by mtannir50 Things got a little more complicated. I want to include a case when clause with the sum function in the Set statement as follows (disregard the field names): UPDATE pSET p.Price =(CASE WHEN SRND01 <> 0 AND Month('2010.01.28') = 1 THEN SUM(SRBP01)/SUM(SRND01)*C8SPT/C8PWD END )FROM(SELECT ROW_NUMBER() OVER(PARTITION BY Product_Name ORDER BY Qty) AS Seq,Price,Product_Name FROM #Products)pJOIN #Price prON pr.Product_Name = p.Product_NameAND p.Seq=1But I am having an error when trying to run it. Any help please?
where does SRBP01,etc come from?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|