NancyJ
08-12-2005, 12:12 PM
Is it possible to merge rows in SQL?
eg. I have rows that contain a product ID, sessionID, Colour, Size and quantity.
Is it possible in SQL to merge the rows so that if a row contains the same product ID, session ID, colour and size then the rows would be merge adding the quantity fields?
I'm using this to display the merged rows but is there a way to modify this to permanently merge the rows... the reason I want to do this is I want to be able to modify the quantities eg. the form shows 3 as the quantity but that might be made up of 2 rows of a 2 and a 1 so I would only want to update one of the rows with that productID not both.
SELECT DISTINCT
Store_tblCart.fldProductID, Store_tblCart.fldSize,
Store_tblCart.fldColour, Store_tblCart.fldPersonalisation,
SUM(Store_tblCart.fldQuantity) AS fldQuantity,
Store_tblProducts.fldTitle, Store_tblProducts.fldPrice,
SUM(Store_tblProducts.fldPrice * fldQuantity)
AS Subtotal
FROM Store_tblCart INNER JOIN
Store_tblProducts ON
Store_tblCart.fldProductID = Store_tblProducts.fldProductID
WHERE Store_tblCart.fldSessionID = '993063358'
GROUP BY Store_tblCart.fldProductID, Store_tblCart.fldSize,
Store_tblCart.fldColour, Store_tblCart.fldPersonalisation,
Store_tblProducts.fldTitle, Store_tblProducts.fldPrice
so my question is basically, is there a way to update only 1 row that matches the criteria or merge the rows so that I can use the ID field to update them?
eg. I have rows that contain a product ID, sessionID, Colour, Size and quantity.
Is it possible in SQL to merge the rows so that if a row contains the same product ID, session ID, colour and size then the rows would be merge adding the quantity fields?
I'm using this to display the merged rows but is there a way to modify this to permanently merge the rows... the reason I want to do this is I want to be able to modify the quantities eg. the form shows 3 as the quantity but that might be made up of 2 rows of a 2 and a 1 so I would only want to update one of the rows with that productID not both.
SELECT DISTINCT
Store_tblCart.fldProductID, Store_tblCart.fldSize,
Store_tblCart.fldColour, Store_tblCart.fldPersonalisation,
SUM(Store_tblCart.fldQuantity) AS fldQuantity,
Store_tblProducts.fldTitle, Store_tblProducts.fldPrice,
SUM(Store_tblProducts.fldPrice * fldQuantity)
AS Subtotal
FROM Store_tblCart INNER JOIN
Store_tblProducts ON
Store_tblCart.fldProductID = Store_tblProducts.fldProductID
WHERE Store_tblCart.fldSessionID = '993063358'
GROUP BY Store_tblCart.fldProductID, Store_tblCart.fldSize,
Store_tblCart.fldColour, Store_tblCart.fldPersonalisation,
Store_tblProducts.fldTitle, Store_tblProducts.fldPrice
so my question is basically, is there a way to update only 1 row that matches the criteria or merge the rows so that I can use the ID field to update them?