Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Just a simple "do or don't" question.

I want to select some user data from my database and put it in an Excel (programmatically). So, I get something like SELECT a, b, c, ... (about 60 more columns) FROM Users ... couple of joins ... etc. Done.

Then I have that data in my application and then I need detail rows. Usually I'd just join, except that in this case I need the details as columns and I also don't want the data I previously got as double rows.
I may have over 1000 rows and selecting the details would either be over 1000 roundtrips or do something like SELECT data FROM Details WHERE Id IN (over 1000 values gotten from previous data).

So I thought I'd want my data something like this:
Id   SomeField   OtherField   etc.   DetailId   DetailField   etc.
1    a           b            ...    NULL       NULL          NULL
1    NULL        NULL         NULL   1          a             ...
1    NULL        NULL         NULL   2          b             ...
2    a           b            ...    NULL       NULL          NULL
2    NULL        NULL         NULL   3          a             ...
You get the point.

Looks something like this:
SQL
WITH CTEA
AS (
   SELECT u.Id AS Id
         ,A AS A
         ,B AS B
         -- 60 more columns
         ,NULL AS DetailId
         ,NULL AS DetailField
   FROM Users u
   --JOINS and WHERE
),
CTEB
AS (
   SELECT u.Id AS Id
         ,NULL AS A
         ,NULL AS B
         -- 60 more columns
         ,det.Id AS DetailId
         ,det.Field AS DetailField
   FROM Users u
   -- JOIN with detail
   WHERE u.Id IN (SELECT Id FROM CTEA)
)
SELECT * FROM CTEA
UNION ALL
SELECT * FROM CTEB
ORDER BY Id, DetailId
I guess it doesn't deserve a prize for beauty, but does it deserve a price for performance and efficiency?

I see no compelling reason NOT to do this, but I'm wondering what my peers think about it.
Thanks.
Posted
Comments
PIEBALDconsult 9-Feb-15 10:02am    
That's about how I'd do it.
Sander Rossel 9-Feb-15 14:39pm    
Nice! Post it as an answer so I can accept (you were the first to confirm, so I guess you deserve the points) :-)
Unless someone is going to point out issues after all, in which case I'd have to downvote you to oblivion ;-p
PIEBALDconsult 9-Feb-15 14:41pm    
Points schmoints.
Sander Rossel 9-Feb-15 14:56pm    
Ok, I was trying to play this nice and promise you shiny points.
Truth is I don't care about your rep, but I really like to close my questions. Not doing so feels like leaving open ends ;-)
Now I'd feel bad for selfishly giving you points :-p
Kornfeld Eliyahu Peter 9-Feb-15 10:12am    
I think that there is no nice way to flatten hierarchical data...For that I can't see no wrong in your SQL...
The performance is - of course - depend on the keys/indexes you have...

1 solution

Just closing this one. Guess it's a do :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900