tags:

views:

33

answers:

2

Hello,

Imagine a simple query:

SELECT name, lastname FROM people

Let's imagine it returns 6 rows. What i need to do is to return a number of rows that ROWS Mod 4 would be equal to 0. So if the query would normally return 6 rows, it should return 2 more rows with NULL for name and lastname. It's easy to count the rows it would return and generate the necessary union selects, but i'm wondering if it's possible to do in sql. I know, sounds kinda stupid and you're probably thinking about telling me to find a different solution, but that's the easiest workaround for my problem ;]

A: 

That's impossible to do in SQL. Because null will highly likely never be equal to name or lastname.

Kevin
the issue isn't really to find where name or last name are null, but to add additional empty rows to a recordset. Michael's solution will work even if name or last name are required fields and never null.
sql_mommy
@sql_mommy - Thanks for clearing that up (:
Kevin
+2  A: 

for oracle:

SELECT name, lastname FROM people
UNION ALL
SELECT NULL, NULL FROM people
WHERE RowNum <= (Select mod(Count(*),4) from people)

for mssql: (>=2005)

SELECT name, lastname FROM people
UNION ALL
select null, null from
(
  SELECT row_number() over(order by name) r FROM people
)
WHERE r <= (Select Count(*) % 4 from people)
Michael Pakhantsov
Unfortunately, my mssql gives an error of incorrect syntax near 'where'. Anyway, i've just looked at my query and it's too complex for me to turn it into this. It'll become unreadable with all those joins and where statements. I guess i'll just add the necessary unions to sql programmatically. Thanks for the help!
Marius S.