Rx_
Nothing In Moderation
- Local time
- Yesterday, 21:46
- Joined
- Oct 22, 2009
- Messages
- 2,803
ID_Wells is the FK for this table.
I was a little surprised.
The ID_Wells grouped by and had the Count of how many HZ_Type_Num were in each ID_Well.
So, I picked one with a count of 5 and filtered the source table Wells_Lease_DirHz.
To my surprise, one of the 5 records didn't have the filter 1,4,5.
Found more cases of this. It was counting all the records if any of the other records had a 1,4,5.
For my purpose to test - Does this ID_Wells have at least 1 assocated Dir_hz with a 1,4,5? This will work.
But, I was surprised it counted all of them.
Can someone explain why?
Or can somone tell me a better way to filter a list of 0 to many ID_Wells that have at least one related Dir_Hz with a 1,4,5
B.T.W. the exact same query on MS SQL Server returned the same thing.
OK, I think a very dim light came on.
After looking at the results, even the ID_Wells with a count of 1 had something other than 1,4,5 (in above sample)
By using the WHERE clause - the group-by seems to only return the ID_Wells I needed.
SELECT ID_Wells, COUNT(HZ_Type_Num) AS Expr1
FROM dbo.Wells_Lease_DirHz
WHERE (HZ_Type_Num IN (1, 4, 5))
GROUP BY ID_Wells
ORDER BY ID_Wells
Going one step beyond in SQL - this returned the proper counts! OK, this is what a 11 1/2 hour day does to you.
Mom, don't let your kids grow up to be programmers.
SELECT TOP (100) PERCENT ID_Wells, COUNT(HZ_Type_Num) AS Expr1
FROM dbo.Wells_Lease_DirHz
WHERE (HZ_Type_Num IN (1, 4, 5))
GROUP BY ID_Wells
ORDER BY ID_Wells
I was a little surprised.
The ID_Wells grouped by and had the Count of how many HZ_Type_Num were in each ID_Well.
So, I picked one with a count of 5 and filtered the source table Wells_Lease_DirHz.
To my surprise, one of the 5 records didn't have the filter 1,4,5.
Found more cases of this. It was counting all the records if any of the other records had a 1,4,5.
For my purpose to test - Does this ID_Wells have at least 1 assocated Dir_hz with a 1,4,5? This will work.
But, I was surprised it counted all of them.
Can someone explain why?
Or can somone tell me a better way to filter a list of 0 to many ID_Wells that have at least one related Dir_Hz with a 1,4,5
Code:
SELECT Wells_Lease_DirHz.ID_Wells, Count(Wells_Lease_DirHz.HZ_Type_Num) AS CountOfHZ_Type_Num
FROM Wells_Lease_DirHz
GROUP BY Wells_Lease_DirHz.ID_Wells
HAVING (((Count(Wells_Lease_DirHz.HZ_Type_Num)) In (1,4,5)))
ORDER BY Wells_Lease_DirHz.ID_Wells;
B.T.W. the exact same query on MS SQL Server returned the same thing.
OK, I think a very dim light came on.
After looking at the results, even the ID_Wells with a count of 1 had something other than 1,4,5 (in above sample)
By using the WHERE clause - the group-by seems to only return the ID_Wells I needed.
SELECT ID_Wells, COUNT(HZ_Type_Num) AS Expr1
FROM dbo.Wells_Lease_DirHz
WHERE (HZ_Type_Num IN (1, 4, 5))
GROUP BY ID_Wells
ORDER BY ID_Wells
Going one step beyond in SQL - this returned the proper counts! OK, this is what a 11 1/2 hour day does to you.
Mom, don't let your kids grow up to be programmers.
SELECT TOP (100) PERCENT ID_Wells, COUNT(HZ_Type_Num) AS Expr1
FROM dbo.Wells_Lease_DirHz
WHERE (HZ_Type_Num IN (1, 4, 5))
GROUP BY ID_Wells
ORDER BY ID_Wells
Last edited: