I have a situation here which I don't know how to handle. Below is what is have and what I am looking for...
I need help in writing a SQL to present output as below
The number of record, versions, columns per a WB are not fixed. I need suggesion about how I can write a SQL to present in the above format.
I appriciate any help in this regards.
This is a pivot query (either old style using max(case) or using the PIVOT function in TD16+).
When the COL_NAME values are not know in advance you need a Stored Procedure to create it dynamically.
Is this what you need?
select WB, BFOR_SEQ, BFOR_VER_SEQ, AFTR_SEQ, AFTR_VER_SEQ,
MAX(case BFOR_BILL_DT when COL_NAME = 'BILL_DT' then BFOR_BILL_DT end)
group by 1,2,3,4,5
order by 1,2,3,4,5
Thank you for the response. I tried PIVOT function and it serves my need partially. There are 365 values for col_name. I don't want to list all these columns when I query for a particular WB number. I want to see only the columns for which there is a record in the source table. If i am querying for more than one WB number, I would like to see only the columns that these two WBs have records for. in other words, the number of the columns in the resultset is dynamic. Any work around using Pivot? Or I need a SP? please let me know if it is not clear.
Whenever the number of values/columns is not known you must use Dynamic SQL.
In a SP you can select the COL_NAMEs and create a matching PIVOT.
Or the client application does the PIVOT.