Results 1 to 4 of 4
  1. #1
    rbevers is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    2

    Getting multiple rows into one row in select

    Given the following table and data:

    TimeStamp HistoryPointID Value
    8/1/2013 1:00:00 PM 1 19.15
    8/1/2013 1:00:00 PM 2 117.1104
    8/1/2013 1:00:00 PM 3 207.111
    8/1/2013 1:00:00 PM 4 85.3264
    8/1/2013 12:00:00 PM 1 20.1667
    8/1/2013 12:00:00 PM 2 140.071
    8/1/2013 12:00:00 PM 3 205.1211
    8/1/2013 12:00:00 PM 4 85.3285



    I would like to do a select where I would get;

    TimeStamp, Value 1, Value 2, Value 3, Value 4
    TimeStamp, Value 1, Value 2, Value 3, Value 4


    or in other words using the actual data:
    TimeStamp Value1 Value2 value3 value4
    8/1/2013 1:00:00 PM 19.15 117.1104 207.111 85.3264
    8/1/2013 12:00:00 PM 20.1667 140.071 205.1211 85.3285

    I have tried group by TimeStamp and I just cant get it. Is there a way in Access to do this? In Microsoft SQL I could do it in the joins, but Access seems to have kittens if I try that.

    Thanks in advance,
    Bob

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Did you try a CROSSTAB query that groups on the TimeStamp field and pivots on the HistoryPointID and uses the First aggregate function on the 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.

  3. #3
    rbevers is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2013
    Posts
    2
    No I didn't because I have to do it in code and I don't know the SQL for that.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Use Access query designer to construct the SQL statement. Switch to SQL View to see the statement. Copy/paste it into VBA and make necessary adjustments.

    Alternatives:

    1. emulate a crosstab http://www.datapigtechnologies.com/f.../crosstab.html:
    SELECT TimeStamp, Last(IIf([HistoryPointID]=1,[Value],Null)) As Value1, Last(IIf([HistoryPointID]=2,[Value],Null)) As Value2, Last(IIf([HistoryPointID]=3,[Value],Null)) As Value3, Last(IIf([HistoryPointID]=4,[Value],Null)) As Value4 GROUP BY TimeStamp;

    2. 4 queries, each with filter for a single HistoryPointID value (assuming there are only the 4 shown in example data), and a 5th query to create a dataset of unique TimeStamp values. Then a 6th query to join the 4 HistoryPointID queries to the distinct TimeStamp query.

    3. write records to a temp 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.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 03:54 PM
  2. Multi select rows in recordset
    By Charlie in forum Programming
    Replies: 1
    Last Post: 01-31-2013, 11:49 PM
  3. Replies: 0
    Last Post: 11-30-2010, 12:51 PM
  4. Concatenate Multiple Rows into One
    By Knarly555 in forum Queries
    Replies: 5
    Last Post: 11-12-2010, 06:51 PM
  5. Replies: 5
    Last Post: 12-10-2009, 10:33 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