VBA SQL - Filter Group By Over entire Group

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pwag
    New Member
    • Feb 2014
    • 28

    #1

    VBA SQL - Filter Group By Over entire Group

    Good Morning Pro's,

    I am trying to write a script that selects a group based on the criteria within that group. I'm looking to pull in only records that meet that criteria.
    Code:
    SELECT ColumnA
    FROM Table
    GROUP BY ColumnA
    HAVING ColumnA = 'A'
    With the GROUP BY, SQL will pull in my GROUP even if there are two records with 'A' or 'B', rightfully so. I'm trying to only select the group when all Columns meet the criteria of 'A'. In otherwards, if there is a 'B' in the Group, I'd like to NOT SELECT it.

    Any creative solutions are welcome. Thanks guys!

    P
    Last edited by NeoPa; Apr 3 '14, 08:11 PM. Reason: Fixed SQL and tags
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Try something like this :
    Code:
    SELECT Min([ColumnA]) AS [MinColA]
    FROM   [Table]
    HAVING (Max([ColumnA])='A')

    Comment

    • pwag
      New Member
      • Feb 2014
      • 28

      #3
      Hi NeoPa,

      Thanks! Exactly what I needed. I used this(a variation of your example):
      Code:
      HAVING (Count(ColumnA))=(SELECT Count(ColumnA) FROM Table WHERE ID = ID);
      Last edited by NeoPa; Apr 3 '14, 11:15 PM. Reason: Trimmed some space

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        I must assume that the actual situation is a little harder to explain so the simpler expedient of comparing the Max() to a specific value (that I suggested) wouldn't work in real life. In that case you've come up with a clever work around.

        Typically, I wouldn't recommend using a subquery where it can be done another way but, as I say, I expect it is necessary in your case in spite of the simplified question.

        Congratulations on getting by with a simplified question too. So many that try end up causing more confusion than they avoid, but in this case it worked well.

        Comment

        Working...