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.
| Author |
Topic |
|
MevaD
Starting Member
32 Posts |
Posted - 2009-02-27 : 09:55:03
|
| HiI am trying to figure out if this is possible:I have two tables where I'm counting records from one table that were added during a certain time period but need to omit records if a record is found in another table with a date in the same time period.Example:========TableA--------IDOpenDateLocationother fields...========TableB--------IDOpenDateother fields...SELECT Count(*) AS Total, TableA.Locaiton FROM TableAWHERE OpenDate >= '01/01/2008' AND OpenDate <= '12/31/2008'AND Record NOT found in TableB with an OpenDate during the same time period.GROUP BY TableA.LocationAny ideas?Thanks!!-MD |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2009-02-27 : 10:18:34
|
does this do what you're after.....?declare @A table (id int, opendate datetime,location varchar(20))insert into @Avalues (1,'20080201','location1') ,(2,'20080501','location2') ,(3,'20090101','location3')declare @B table (id int, opendate datetime)insert into @Bvalues (1,'20080601') ,(2,'20090501') ,(3,'20090101') select location,COUNT(*)from @A a left join @b b on a.id = b.id and b.opendate between '20080101' and '20081231'where a.opendate between '20080101' and '20081231'and b.id is nullgroup by location Em |
 |
|
|
MevaD
Starting Member
32 Posts |
Posted - 2009-02-27 : 10:30:40
|
| elancaster,That's it. I wasn't sure how to add the OpenDate condition in the JOIN.Thanks!!-MD |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-02 : 01:24:26
|
| hi mevad,try this one alsoselect location,COUNT(*) from @a a where not exists (select * from @b where id = a.id and opendate between '20080101' and '20081231')and a.opendate between '20080101' and '20081231'group by locationselect location,COUNT(*) from @a a where id not in (select id from @b where id = a.id and opendate between '20080101' and '20081231')and a.opendate between '20080101' and '20081231'group by location |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-03-02 : 02:50:13
|
quote: Originally posted by elancaster does this do what you're after.....?declare @A table (id int, opendate datetime,location varchar(20))insert into @Avalues (1,'20080201','location1') ,(2,'20080501','location2') ,(3,'20090101','location3')declare @B table (id int, opendate datetime)insert into @Bvalues (1,'20080601') ,(2,'20090501') ,(3,'20090101') select location,COUNT(*)from @A a left join @b b on a.id = b.id and b.opendate between '20080101' and '20081231'where a.opendate between '20080101' and '20081231'and b.id is nullgroup by location Em
Wont work where version<2008 MadhivananFailing to plan is Planning to fail |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2009-03-02 : 03:19:17
|
well.... the solution will, just the setting up of test data that won't Em |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-03-02 : 05:06:14
|
quote: Originally posted by elancaster well.... the solution will, just the setting up of test data that won't Em
I actually meant it MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|