Avatar of dprovencher
dprovencher

asked on 

Output grid-like table from many unlinked tables

Hi, I've been searching and thinking of a way to do this all day yesterday and I'm about to give up and do it in C#.  But before I do I'd really like to know if anyone can think of a way to do this in SQL.  What I want to do is make a query that returns rows based on other tables.  That's not clear, let's say I've got the following tables:

tbl1
a b c
d e f
g h i

tbl2
1 2 3
4 5 6
7 8 9
0 1 2

What I want is to get something like this:

a b c 1 2 3
d e f 4 5 6
g h i 7 8 9
NULL NULL NULL 0 1 2

I don't want to use the CREATE TABLE by the way, I hate the idea of temp tables, they should never be used :)

Thanks for any input!

I use SQL Server 2000.
Microsoft SQL ServerMicrosoft SQL Server 2005SQL

Avatar of undefined
Last Comment
dprovencher
Avatar of JCinDE
JCinDE

You generally need to have some sort of key to identify which row in tbl1 goes with which row in tbl2. (Unless you're looking for a matrix of the two, which is pretty simple to get with just "select * from tbl1, tbl2" but I don't get the sense that's what you want.)

I really need more information to go on. The question is pretty vague.

By the way: Never say never. Temp tables have their place
Avatar of dprovencher
dprovencher

ASKER

There is no common key in either table, the resulting records will be used in an horizontal report, that's why I need this.  Thanks for the reply :)
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dprovencher
dprovencher

ASKER

Damn thanks!  All I was missing was the ORDER BY COALESCE(#t1.ID, #t2.ID) all my records were mixed up :D

I'll post my solution without temp tables and give you the points.
Avatar of dprovencher
dprovencher

ASKER

Here's my code, first a UDF for each table (don't know how else to do this):

CREATE FUNCTION fct_tbl_cie_identity ()
RETURNS @Table TABLE  (NoCompagnie NVARCHAR(1024), Nom NVARCHAR(1024), RowIndex INT IDENTITY) AS
BEGIN
INSERT INTO @Table
SELECT NoCompagnie, Nom FROM tbl_cie
RETURN
END

And the actual query:

SELECT *
FROM
fct_tbl_cie_identity() cie FULL OUTER JOIN
fct_tbl_contrat_identity() contrat ON cie.RowIndex = contrat.RowIndex FULL OUTER JOIN
fct_tbl_chantier_identity() chantier ON contrat.RowIndex = chantier.RowIndex FULL OUTER JOIN
fct_tbl_activite_identity() activite ON chantier.RowIndex = activite.RowIndex
ORDER BY COALESCE(cie.RowIndex, contrat.RowIndex, chantier.RowIndex, activite.RowIndex)
dprovencher,

Nicely done :)  I would be curious to know if there were any difference in performance when comparing our
two approaches...

Regards,

Patrick
Avatar of dprovencher
dprovencher

ASKER

The tables are not big enough I don't think so, but I'd rather use memory instead of the hard disk :)
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo