Results 1 to 3 of 3
  1. #1
    smatchymo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2019
    Posts
    15

    Union query - adding a single value column with SQL

    I'm practicing Access with a fantasy football database. I have union queries that list the top X amount of players at a position each week. Here's a snippet of the SQL:



    Code:
    (SELECT top 10 qrySortPointsDef2016.Team, qrySortPointsDef2016.Week, qrySortPointsDef2016.TotalFPtsFROM qrySortPointsDef2016
    WHERE (((qrySortPointsDef2016.Week)=1))
    ORDER BY qrySortPointsDef2016.TotalFPts DESC)
    
    
    UNION ALL
    
    
    (SELECT top 10 qrySortPointsDef2016.Team, qrySortPointsDef2016.Week, qrySortPointsDef2016.TotalFPts
    FROM qrySortPointsDef2016
    WHERE (((qrySortPointsDef2016.Week)=2))
    ORDER BY qrySortPointsDef2016.TotalFPts DESC)
    
    
    UNION ALL
    
    
    (SELECT top 10 qrySortPointsDef2016.Team, qrySortPointsDef2016.Week, qrySortPointsDef2016.TotalFPts
    FROM qrySortPointsDef2016
    WHERE (((qrySortPointsDef2016.Week)=3))
    ORDER BY qrySortPointsDef2016.TotalFPts DESC)
    Is there a way to have the result include an extra column that contains the same value for all records. For example, my current results are Team, Week, TotalFpts. I would like Team, Week, TotalFpts, and Tier. Then every field would have a 1 in the Tier column.

    Thanks in advance!

    Edit: I should add that I have a fair amount of these. If possible I would like to use the SQL of the existing union queries to make this happen. I don't really want to add a bunch of new queries using the union queries as the source to do this.

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    IF Tier is a number field, try
    SELECT top 10 qrySortPointsDef2016.Team, qrySortPointsDef2016.Week, qrySortPointsDef2016.TotalFPts, 1 AS Tier FROM qrySortPointsDef2016...
    otherwise if it is text, "1" AS Tier...
    You will have to add this to every union part because each part must have the same field count.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    This is a calculated field, just include in each SELECT:

    1 AS Tier
    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. Sort by column in a UNION ALL query
    By templeowls in forum Queries
    Replies: 3
    Last Post: 03-27-2019, 12:08 PM
  2. Replies: 3
    Last Post: 08-11-2014, 10:14 AM
  3. Replies: 3
    Last Post: 04-17-2013, 07:04 PM
  4. Replies: 9
    Last Post: 01-03-2012, 11:35 AM
  5. Replies: 7
    Last Post: 10-08-2010, 09:48 AM

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