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: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
Post #1386128
Posted Sunday, November 18, 2012 8:14 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:44 PM
Points: 1,945, Visits: 2,862
I have a CTE Query but I have no concept about it. And I am new in programming.


And you are doing it wrong. Rows are not records. Tables are not files. The goal of a database is to remove redundancy; you want to increase it. All wrong.

What you are doing is using SQL to print a deck of punch cards. You are not writing RDBMS! Yes, we can help you be a bad SQL programmer, if you really want to be a bad programmer. But why?

You saw a recursive CTE, and it looks like a loop so you want to use it. But SQL is a declarative language and we hate loops. What are you actually trying to do?


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1386131
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: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
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: Yesterday @ 11:12 PM
Points: 3,616, Visits: 5,230
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!
Post #1386172
Posted Monday, November 19, 2012 2:53 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 18, 2014 9:09 AM
Points: 870, Visits: 2,385
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