Results 1 to 3 of 3
  1. #1
    stalk is offline Competent Performer
    Windows 8 Access 2003
    Join Date
    May 2014
    Posts
    143

    Cross tab- How to order the pivot colum with out hard coding ( its date field)

    How to order the date_received column with out hard coding. As I will not be knowing the future dates to hard code.




    TRANSFORM Count(PR_S.Id_NO) AS tot
    SELECT PR_S.Final_Result, Count(PR_S.Id_NO) AS Overall_counts
    FROM PR_S
    GROUP BY PR_S.Final_Result
    ORDER BY PR_S.Final_Result, Format([DateReceived],"Short Date")
    PIVOT Format([DateReceived],"Short Date") in(10/26/2017,11/7/2017, 12/14/2017, 12/28/2017,1/4/2018, 1/10/2018);

    If the dates are not hard code, the order is not sorted- 1/10/2018, 1/4/2018, 10/26/2017,11/7/2017, 12/14/2017, 12/28/2017,1/4/2018, 1/10/2018);

    I tried to create a separate table with distinct dates and when I joined with RIGHT join it gave me transform error as I'm not sure what to select in the second table for total(Group by or expression or var or where etc..)

    Thank you so much for your help

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    do not use code,
    instead, make a 'picked' table. Place the dates desired to pull in the table.
    For Q1, Join this tPicked table to the data table to get the data
    Q2 is the Crosstab query of Q1.

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you are ordering a short date which is text

    1/10/2018
    1/4/2018
    10/26/2017
    etc

    in text 1/1 comes before 1/4 and 1/4 comes before 10/, the rest of the characters are immaterial

    you need to use a format which is correct for text e.g.

    20171026
    20180104
    20180110

    or not tested but try just ordering by the date - as a separate column in your query

    Dates are stored as numbers, what you see as a date is just a format. Today is 43123 which is formatted as 23/01/2018 for the UK and 01/23/2018 in the US. When communicating date values as text to SQL you have to use the US format of mm/dd/yyyy otherwise 1/4/2018 (UK 1st April) will be interpreted US style as 4th Jan.

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

Similar Threads

  1. Hard Coding
    By zashaikh in forum Forms
    Replies: 7
    Last Post: 10-30-2017, 01:50 PM
  2. Replies: 4
    Last Post: 03-31-2017, 08:35 AM
  3. Coding and Order in Access Query
    By Ucpaul in forum Queries
    Replies: 5
    Last Post: 04-17-2015, 03:18 PM
  4. Date minus zeven days, How hard can it be ?
    By JeroenMioch in forum Access
    Replies: 4
    Last Post: 11-10-2014, 09:07 AM
  5. SQL Order By Date Field
    By ItsMe in forum Queries
    Replies: 3
    Last Post: 11-12-2013, 04:40 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