|
I have a script that needs to join to 9 tables, they are all on outer joins as they don't all have matching data and some tables will bring back multiple rows. Ideally I would like my script to bring back only unique fields. To replicate my issue use the following: ** Edited to remove the insert statements *** What I would really like is: And I'm not sure how to do this, I'm using SQL 2000 otherwise I would have used ROWNUMBER function. Many thanks ---------- Excerpt from real data added: The tables join on FieldA, value 900075752 (for this example), and for this example TableA is the primary table to use and left outer join to the others. TableB for this value has: 3 5 7 8 10 11 14 TableC for this value has: 57, 80, 433 481 675 742 1207 When I run the script the data comes back giving all options for the three tables.
(comments are locked)
|
|
As a starter..... This pre-2005 solution creates a sequence number (or rank) by joining the table back onto itself and counting the number of rows with a value less than or equal to the current one. ---- Edit: going code-blind now... does this give the right results Includes a pre-2005 tally table to give something common for all the tables to join on, even if there are differing numbers of rows. This has worked to a point. I've tried it on the first couple of tables but it's stopped when one of the tables has hit the total of number of records in that table for that unique record, for example tableB as 14 records and tableC as 25 records, it's brought back 14 records. There are no duplicates though.
May 24 '11 at 07:23 AM
Mrs_Fatherjack
How where you going to use row_number()? Could you use this SQL2000 method instead?
May 24 '11 at 07:26 AM
Kev Riley ♦♦
I thought it would work so I'm trying to run it on a SQL 2005 box linked to the SQL 2000 server but currently struggling with that too. I'm not actually sure this is possible or whether I just suggest the user trawls through lots of data given the time frames available.
May 24 '11 at 07:28 AM
Mrs_Fatherjack
@mrs_fatherjack - the limit is caused by the JOIN in the bracketed SQL, I think changing those to outer joins would mean you would get flexible row counts from tables used
May 24 '11 at 07:30 AM
Fatherjack ♦♦
@Kev Riley It is hard to believe that just a few short years ago we all had to use the very triangular join everyone disliked but had to use because of the lack of the windowing functions in SQL Server pre-2005. Just remembering how to do it deserves more than +1 :)
May 24 '11 at 07:33 AM
Oleg
(comments are locked)
|
|
OK, one option (edit - based on original query - edit) would appear to be create a fourth temporary table and select from that. So change your Then use the following:
(comments are locked)
|
|
select field1,field2,count(*) as count into #temp from tablename groupby field1,field2 truncate table tablename select field1,field2 into tablename from #temp
(comments are locked)
|
|
select field1,field2,count(*) as count into #temp from tablename groupby field1,field2 truncate table tablename select field1,field2 into tablename from #temp
(comments are locked)
|


What's the value in FieldA for the 2nd and 3rd rows?
It's the same as in the first row.
So you want FieldA to autoincrement whenever fieldb = fieldc = 1? and for it to be blank otherwise?
Oh, and what are the ordering / sorting criteria?
I don't think I explained my script very clearly sorry. Basically I have 9 tables that join on 1 field, I've used the outer join as not all the tables will come back with any data. Because some of the tables have many rows for each unique field the data coming back is enormous and repeated and I want to try to just bring back each set of data once. I've added an excerpt from the actual data
I think you need to illustrate with different values as I'm getting confused as to how the tables join (or don't)