Results 1 to 11 of 11
  1. #1
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60

    query question

    Hi



    I have a table that looks like this:
    Store ---- Dept -----Item
    A ZZ Apple
    A ZZ Orange
    A YY Apple
    A KK Apple
    B ZZ Apple
    B WW Orange
    B WW Pear
    B WW Pear
    ..

    I am trying to write a query to return how many items (count) in each Store/Dept.
    So, for Atore A, DEPT ZZ, the Query should return 2
    For Store b, Dept WWW, the query should return 3

    I am stuck on getting the desired count. I grouped by Store and Dept but can't figure out the count part as mentioned above.


    Any SQL statement help or hint on how to write this would be greatly appreciated.

    thanks

  2. #2
    Parsonshere is offline Novice
    Windows 8 Access 2013
    Join Date
    Aug 2012
    Location
    Rusk
    Posts
    25
    Click image for larger version. 

Name:	CountExamp.PNG 
Views:	23 
Size:	1.4 KB 
ID:	11944
    SELECT tblItem.fldStore, tblItem.fldDept, Count(tblItem.fldItem) AS fldCnt
    FROM tblItem
    GROUP BY tblItem.fldStore, tblItem.fldDept;

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Or build a report using Grouping & Sorting with aggregate calc in group footer.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60
    Hi

    thanks for the reply. I appreciate it.

    I tried this approach
    SELECT tblItem.fldStore, tblItem.fldDept, Count(tblItem.fldItem) AS fldCnt
    FROM tblItem
    GROUP BY tblItem.fldStore, tblItem.fldDept;

    and it does return the count. I just need to tweek to get "Distinct" Item. So for this example

    Store ---- Dept -----Item
    A ZZ Apple
    A ZZ Orange
    A YY Apple
    A KK Apple
    B ZZ Apple
    B WW Orange
    B WW Pear
    B WW Pear

    If I query for Item in Store B, Dept WW, the query should return 2 item categories.
    B ww Orange
    and
    B ww Pear

    Thanks for the help.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Join the aggregate query to the table and use DISTINCT keyword or do another GROUP BY.

    Orange and Pear will show the same count value.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60
    Hi

    Thanks ..can you please provide an example sql statement setup.

    thanks

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Just use the query designer. Drag the table and the query into the query builder, set a join link on Store and Dept. Drag fields to the grid. Choose DISTINCT from the query properties dialog or select TOTALS from the ribbon. Basic Access query building.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    jscriptor09 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    60
    I did that already ... in a bit of different flavor. But I am looking for a single sql statement that I can stick in a VBA code. Here is what I did usinf the 2 steps approach:

    Query #1 : grouped by three fields
    Query #2 : used Query #1 and Did a Count on the item field.

    This provided the values right.

    However, I am looking for the single SQL statement construct that I can put in a VBA logic. It is a select from a select construct that I am not getting right.

    Thanks

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Now open queries in SQL View. Copy paste the SQL of Query1 into Query2 so the result is like:


    .... (sql for query 1 without semi-colon here) AS Query1 ...


    That's how I build nested queries.


    Show your SQL statements for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Parsonshere is offline Novice
    Windows 8 Access 2013
    Join Date
    Aug 2012
    Location
    Rusk
    Posts
    25
    Not sure I understand, but look like to me this:
    SELECT tblItem.fldStore, tblItem.fldDept, tblItem.fldItem, Count(tblItem.fldItem) AS fldCnt
    FROM tblItem
    GROUP BY tblItem.fldStore, tblItem.fldDept, tblItem.fldItem;

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    That is an SQL statement but is not a nested construct which would look like:

    SELECT <fields> FROM (SELECT <fields> FROM table/query ...) AS Query1 ...;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query question/help
    By 9944pdx in forum Queries
    Replies: 4
    Last Post: 12-12-2017, 08:13 PM
  2. Sub-query question
    By hklein in forum Queries
    Replies: 1
    Last Post: 05-01-2012, 03:02 PM
  3. Query Question
    By gjennings1 in forum Access
    Replies: 5
    Last Post: 12-14-2011, 08:34 AM
  4. Question with query
    By Eric Huang in forum Queries
    Replies: 3
    Last Post: 08-01-2011, 05:53 AM
  5. Query Question
    By Guiseppe in forum Queries
    Replies: 5
    Last Post: 03-23-2010, 04:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums