Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update Query Expand / Collapse
Author
Message
Posted Sunday, September 23, 2012 2:29 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 11:19 AM
Points: 245, Visits: 1,132
USE [tempdb]

CREATE TABLE [dbo].[hubCustomer](
[CustVid] [int] IDENTITY(1,1) NOT NULL,
[CustSourceGid] [nvarchar](30) NULL

)

INSERT INTO HUBCustomer SELECT '103' UNION ALL
SELECT '105' UNION ALL
SELECT '107'

CREATE TABLE [dbo].[hubProductSale](
[ProductSaleVid] [int] IDENTITY(1,1) NOT NULL,
[CustSourceGid] [nvarchar](30) NULL,
[ProductSourceGid] [nvarchar](100) NULL
)

INSERT INTO hubProductSale SELECT '103','001' UNION ALL
SELECT '105','007'UNION ALL
SELECT '107','008'UNION ALL
SELECT '109','006'UNION ALL
SELECT '104','007'

CREATE TABLE [dbo].[LnkCustProduct](
[LinkVid] [int] IDENTITY(1,1) NOT NULL,
[CustVid] [int] NULL,
[ProductSaleVid] [int] NULL
)

INSERT INTO LnkCustProduct SELECT '0','5' UNION ALL
SELECT '0','6' UNION ALL
SELECT '0','5' UNION ALL
SELECT '1','1' UNION ALL
SELECT '1','2' UNION ALL
SELECT '2','3'

CREATE TABLE [dbo].[SuspectRecords](
[SuspetRecordsID] [int] IDENTITY(1,1) NOT NULL,
[SourceParentGID] [nvarchar](100) NULL,

[SourceChildGid] [nvarchar](100) NULL
)
INSERT INTO SuspectRecords SELECT '104','007' UNION ALL
SELECT '109','006'
update LnkCustProduct 
set CustVid=
( case when LCP.CustVid =0 THEN hc.CustVid
else LCP.CustVid end )

FROM SuspectRecords SR inner join hubCustomer hc on SR.SourceParentGID=hc.CustSourceGid
INNER JOIN hubProductSale HPS ON SR.SourceChildGid=HPS.ProductSourceGid
INNER JOIN LnkCustProduct LCP ON HPS.ProductSaleVid=LCP.ProductSaleVid


HubCustomer and hubProductSale has parent child relationship.

linkCustProduct records the relationship between hubcustomer and hubProductSale.

Whenever there is a sale visible of a customer in LnkCustProduct table but the custID is not yet visible in hubCustomer the CustVid for those records in the linkTable (LnkCustProduct) is recroded as null converted to 0 and the CustSourceGid and ProductSourceGid of that record is recorded in a separate table called suspect records.

If the parentID of that record is visible later in time, I want to update the CustVid of that record in the linktable and remove it from SuspectTable.

In this example lets say, there was an insert of 104 in hubCustomer so that is no longer a suspect record. So I would like to update its corresponding CustVid into the link table from 0 and remove it from suspect record. My query is doesn’t quite do that. Please shed some light.



Post #1363297
Posted Sunday, September 23, 2012 11:51 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:01 PM
Points: 3,443, Visits: 5,402
Can you provide expected results in each of the 4 tables after the update you want to perform is applied.

I looked at your description and maybe my brain is just fuzzy this morning but I couldn't put the pieces together.

A MERGE may work for this but I'd need to see the expected results to make it clear to me.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1363339
Posted Monday, September 24, 2012 10:45 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 4, 2014 1:50 PM
Points: 339, Visits: 386
Marbe this helps.



UPDATE LnkCustProduct SET LnkCustProduct.CustVid = hc.CustVid
FROM SuspectRecords SR inner join hubCustomer hc on SR.SourceParentGID=hc.CustSourceGid
INNER JOIN hubProductSale HPS ON SR.SourceChildGid=HPS.ProductSourceGid AND
SR.SourceParentGID=HPS.CustSourceGid
INNER JOIN LnkCustProduct LCP ON HPS.ProductSaleVid=LCP.ProductSaleVid


DELETE SuspectRecords WHERE EXISTS(SELECT CustVid FROM HubCustomer HC WHERE SuspectRecords.SourceParentGID=hc.CustSourceGid )



Post #1363626
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse