How to define query TRANSFORM/PIVOT in SQL Server? -


the following ms access sql query given:

parameters formulare![hauptmenü]![startdatum] datetime, formulare![hauptmenü]![enddatum] datetime;  transform count(ds.datum) anzahldatum  select ds.quote_rate ds group ds.quote_rate order ds.quote_rate desc , ds.isin pivot ds.isin; 

i'm trying code above define in sql server stored procedure:

create procedure [dbo].[monthrepo]      -- add parameters stored procedure here     @from datetime,     @to datetime begin     set nocount on;      select *     (         select ds.datum datesum,                ds.ct_quot_rate quote,                ds.isin         ds         ds.datum >= @from , ds.datum <= @to     ) tbl     pivot (         round(quote,0) --incorrect syntax near '0'. expecting '.', id, or quoted_id         isin in(ab000001, ab000002, ab000003) --incorrect syntax near 'ab000001'. expecting '(', or select     ) piv end 

but i'm getting error messages can see message in code. pivot bit complicated me..

the table can see below when input dates @from='2015-01-01', @to='2015-01-03':

datum      | quote_rate | isin ================================== 2015-01-01 | 100        | ab000001 2015-01-01 | 100        | ab000002 2015-01-02 | 98         | ab000003 2015-01-02 | 70         | ab000001 2015-01-03 | 100        | ab000001 

and table want achieve:

quote_rate | ab000001 | ab000002 | ab000003 =========================================== 100        |     2    |     1    | 98         |          |          |     1 70         |     1    |          | 

edit:

the static solution:

create procedure [dbo].[monthrepo]          -- add parameters stored procedure here         @from datetime,         @to datetime         begin         set nocount on;          select *         (             select ds.datum datesum,                    ds.ct_quot_rate quote,                    ds.isin             ds             ds.datum >= @from , ds.datum <= @to         ) tbl         pivot (             count(quote)             isin in(ab000001, ab000002, ab000003)         ) piv     end 

the new question is, how can define static code dynamic query?

try this....

select * (     select ds.datum datesum,            round(ds.ct_quot_rate,0) quote,            ds.isin     ds     ds.datum >= @from , ds.datum <= @to ) tbl pivot (     count(quote) --incorrect syntax near '0'. expecting '.', id, or quoted_id     isin      in(ab000001, ab000002, ab000003) --incorrect syntax near 'ab000001'. expecting '(', or select ) piv 

Comments