Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286

    Include ROW Count in Query???

    Hello, im using access 2010. How do i edit my query so that it will display the row number in the column? I only have two columns: AMR AIS# and Count. I want the column "Count" to display the number of rows. By the way, the IDs derive from another query, so just simply selecting the "summation" button and choosing "count" will not work as it wont be accurate. Heres how i want it:





    ID Count

    000123 1
    000124 2
    000152 3

  2. #2
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    INSERT INTO dbo_List_Detail ( Account_ID )
    SELECT qry_select_only_missingdata_or_unabletoconnect_by_ group.[AMR AIS #]
    FROM qry_select_only_missingdata_or_unabletoconnect_by_ group;

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Calculating a sequential number in query requires subquery. Review: http://allenbrowne.com/ranking.html

    Easier to use the RunningSum property of textbox in report. The ControlSource would be =1.

    Why wouldn't the aggregate Count be accurate?
    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
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    June, thansk for the help. The SQL i have above IS a sub query. Its a query querying another query. The reason why i didnt use the summation and select by count is because there is duplicate data in the query that i was based off of (but i used group by to group up all the duplicates). Can you help me write what i should put into the "Field" column of the Design View of my query to create my "Count" column?

    Heres what i have so far:

    Count: (Select Count([AMR AIS #]))

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Either way, need another query. Use the GROUP BY query as basis for another query to do Count or as source for the row number calc. The general syntax to return row number over all records (not by group) would be:

    SELECT Query1.ID, (SELECT Count(ID) AS RN FROM Query1 As Dupe WHERE Dupe.ID<Query1.ID) AS RowNum FROM Query1;

    If all you care about is the total and not details, not a row number, do the Count.

    What do you plan to do with this query?

    A report using Grouping & Sorting with aggregate calcs in header/footer sections would probably be easiest. This will allow display of detail records and summary data. The GROUP BY query would be the form's RecordSource.
    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
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    june, thanks for the help. to answer your question, the results of this query will be used to append into a table. Therefore, i cannot use report as that is not the purpose of the query. I tried following your format but am getting an error message about missing a ( { or something somwhere. Can you help me figure out the problem? thanks! The name of my qry is qry_select_only_missingdata_or_unabletoconnect_by_ group and the name of the ID column is AMR AIS #


    SELECT qry_select_only_missingdata_or_unabletoconnect_by_ group.[AMR AIS #], (SELECT Count(AMR AIS #) AS RowNum FROM qry_select_only_missingdata_or_unabletoconnect_by_ group As Dupe WHERE Dupe.[AMR AIS #]<qry_select_only_missingdata_or_unabletoconnect_by _group.[AMR AIS #]) AS Q1 FROM qry_select_only_missingdata_or_unabletoconnect_by_ group

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Missing []
    SELECT Count([AMR AIS #])

    BTW, advise no spaces, special characters or punctuation (underscore is exception) in names nor reserved words as names. If do, must enclose in []. Sometimes Access will try to provide [] but can't depend on. Better would be AMR_AIS_Num.
    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
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    I see, i just left it like that before because that column was taken from a link table to an excel spreadsheet and thats how the column is formatted. Will take note of it in the future. Now im getting the message "you may have entered an operand without an operator" and highlights the "AS" right before the "Q1" in the statement below

    Q1: (SELECT Count([AMR AIS #]) AS RowNum FROM qry_select_only_missingdata_or_unabletoconnect_by_ group As Dupe WHERE Dupe.[AMR AIS #]<qry_select_only_missingdata_or_unabletoconnect_ by _group.[AMR AIS #]) AS Q1 FROM qry_select_only_missingdata_or_unabletoconnect_by_ group

  9. #9
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    june, it is working now, i just had to rename my id (i used a format() function). Only thing is, it is displaying a "0" as the first row. is there a way to make it begin with "1"? Thanks so much

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try + 1 and I changed the aliases a little:

    SELECT qry_select_only_missingdata_or_unabletoconnect_by_ group.[AMR AIS #], (SELECT Count([AMR AIS #]) + 1 AS RN FROM qry_select_only_missingdata_or_unabletoconnect_by_ group As Dupe WHERE Dupe.[AMR AIS #]<qry_select_only_missingdata_or_unabletoconnect_ by _group.[AMR AIS #]) AS RowNum FROM qry_select_only_missingdata_or_unabletoconnect_by_ group;
    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.

  11. #11
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    Wow, you are a genius!, it worked. thanks so much!

  12. #12
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    june, after trying this query again, for some reason it is taking forever to load. it takes almost 5 minutes, and sometimes even freezes the database. When i remove the whole "count" function, it works fine. Is there a way to re work this to make it simpler for the database to process? if you have any ideas let me know. thanks!

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I don't know why it would have slowed down.

    The RunningSum property in report is the simplest approach.

    Anything else requires VBA. You could use VBA to populate a field in table with sequential value. Requires looping through an editable recordset and setting value of field which will update the table.
    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.

  14. #14
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    here is how my query is written right now, do you see any issues with it?

    SELECT qry_select_only_missingdata_or_unabletoconnect_by_ group.AMR_AIS_Num, (SELECT Count(AMR_AIS_Num) + 1 AS RowNum FROM
    qry_select_only_missingdata_or_unabletoconnect_by_ group As Dupe WHERE Dupe.AMR_AIS_Num<
    qry_select_only_missingdata_or_unabletoconnect_by_ group.AMR_AIS_Num) AS [Count], Format([AMR_AIS_Num],"000000") AS AMR_AIS_Num2
    FROM qry_select_only_missingdata_or_unabletoconnect_by_ group;

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Might try without the Format function. Instead apply format in textbox ControlSource expression on form or report.
    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.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2012, 08:38 PM
  2. Query results to include descriptions
    By funkygoorilla in forum Queries
    Replies: 4
    Last Post: 02-07-2012, 11:07 AM
  3. Replies: 3
    Last Post: 01-05-2012, 12:04 PM
  4. Include zero values in below query!
    By daffykyle in forum Access
    Replies: 3
    Last Post: 11-30-2011, 08:56 AM
  5. Only include unique occurances
    By filla_dilla in forum Queries
    Replies: 8
    Last Post: 07-18-2011, 10:22 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