Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Selecting/counting only one of multiple records

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 ip
join ipfiling on ip.ipid = ipfiling.ipid

Results include the following "duplication" for IPID 12:
IPID - IPFilingID
11 - 195
12 - 233
12 - 238
13 - 389

Can 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 ip
join ipfiling on ip.ipid = ipfiling.ipid
and
select count(*)
ipfiling
return the same number...

Thanks, Vadym
MCITP DBA 2005/2008
Chief DBA at http://www.db-staff.com
Go to Top of Page

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 ip
join ipfiling on ip.ipid = ipfiling.ipid
GROUP BY ip.IPID
[/code]
Go to Top of Page

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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-02 : 09:44:41
did you try my suggestion?
Go to Top of Page

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. :)
Go to Top of Page

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.
Go to Top of Page

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?
Go to Top of Page

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!
Go to Top of Page

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 this

SELECT *
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.Datefield) AS Seq,
ip.IPID,ipfiling.*
FROM from ip
join ipfiling on ip.ipid = ipfiling.ipid
)t
WHERE Seq=1
Go to Top of Page

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 ip
join ipfiling on ip.ipid = ipfiling.ipid
)t
WHERE Seq=1

gives me the following error:
Msg 8156, Level 16, State 1, Line 1
The 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
Go to Top of Page

heavymind
Posting Yak Master

115 Posts

Posted - 2009-02-02 : 10:47:21
the problem seems to be

Running
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY ip.IPID ORDER BY ipfiling.filedate) AS Seq,
ip.IPID as ipipid [b]/*here*/[b],ipfiling.*
FROM ip
join ipfiling on ip.ipid = ipfiling.ipid
)t
WHERE Seq=1

Thanks, Vadym
MCITP DBA 2005/2008
Chief DBA at http://www.db-staff.com
Go to Top of Page

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 ip
join ipfiling on ip.ipid = ipfiling.ipid
)t
WHERE Seq=1

gives me the following error:
Msg 8156, Level 16, State 1, Line 1
The 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
Go to Top of Page

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
- Sequence

I 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.filedate
FROM ip
join ipfiling on ip.ipid = ipfiling.ipid
and ipfiling.filedate > '2007-01-01 00:00:00'
)t
WHERE Seq=1
order by 4
Go to Top of Page

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
- Sequence

I 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.filedate
FROM ip
join ipfiling on ip.ipid = ipfiling.ipid
and ipfiling.filedate > '2007-01-01 00:00:00'
)t
WHERE Seq=1
order by 4



t is just an alias for the derived table i.e just another name for created derived table (internal query)
Go to Top of Page
   

- Advertisement -