+ Reply to Thread
Results 1 to 7 of 7

Thread: Pivot with out Aggregate Function -SQL 2005

  1. #1
    Barn Newbie MySQL is an unknown quantity at this point MySQL's Avatar
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    3

    Pivot with out Aggregate Function -SQL 2005

    Hi All,
    I have the data in temp table in this format
    PID TypeId Date
    111 00010 2009-07-14
    111 00011 2008-08-04
    111 00012 2007-08-07

    I want the output as
    PID TypeID1 Date1 TypeID2 Date2 TypeId3 date3
    111 00010 2009-07-14 00011 2008-08-04 00012 2007-08-07

    i am using SQL server 2005. how can we query to get the data as above output
    Please let me if this is possible with SQL pivot. If not, any ideas..?

    Thanks in advance

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Hi, take a look at the example here: Pivot without aggregate function?

    They still use an aggregate function, and I don't think there is a way out of that when using a pivot. However, the correct results are returned.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    try this:
    Code:
    With typerank as
    (select pid, typeid, date,
    	row_number () over (
    		partition by pid 
    		order by typeid) as rn
    from test_1
    )
    select pid, 
    	max(case when rn = 1 then typeid end) as typeid1,
    	max(case when rn = 1 then date end) as date1,
    	max(case when rn = 2 then typeid end) as typeid2,
    	max(case when rn = 2 then date end) as date2,
    	Max(case when rn = 3 then typeid end) as typeid3,
    	max(case when rn = 3 then date end) as date3
    from typerank t
    group by pid
    order by pid
    
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    You can use the PIVOT function to do this, and yes, it DOES require an aggregate function. In you case, try using min(date) and the aggregate -- assuming that PID and TypeID uniquely identify the date you want.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #5
    Barn Newbie MySQL is an unknown quantity at this point MySQL's Avatar
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    3

    Hi Thanx for the replies,

    I tried using min/max in pivot but it didn't work.
    SELECT * from
    (SELECT TypeId, Date, PID
    FROM SourceTable) st
    PIVOT ( max(Date) FOR Date in (V1 ,V2 ,V3 ,V4 ,V5 ,V6 ,V7,V8) )AS pvt

    But I didn't get the result as expected also i got data type error.

    Also Mehere, with your code PID's are hard coded we can't code in our requirement. i had one more doubt using "with and max " in your code..... will it slow down the performance....??

  6. #6
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    i'm not sure what you mean by "PID's are hard coded". i don't see how this has anything to do with the code i posted.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  7. #7
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Well, you have a problem with your syntax. Specificially
    Code:
    PIVOT ( max(Date) FOR Date in (V1 ,V2 ,V3 ,V4 ,V5 ,V6 ,V7,V8) )AS pvt
    
    I doubt that Date is ever going to take the value V1, V2, V3 etc. Pivot requries two columns, the first, the one that takes the aggregate, that supplies the values and the second, between the for and the in, the will become the columns in the pivot table. Before we get to deep into this, how many distinct values are there of TYPEID?

    Also, I why worry about the performance of the aggregate function? PIVOT is a real pig -- I doubt you would even notice the overhead of the aggregate function.

    [edit] I just looked back at you OP and realized that you are doing a true PIVOT here -- in a PIVOT, there is column that supplies the names of the new column names and a column the supplies the values to be aggregated. If we were doing a true PIVOT , you would have your dates in columns named 00010, 00011, 00012, etc. [/edit]
    Last edited by Wolffy; July 21st, 2009 at 10:55 AM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. SQL Pivot w/o aggregate function
    By Wolffy in forum SQL Development
    Replies: 4
    Last Post: June 22nd, 2009, 03:40 PM
  2. Help with JS function
    By Wolffy in forum JavaScript Programming
    Replies: 1
    Last Post: June 9th, 2009, 10:52 AM
  3. Help using function
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: October 13th, 2008, 01:13 PM
  4. Error MSG aggregate function
    By nboscaino in forum SQL Development
    Replies: 3
    Last Post: August 2nd, 2008, 11:14 AM
  5. without function
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: July 15th, 2008, 05:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO