Get Records according to dataset.

  • 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

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Many many thanks SSCommitted.

    You solved my problem.:-)

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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!

    :hehe: Classic reply, and so true.....

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply