| | |
Selecting one record to many records
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
Hi
I have 3 tables that I need to display in a report
TableA
idxA valueA
1, a
2, b
3, c
TableB
idxB bidxA valueB
1, 1, aa
2, 1, bb
3, 1, cc
4, 2, aa
5, 2, cc
TableC
idxC bidcA valueC
1, 1, dd
2, 1, ee
3, 2, ff
4, 2, gg
5, 2, hh
6, 2, ii
The resut I would like to display is
1 aa dd
1 bb ee
1 cc __
2 aa ff
2 cc gg
2 __ hh
2 __ ii
Does anyone have and idea and preferably an example on how to do something like this in SQL
Thanks
I have 3 tables that I need to display in a report
TableA
idxA valueA
1, a
2, b
3, c
TableB
idxB bidxA valueB
1, 1, aa
2, 1, bb
3, 1, cc
4, 2, aa
5, 2, cc
TableC
idxC bidcA valueC
1, 1, dd
2, 1, ee
3, 2, ff
4, 2, gg
5, 2, hh
6, 2, ii
The resut I would like to display is
1 aa dd
1 bb ee
1 cc __
2 aa ff
2 cc gg
2 __ hh
2 __ ii
Does anyone have and idea and preferably an example on how to do something like this in SQL
Thanks
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
not possible (or at least extremely difficult to do and to maintain) the way you have the database setup as there is no way (other than the order that they are entered in the database, which is rather unreliable) to tell how tables b and c are linked. However if you change your setup and have tableC look like this
idxC , bidcA ,bidcb ,valueC
then you can run your query like this:
which will give you a result that looks like
slightly different order, but that is unavoidable as nulls come first when sorting alphabetically
idxC , bidcA ,bidcb ,valueC
then you can run your query like this:
MS SQL Syntax (Toggle Plain Text)
SELECT idxA, valueb, valuec FROM tablea, tableb, tablec WHERE (idxa = bidxa AND idxa = bidca AND bidcb=idxb) union SELECT idxA, NULL AS valueb, valuec FROM tablea, tablec WHERE idxa = bidca AND bidcb IS NULL union SELECT idxA, valueb,NULL AS valuec FROM tablea, tableb WHERE idxa = bidxa AND idxb NOT IN (SELECT bidcb FROM tablec WHERE bidcb IS NOT NULL) ORDER BY idxA ASC,valueb ASC, valuec ASC
MS SQL Syntax (Toggle Plain Text)
idxA valueb valuec 1 aa dd 1 bb ee 1 cc 2 hh 2 ii 2 aa ff 2 cc gg
slightly different order, but that is unavoidable as nulls come first when sorting alphabetically
![]() |
Similar Threads
- Duplicate record and child records (MS Access and FileMaker Pro)
- please anybody to help Dropdownlist selected index problem (ASP.NET)
- move previouse record in VB.net (VB.NET)
- assignment help [fstream, on modifying a record in a file] (C++)
- Help with a reservation program! GUI Messed XD (Java)
- Trouble Reading Records (C++)
- need to update each records after record deleted (PHP)
- Deleting a record from a file (C)
- Code not working for deleting recordfrom a file (Visual Basic 4 / 5 / 6)
Other Threads in the MS SQL Forum
- Previous Thread: SQL Golf Handicap
- Next Thread: SQLServer 2000 Scheduled Job recurring every second
| Thread Tools | Search this Thread |





