0

I have a table with field1, field2, field3, … and I need to count the number of items in field1 such that I return all records(field1,filed2,field3,…) that occur 6 times or less in the table.

My SQL code is:

SELECT field1, field2, field3, count(field1) CNT
FROM myTable
WHERE trunc(date)  = tp_date(‘03/22/2011’,’mm/dd/yyyy’)
GROUP BY field1
HAVING COUNT(field1)  <  7;

The output that I am getting from the above code is all records are returned from the table not what I expected? Any help would be appreciated!!

6
  • 2
    What do you mean by "number of items in field1?" Could you please post some sample data and desired output? Commented Mar 22, 2011 at 16:58
  • I need to count the contents of field1 and if there or 6 or less I need to return those record for the table. Commented Mar 22, 2011 at 17:06
  • 2
    @John: sorry, I don't understand what does "count the contents of field1" mean. It would be really easier if you posted sample data. Commented Mar 22, 2011 at 17:08
  • @John: just a misambiguation: If field1 appears in 7 or more rows but trunc(date)=tp_date(‘03/22/2011’,’mm/dd/yyyy’) is true for less than 7 of them, do you want these (less than 7 rows) to appear or not? Commented Mar 23, 2011 at 0:32
  • Also: if field1 appears in less than 7 rows but trunc(date)=tp_date(‘03/22/2011’,’mm/dd/yyyy’) is true for a few of them, do you want all of them to appear or not? I guess no in this case. Commented Mar 23, 2011 at 0:34

3 Answers 3

1

I think you need to use a subquery:

SELECT field1, field2, field3, 
FROM myTable
WHERE trunc(date)  = tp_date(‘03/22/2011’,’mm/dd/yyyy’)
AND field1 in 
   (SELECT field1
    FROM mytable
    GROUP BY field1
    HAVING COUNT(field1)  <  7); 
Sign up to request clarification or add additional context in comments.

4 Comments

Lost in Alabama, You code is spot on!! This solved my issue and now I know to use subquery, very powerful. P.S. I am originally from Alabama but now Lost in California! Thank John
You're welcome. Subqueries certainly can give you many more options for solving sql problems. Please mark the answer as accepted.
P.S. Wish I lived in California too!
Sorry but this is the first time I used Stackoverflow and I do not see where to mark the answer as accepted?
1
WITH tmp AS
(
    SELECT field1, COUNT(1) as CountOfField1
    FROM myTable
    WHERE trunc(date) = tp_date(‘03/22/2011’,’mm/dd/yyyy’)
    GROUP BY field1
    HAVING COUNT(field1) < 7
)
SELECT  mytable.field1, mytable.field2, mytable.field3, tmp.CountOfField1
FROM    myTable 
        INNER JOIN tmp 
            ON myTable.Field1 = tmp.Field1

2 Comments

Clyc, Thanks you for your example! I will code this up to see how this works!
You don't need the <7 check twice.
0

Yet another way to do it:

SELECT t.field1, t.field2, t.field3, 
FROM myTable t
WHERE trunc(t.date) = tp_date(‘03/22/2011’,’mm/dd/yyyy’)
  AND EXISTS
  ( SELECT *
    FROM mytable t2
    WHERE t2.field1 = t.field1
      AND trunc(t2.date) = tp_date(‘03/22/2011’,’mm/dd/yyyy’)
    GROUP BY t2.field1
    HAVING COUNT(t2.field1)  <  7
  )
; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.