Distinct Data as column name

  • Is this possible within TSQL?

    InvoiceHeader schema / sample data

    ClientID

    InvoiceDate

    InvoiceTotal

    {guid1}1/1/20051500
    {guid1}2/1/20051250
    {guid1}3/1/2005995
    {guid2}1/1/2005500
    {guid2}3/1/2005750

    I want to return a dataset where the distinct InvoiceDate values are the column names with the InvoiceTotal as the column value

    eg:

    ClientID1/1/20052/1/20053/1/2005
    {guid1}15001250995
    [guid2}500

    NULL

    750
  • Sure, but you're going to have to use a stored proc and dynamic SQL.

    Unless you know in advance what the columns are always going to be.

     

  • No, the invoicedate values are variable.

  • Answered many times and many ways.  Try a search on this site for:

    "variable column names"

  • Thanks, will do.

    [Edit] - In reviewing those as well as others, none are quite the same in that my column names will be totally variable, so the traditional Cross-tab methodology of using a CASE statement with a defined column name does not apply.

    Seems to be what's apparently referred to as a Dynamic Crosstab query. I'm researching that now.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply