Author |
Topic |
dani2
Starting Member
32 Posts |
Posted - 2006-10-30 : 01:53:34
|
How can I select more rows than maximum rows in a table. There is no secondary table to join to.thank you |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 01:56:47
|
You can make a CROSS JOIN and alias the single table again, asSELECT *FROM MyTable aCROSS JOIN MyTable b Peter LarssonHelsingborg, Sweden |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:01:02
|
"How can I select more rows than maximum rows in a table"What are you trying to achieve ?"There is no secondary table to join to."Join with a derived number tableSELECT *FROM MyTable a cross join ( select num = 0 union all select num = 1 union all select num = 2 ) n KH |
|
|
dani2
Starting Member
32 Posts |
Posted - 2006-10-30 : 02:07:56
|
quote:
SELECT *FROM MyTable a cross join ( select num = 0 union all select num = 1 union all select num = 2 ) n
I have a table with a variable number of rows. I need a query that returns maximum number of rows everytime. Your query is perfect except I want empty rows added. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 02:11:16
|
Use UNIONSELECT Col1, Col2FROM MyTableUNION ALLSELECT NULL, NULL Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 02:11:47
|
I think you have to explain what ou are trying to acheive.Peter LarssonHelsingborg, Sweden |
|
|
dani2
Starting Member
32 Posts |
Posted - 2006-10-30 : 02:18:44
|
quote: Originally posted by Peso I think you have to explain what ou are trying to acheive.Peter LarssonHelsingborg, Sweden
Ok. Lets say there is a table with 2 cols [Name] varchar, [Value] DecimalThe table may have 1 to 5 rows. I need a query that returns 5 rows no matter how many rows are in the table. For example test 100test2 200null nullnull nullnull null if the table has only 2 rows |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:20:49
|
[code]select top 5 *from(select [Name], [Value]from [Table]union allselect NULL, NULLfrom ( select 1 as num union all select 2 as num union all select 3 as num union all select 4 as num union all select 5 ) n) a[/code] KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:21:30
|
But why do you need the constant number of rows to return ? KH |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 02:25:32
|
i don't think you need two levels of derived tables.select top 5 x.*from ( select num, name from [Tab2] union all select NULL, NULL union all select NULL, NULL union all select NULL, NULL union all select NULL, NULL union all select NULL, NULL ) x Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 02:26:03
|
I think it has to do with client side report generator.Peter LarssonHelsingborg, Sweden |
|
|
dani2
Starting Member
32 Posts |
Posted - 2006-10-30 : 02:27:32
|
quote: Originally posted by khtan But why do you need the constant number of rows to return ? KH
I have to make a report in a very clear specified format. better dont ask :)thanks a lot, its exactly what i want |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:28:43
|
>> i don't think you need two levels of derived tables.You are correct.Was originally intended to make a number table out of cross join and specify the max records in where condition. But since it is only 5 reocrds, decided to simply it a bit. KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:31:32
|
quote: Originally posted by dani2
quote: Originally posted by khtan But why do you need the constant number of rows to return ? KH
I have to make a report in a very clear specified format. better dont ask :)thanks a lot, its exactly what i want
OK. I won't Peter, you have foresignt ? KH |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 02:43:21
|
Comes with the territory Peter LarssonHelsingborg, Sweden |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 02:58:45
|
>> Comes with the territory KH |
|
|
|