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

Get Records according to dataset. Expand / Collapse
Author
Message
Posted Sunday, November 18, 2012 7:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, November 18, 2012 11:47 PM
Points: 2, Visits: 6
Hello Expert !
I have a CTE Query but I have nothing concepts about it. And I am new in programming. Actually i have Qty column in my query and i want repeat rows until qty. means if qty is equal to 50 then data set returns me 50 time records. if qty is equal to 1000 thousand then query returns me 10000 thousand records. Following query works fine for me till <99 rcords how can i grow for 1000 rocords.
Thanx in advance.


SELECT * INTO #TableA
FROM
(

SELECT MIS_Request_Detail.vno, MIS_Request_Detail.IID, MIS_Request_Detail.Qty-MIS_Request_Detail.BalQty qty,
1 RcvdQty, pk,
ItmMstr.IName, ItmMstr.UName, '' SrNo,
MIS_Request_Detail.Qty As ReqQty, MIS_Request_Detail.BalQty, MIS_Request_Detail.Qty-MIS_Request_Detail.BalQty RemainQty
FROM MIS_Request_Detail INNER JOIN
view_MIS_Item_Master ItmMstr ON ItmMstr.IID=MIS_Request_Detail.IID
WHERE ReqType=1 AND MIS_Request_Detail.vno='00008/1012'

) T


;WITH Nbrs ( Number ) AS (
SELECT 1 UNION ALL
SELECT 1 + Number FROM Nbrs WHERE Number < 99
)

SELECT
A.vno,A.IID, A.IName, A.UName, CAST(SrNo AS VARCHAR(50)) SrNo, CAST(N.Number AS varchar(50)) QTY, pk ReqDetpk, RcvdQty,
A.ReqQty, A.BalQty, A.RemainQty, 0 Color, 0 chked, 0 Pk, 0 AS isupdate, '' AS isdeleted
FROM
#TableA A
JOIN
Nbrs N ON N.Number <= A.qty
ORDER BY IID, N.Number, pk



Post #1386126
Posted Sunday, November 18, 2012 7:28 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 @ 1:45 AM
Points: 3,972, Visits: 6,361
You can use a more efficient numbers table that returns numbers up to 1000:

;WITH Nbrs ( Number ) AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t1 (n1)
CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t2 (n2)
CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t3 (n3))





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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1386128
Posted Sunday, November 18, 2012 8:18 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 @ 1:45 AM
Points: 3,972, Visits: 6,361
CELKO (11/18/2012)
Yes, we can help you be a bad SQL programmer, if you really want to be a bad programmer. But why?



So you can be promoted to a management position of course!



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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1386133
Posted Sunday, November 18, 2012 11:57 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, November 18, 2012 11:47 PM
Points: 2, Visits: 6
Many many thanks SSCommitted.
You solved my problem.
Post #1386171
Posted Monday, November 19, 2012 12:00 AM


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 @ 1:45 AM
Points: 3,972, Visits: 6,361
abdul_rouf26 (11/18/2012)
Many many thanks SSCommitted.
You solved my problem.


While many have suggested that I should be committed, my board handle is actually Dwain.C!

You're welcome anyway.



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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1386172
Posted Monday, November 19, 2012 2:53 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 23, 2015 5:55 AM
Points: 1,042, Visits: 3,030
dwain.c (11/18/2012)
CELKO (11/18/2012)
Yes, we can help you be a bad SQL programmer, if you really want to be a bad programmer. But why?



So you can be promoted to a management position of course!


Classic reply, and so true.....


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1386250
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse