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 |
|
uberman
Posting Yak Master
159 Posts |
Posted - 2004-09-01 : 04:41:26
|
I need to work out what gets shipped as part of a distribution of assets to hundreds of destinations. Each destination usually gets specific parcels, but on an ad hoc basis these parcels maybe bundled together for ease of shipping.This means I need to workout when to send the usual parcel and when to send a parcel that contains other parcels!I have put together a very simple example below, with the desired output at the bottom.I am banging my head with this one and think that some kind of intermedate table is required but just cant think of the next move, any help would be much appreciated.DECLARE @PARCELS TABLE( uidparcel INTEGER PRIMARY KEY, toplevel INTEGER, parcel VARCHAR(100))INSERT INTO @PARCELSSELECT 1,1, 'Big Box'UNIONSELECT 2,1,'Envelope A'UNIONSELECT 3,1,'Envelope B'UNIONSELECT 4,4,'Envelope C'--big box is a special case created just for this run, and it will contain--two envelopesDECLARE @DESTINATIONS TABLE(uiddestination INTEGER PRIMARY KEY,destination VARCHAR(10))INSERT INTO @DESTINATIONSSELECT 1,'A'UNIONSELECT 2,'B'UNIONSELECT 3,'C'UNION SELECT 4,'D'DECLARE @RECEIVES TABLE( uiddestination INTEGER, uidparcel INTEGER)INSERT INTO @RECEIVESSELECT 1,2UNIONSELECT 2,3UNION SELECT 3,2UNION SELECT 3,3UNIONSELECT 4,2UNIONSELECT 4,3UNIONSELECT 4,4SELECT D.destination, P.parcelFROM @DESTINATIONS DINNER JOIN @RECEIVES RON D.uiddestination = R.uiddestinationINNER JOIN @PARCELS PON P.uidparcel = R.uidparcel--this shows that destination C would normally get two envelopes sent--for this run I want destination C to get the big box--destinaction D needs the big box and envelope C--desired output/*A Envelope AB Envelope BC Big BoxD Big BoxD Envelope C*/ |
|
|
hgorijal
Constraint Violating Yak Guru
277 Posts |
Posted - 2004-09-01 : 06:21:51
|
| looks a bit crude... but see if it works..create view xyz as SELECT uiddestination, count(*) cntFROM RECEIVESgroup by uiddestination having count(*) > 1SELECT distinct D.destination, Isnull(P3.parcel, P.parcel)FROM DESTINATIONS DINNER JOIN RECEIVES R ON D.uiddestination = R.uiddestinationINNER JOIN PARCELS P ON P.uidparcel = R.uidparcelleft outer join xyz X on X.uiddestination = D.uiddestination left outer join RECEIVES R2 ON X.uiddestination = R2.uiddestinationleft outer join PARCELS P2 ON P2.uidparcel = R2.uidparcelleft outer join PARCELS P3 ON P2.toplevel = p3.uidparcelHemanth GorijalaBI Architect / DBA |
 |
|
|
uberman
Posting Yak Master
159 Posts |
Posted - 2004-09-01 : 07:03:21
|
I can see where you are going but it is not quite rightI put your suggestions into the demo script and it worked!BUT I then set it up so that Destination A gets Envelope A and Envelope C and the query now says destination A gets BIG BOX and Envelope C, which is not quite right. I can see why it is going wrong (the @view sees that destination A now has multiple destinations and the outer joins pick up big box as envelope a is in the big box), but I cant work out how to exclude it from the results, it sort of needs a conditional join that is giving me nightmaresDECLARE @PARCELS TABLE( uidparcel INTEGER PRIMARY KEY, toplevel INTEGER, parcel VARCHAR(100))INSERT INTO @PARCELSSELECT 1,1, 'Big Box'UNIONSELECT 2,1,'Envelope A'UNIONSELECT 3,1,'Envelope B'UNIONSELECT 4,4,'Envelope C'--big box is a special case created just for this run, and it will contain--two envelopesDECLARE @DESTINATIONS TABLE(uiddestination INTEGER PRIMARY KEY,destination VARCHAR(10))INSERT INTO @DESTINATIONSSELECT 1,'A'UNIONSELECT 2,'B'UNIONSELECT 3,'C'UNION SELECT 4,'D'DECLARE @RECEIVES TABLE( uiddestination INTEGER, uidparcel INTEGER)INSERT INTO @RECEIVESSELECT 1,2UNIONSELECT 1,4 -- destination A now gets envelope cUNIONSELECT 2,3UNION SELECT 3,2UNION SELECT 3,3UNIONSELECT 4,2UNIONSELECT 4,3UNIONSELECT 4,4SELECT D.destination, P.parcelFROM @DESTINATIONS DINNER JOIN @RECEIVES RON D.uiddestination = R.uiddestinationINNER JOIN @PARCELS PON P.uidparcel = R.uidparcel--this is the raw data without taking into account that some--items can be shipped in the BIG BOXDECLARE @VIEW TABLE( uiddestination INTEGER, cnt INTEGER)INSERT INTO @VIEWSELECT uiddestination, count(*)FROM @RECEIVESgroup by uiddestination having count(*) > 1SELECT DISTINCT D.destination, Isnull(P3.parcel, P.parcel) AS PARCELFROM @DESTINATIONS DINNER JOIN @RECEIVES RON D.uiddestination = R.uiddestinationINNER JOIN @PARCELS PON P.uidparcel = R.uidparcelleft outer join @VIEW Von V.uiddestination = D.uiddestination left outer join @RECEIVES R2ON V.uiddestination = R2.uiddestinationleft outer join @PARCELS P2ON P2.uidparcel = R2.uidparcelleft outer join @PARCELS P3ON P2.toplevel = p3.uidparcel--this works for destination C and D, but destination A is now showing a big box--when in fact it should just get the two envelopes--desired output/*A Envelope AA Envelope CB Envelope BC Big BoxD Big BoxD Envelope C*/ |
 |
|
|
hgorijal
Constraint Violating Yak Guru
277 Posts |
Posted - 2004-09-01 : 07:44:43
|
| OK.While inserting into @View add "where uidparcel <> 4"This will exclude anything with "Envelope C" from going into "Big Box"Hemanth GorijalaBI Architect / DBA |
 |
|
|
|
|
|
|
|