| Author |
Topic |
|
pithed2
Starting Member
3 Posts |
Posted - 2006-07-18 : 22:38:01
|
| I'm working with a table that's keyed by Member# and a sequencing number within that member#. A user wants a report that is sorted by the sequence #1's last name and first name, but then is followed by that seq #1's following seq #'s under the Member#.For example:Last Name = SmithFirst Name = AlexMbr# = 10Seq# = 01Smith, Alex, 10, 01 Then under Alex Smith would be his wife and children (who may not have the same last name as him).Smith, Jenny, 10, 02Jones, Jimmy, 10, 03Smith, Julie, 10, 04Next alphabetic Seq# 1Smith, Brian, 25, 01Smith, Lucy, 25, 02Williams, Gina, 25, 03Williams, Gordon, 25, 04Next alphabetic Seq# 1Smith, Bryana, 18, 01Johnson, John, 18, 02Smith, Jorge, 18, 03I can do this fairly easily with multiple queries, but due to the size of the database and that it's a web query using ODBC, I need to try to make into one SQL statement if possible.I would assume I could create a sub query that first pulls all the Seq#1's ordered by Last and First, but can I then tie that back to the same table to by a join to get the seq#02 and up??Thanks for any help ahead of time.Andy |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2006-07-18 : 22:42:00
|
| Can you post some more data so we can get a clearer view of what you're playing with?Cheers,Tim |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-18 : 23:08:30
|
this ?select *from tableorder by Mbr#, Seq# Maybe you should post the table structure, some sample data and the result that you want. KH |
 |
|
|
pithed2
Starting Member
3 Posts |
Posted - 2006-07-18 : 23:27:49
|
| Sorry. Here's a quick, short form. The table is not normalized and resides on an AS400. So I'm only going to give a few fields from it.Table: Member_MasterFields: Member# (11 int), Seq#(2 int), First_Name(15 string), Last_Name (25 string)Keyed by Member#, Seq# uniquelyThe data would look like this:10, 01, Alex, Smith10, 02, Jenny, Smith10, 03, Jimmy, Jones10, 04, Julie, Smith18, 01, Bryana, Smith18, 02, John, Johnson18, 03, Jorge, Smith25, 01, Brian, Smith25, 02, Lucy, Smith25, 03, Gina, Williams25, 04, Gordon, WilliamsI'm probably not on the same syntax as most of you as I work on an AS400 primarily. Anyway, I need to sort all these members by last name, then first, however, I need to have the seq# > 1, return under the seq# 01 in seq# order, rather than alphabetical.So I need to receive this, from that data:Smith, Alex, 10, 01Smith, Jenny, 10, 02Jones, Jimmy, 10, 03Smith, Julie, 10, 04Smith, Brian, 25, 01Smith, Lucy, 25, 02Williams, Gina, 25, 03Williams, Gordon, 25, 04Smith, Bryana, 18, 01Johnson, John, 18, 02Smith, Jorge, 18, 03The order being:Select *From Member_MasterWhere Seq# = 01Order by Last_Name, First NameBut then I need to take each of those and join them through an inner join?? back to Member_Master to get the seq# > 1 ordered by seq#.Is that possible??On the 400, I would just read through the Member Master file sorted by last name, first name for seq# = 01. Once I found a member, I would then loop back through the file, sorted by member# and seq# and get all the seq#'s in order. However, I have to do this for a php script and can't quite do that. The table is very large (5 mil records), so I have to find a way to make it timely on the internet. Once again, I apologize for jumping between lingo (table/records), but I'm much more used to the AS400 lingo. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-18 : 23:53:25
|
"The table is not normalized and resides on an AS400."This is a MS SQL SERVER forum site so we works primary on MS SQL. This works in MS SQL Server not sure about AS400.declare @member table( member_no int, seq_no int, first_name varchar(10), last_name varchar(10))insert into @memberselect 10, 01, 'Alex', 'Smith' union allselect 10, 02, 'Jenny', 'Smith' union allselect 10, 03, 'Jimmy', 'Jones' union allselect 10, 04, 'Julie', 'Smith' union allselect 18, 01, 'Bryana', 'Smith' union allselect 18, 02, 'John', 'Johnson' union allselect 18, 03, 'Jorge', 'Smith' union allselect 25, 01, 'Brian', 'Smith' union allselect 25, 02, 'Lucy', 'Smith' union allselect 25, 03, 'Gina', 'Williams' union allselect 25, 04, 'Gordon', 'Williams'select c.last_name, c.first_name, c.member_no, c.seq_nofrom @member p inner join @member con p.member_no = c.member_nowhere p.seq_no = 1order by p.last_name, p.first_name, c.seq_no/* RESULTlast_name first_name member_no seq_no ---------- ---------- ----------- ----------- Smith Alex 10 1Smith Jenny 10 2Jones Jimmy 10 3Smith Julie 10 4Smith Brian 25 1Smith Lucy 25 2Williams Gina 25 3Williams Gordon 25 4Smith Bryana 18 1Johnson John 18 2Smith Jorge 18 3(11 row(s) affected)*/ KH |
 |
|
|
pithed2
Starting Member
3 Posts |
Posted - 2006-07-19 : 10:07:34
|
| That worked perfectly. Thank you very much for your help!!Andy |
 |
|
|
|
|
|