Results 1 to 6 of 6
  1. #1
    bb_ is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    15

    Smile Crosstab query

    I am using a nested query in a crosstab query to essentially do this:



    original table:
    ID Phone Number
    1 111-111-1111
    2 222-222-2222
    2 222-222-2222
    3 333-333-3333
    4 444-444-4444
    4 444-000-0000

    query:
    ID Phone Number 1 Phone Number 2
    1 111-111-1111
    2 222-222-2222
    3 333-333-3333
    4 444-444-4444 444-444-0000



    SQL:
    TRANSFORM First([Phone No]) AS Phone1
    SELECT [ID]
    FROM (Select [ID],[Phone No],DCount("*","Table","[ID] ='" & [ID] & "'
    AND [Phone No] <'" & [Phone No] & "'")+1 AS Seq FROM LD) AS Query1
    Group by [ID]
    PIVOT Seq;


    However I need all of the phone numbers to be transposed into the columns even if they are duplicated. (see ex with ID 2)

    Any ideas?

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,439
    you need an additional ID column (e.g. primary key) to differentiate these two records
    ID Phone Number
    2 222-222-2222
    2 222-222-2222


    because this

    [ID] ='" & [ID] & "' AND [Phone No] <'" & [Phone No] & "'"

    will produce the same result for both records

  3. #3
    bb_ is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    15
    okay thank u. where can i add the autoID field to the sql code

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,439
    you don't add it to the code, it needs to be in the table

    edit: and then referenced by the sql code

  5. #5
    bb_ is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    15
    to the Dcount?

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,439
    DCount("*","Table","[ID] ='" & [ID] & "' AND [PK] <=" & [PK])

    edit: PK will be a number so treat PK as a number

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

Similar Threads

  1. Replies: 17
    Last Post: 07-13-2017, 05:23 PM
  2. Replies: 8
    Last Post: 05-11-2017, 07:54 AM
  3. Replies: 2
    Last Post: 04-30-2014, 03:01 PM
  4. Replies: 2
    Last Post: 08-16-2013, 01:36 AM
  5. Replies: 1
    Last Post: 07-30-2010, 10:28 AM

Tags for this Thread

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