November 29, 2010 at 2:35 pm
I have a project where I need to create multiple records out of the same data (exact duplicates) based on the value of a field in the query. For example, a customer has 3 items on an order. Each item has 4 qty ordered. I need my query to return a total of 12 records.
This will in turn produce 12 labels via a Crystal Report (yes, I am constrained to Crystal)
My Query is at the bottom, right beneath some sample code for some test data. I need the records to duplicate based on the value of dbo.SOP10200.QUANTITY.
I would like to avoid cursors due to their known inefficiencies, but I don't know of a better way. Thanks for the help!
----sample code-----
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable
CREATE TABLE #mytable (labeldata varchar(20), quantity numeric(5) )
INSERT INTO #mytable (labeldata, quantity)
SELECT 'labeldata1--item1',4 UNION ALL
SELECT 'labeldata1--item2',4 UNION ALL
SELECT 'labeldata1--item2',4 UNION ALL
-----actual query----
SELECT dbo.SOP10200.SOPNUMBE, dbo.SOP10200.ITEMNMBR, dbo.SOP10200.ITEMDESC, dbo.SOP60300.CUSTITEMNMBR, dbo.SOP60300.CUSTITEMDESC,
dbo.SOP60300.CUSTITEMSHORNAME, dbo.SOP60300.CUSTITEMGENERICDESC, dbo.SOP60300.USERDEF1, dbo.SOP60300.USERDEF2, dbo.SOP60300.USRDEF03,
dbo.SOP60300.USRDEF04, dbo.SOP60300.USRDEF05, dbo.SOP10100.CUSTNMBR, dbo.SOP10100.CUSTNAME, dbo.SOP10100.CSTPONBR,
dbo.SOP10100.CNTCPRSN, dbo.SOP10100.ShipToName, dbo.SOP10100.ADDRESS1, dbo.SOP10100.ADDRESS2, dbo.SOP10100.ADDRESS3, dbo.SOP10100.CITY,
dbo.SOP10100.STATE, dbo.SOP10100.ZIPCODE, dbo.SOP10100.COUNTRY, dbo.SOP10100.PHNUMBR1, dbo.SOP10200.QUANTITY
FROM dbo.SOP10200 INNER JOIN
dbo.SOP10100 ON dbo.SOP10200.SOPTYPE = dbo.SOP10100.SOPTYPE AND dbo.SOP10200.SOPNUMBE = dbo.SOP10100.SOPNUMBE LEFT OUTER JOIN
dbo.IV00101 ON dbo.SOP10200.ITEMNMBR = dbo.IV00101.ITEMNMBR LEFT OUTER JOIN
dbo.SOP60300 ON dbo.SOP10200.ITEMNMBR = dbo.SOP60300.ITEMNMBR AND dbo.SOP10100.CUSTNMBR = dbo.SOP60300.CUSTNMBR
December 3, 2010 at 5:31 am
Josh,
Can you give us DDL code for the other tables too? And it would help to know which of those tables in the query is the one that #mytable is standing in for.
December 3, 2010 at 6:38 am
Much simpler than you think - join to a tally table on dbo.SOP10200.QUANTITY.
What folks often forget about tally tables is that they are a source of rows as well as numbers.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 3, 2010 at 6:43 am
Chris Morris-439714 (12/3/2010)
What folks often forget about tally tables is that they are a source of rows as well as numbers.
I've actually used a Tally Table to do that too, but a conditional needs to be put on the join to keep it from oversizing the rows.
But does Quantity reflect a single unit or the 4 Qty that the OP was talking about? That's why I asked for DDL (should have also mentioned sample data), because doing it wrong can screw up inventory.
December 3, 2010 at 6:47 am
Brandie Tarvin (12/3/2010)
Chris Morris-439714 (12/3/2010)
What folks often forget about tally tables is that they are a source of rows as well as numbers.I've actually used a Tally Table to do that too, but a conditional needs to be put on the join to keep it from oversizing the rows.
But does Quantity reflect a single unit or the 4 Qty that the OP was talking about? That's why I asked for DDL (should have also mentioned sample data), because doing it wrong can screw up inventory.
Not sure yet, Brandie. The sample data is a little short. Good point though.
@josh: are you using a query tool to generate your SQL or are you writing it freehand?
Edit: blonde moment.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply