| Author |
Topic |
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 09:32:55
|
| Greetings, I need help with a fundamental concept that I just can't figure out. I have two tables, IP and IPFiling. Any single IP can have one or more IPFilings records associated with it. (IPID is the PK for IP, and IPID is FK to IPFiling.) I want to write a SELECT or SELECT COUNT(*) statement that looks for any IP record where there are one or more IPFilings associated with it. This statement doesn't cut it, because I'm returning duplicates:select count(*)from ipjoin ipfiling on ip.ipid = ipfiling.ipidResults include the following "duplication" for IPID 12:IPID - IPFilingID11 - 19512 - 23312 - 23813 - 389Can someone help? I need help figuring out the logic of what I'm looking for. Thanks,Jennifer |
|
|
heavymind
Posting Yak Master
115 Posts |
Posted - 2009-02-02 : 09:36:53
|
| really...what are you looking for Jeni?select count(*) simply returns the count of records which might have been returned by you query. I bet select count(*)from ipjoin ipfiling on ip.ipid = ipfiling.ipidandselect count(*)ipfilingreturn the same number...Thanks, VadymMCITP DBA 2005/2008Chief DBA at http://www.db-staff.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 09:38:31
|
| [code]SELECT ip.IPID,COUNT(DISTINCT ipfiling.IPFilingID)FROM from ipjoin ipfiling on ip.ipid = ipfiling.ipidGROUP BY ip.IPID[/code] |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 09:42:25
|
| Vadym, Thanks for the quick response and right you are, my bad for not seeing that. However, that doesn't give me the number I need. There are 782 IPFiling records but they do not correlate to exactly 782 IP records. Each IP record is tied to one or more IPFiling records. In fact, most IP records have two IPFiling records associated with them. So I know that the number of IP records that have *one or more* IPFiling records is going to be less than 782. Does that help? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 09:44:41
|
| did you try my suggestion? |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 09:47:50
|
| Yes, and it works brilliantly (633 records). Now I'm trying to figure out how to integrate that concept into a more complex select statement. I've never used "group by" before. It seems handy. :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 09:51:06
|
| welcome..lemme know if you need any more help. |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 10:08:42
|
| "Group by" is cool and gives me the number I need, but it's not easy to integrate the Group By concept into a more complex select statement. The next step for me is to use this data to limit the number of results in another select statement. For this, I think I may need to use a different feature? So, start with the IP table. Find any IP that has one or more associated IPFiling records. Then, of those IPFiling records, sort by date and return only the oldest record. What do you think? Do-able? |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 10:10:35
|
quote: Originally posted by visakh16 welcome..lemme know if you need any more help.
Oooh, I just noticed that you're a "Flowing Fount of Yak Knowledge." How lucky for me! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 10:19:44
|
quote: Originally posted by JeniQ "Group by" is cool and gives me the number I need, but it's not easy to integrate the Group By concept into a more complex select statement. The next step for me is to use this data to limit the number of results in another select statement. For this, I think I may need to use a different feature? So, start with the IP table. Find any IP that has one or more associated IPFiling records. Then, of those IPFiling records, sort by date and return only the oldest record. What do you think? Do-able?
ok. try thisSELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.Datefield) AS Seq,ip.IPID,ipfiling.*FROM from ipjoin ipfiling on ip.ipid = ipfiling.ipid)tWHERE Seq=1 |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 10:39:04
|
| Running SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,ip.IPID,ipfiling.*FROM ipjoin ipfiling on ip.ipid = ipfiling.ipid)tWHERE Seq=1gives me the following error: Msg 8156, Level 16, State 1, Line 1The column 'IPID' was specified multiple times for 't'.How can I learn more about the syntax you're using so I can understand where the problem is? Thanks,Jennifer |
 |
|
|
heavymind
Posting Yak Master
115 Posts |
Posted - 2009-02-02 : 10:47:21
|
| the problem seems to be RunningSELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,ip.IPID as ipipid [b]/*here*/[b],ipfiling.*FROM ipjoin ipfiling on ip.ipid = ipfiling.ipid)tWHERE Seq=1Thanks, VadymMCITP DBA 2005/2008Chief DBA at http://www.db-staff.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 10:50:40
|
quote: Originally posted by JeniQ Running SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,ip.IPID,ipfiling.<all except IPID column>FROM ipjoin ipfiling on ip.ipid = ipfiling.ipid)tWHERE Seq=1gives me the following error: Msg 8156, Level 16, State 1, Line 1The column 'IPID' was specified multiple times for 't'.How can I learn more about the syntax you're using so I can understand where the problem is? Thanks,Jennifer
replace * with actual column names in query.also dont repeat IPID field from other table |
 |
|
|
JeniQ
Starting Member
29 Posts |
Posted - 2009-02-02 : 11:01:31
|
| We are cooking with gas now, fellas! I'm sorry I didn't understand your implied <your stuff goes here> notes, but now I do. So I am able to run this successfully, and it works like a charm. I still don't quite understand what I'm doing, so I hope to learn more about the syntax so I can learn from this experience. Here's what I'm going to read up on:- Partition by- SequenceI don't quite get the "t" part. Was I supposed to put my table name there? Is that just an assigned variable? If you can point me to any info about that or explain further, I would appreciate it. I have a meeting to run to but after that I'll see if I can make this statement work with my existing code. Thanks very much, all of you!Working Code:SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,ip.ipid, ipfiling.ipfilingbusid, ipfiling.filedateFROM ipjoin ipfiling on ip.ipid = ipfiling.ipidand ipfiling.filedate > '2007-01-01 00:00:00')tWHERE Seq=1order by 4 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-02 : 11:51:27
|
quote: Originally posted by JeniQ We are cooking with gas now, fellas! I'm sorry I didn't understand your implied <your stuff goes here> notes, but now I do. So I am able to run this successfully, and it works like a charm. I still don't quite understand what I'm doing, so I hope to learn more about the syntax so I can learn from this experience. Here's what I'm going to read up on:- Partition by- SequenceI don't quite get the "t" part. Was I supposed to put my table name there? Is that just an assigned variable? If you can point me to any info about that or explain further, I would appreciate it. I have a meeting to run to but after that I'll see if I can make this statement work with my existing code. Thanks very much, all of you!Working Code:SELECT *FROM(SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,ip.ipid, ipfiling.ipfilingbusid, ipfiling.filedateFROM ipjoin ipfiling on ip.ipid = ipfiling.ipidand ipfiling.filedate > '2007-01-01 00:00:00')tWHERE Seq=1order by 4
t is just an alias for the derived table i.e just another name for created derived table (internal query) |
 |
|
|
|