0
以下MS访问SQL查询中给出:如何在SQL Server中定义查询TRANSFORM/PIVOT?
PARAMETERS Formulare![Hauptmenü]![Startdatum] DateTime, Formulare![Hauptmenü]![Enddatum] DateTime;
TRANSFORM Count(ds.Datum) AS AnzahlDatum
SELECT ds.quote_rate
FROM ds
GROUP BY ds.quote_rate
ORDER BY ds.quote_rate DESC , ds.isin
PIVOT ds.isin;
我想上面的代码在SQL Server存储过程来定义:
CREATE PROCEDURE [dbo].[MonthRepo]
-- Add the parameters for the stored procedure here
@from datetime,
@to datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM (
SELECT ds.datum AS DateSum,
ds.ct_quot_rate AS Quote,
ds.isin
FROM ds
WHERE ds.datum >= @from AND ds.datum <= @to
) tbl
PIVOT (
ROUND(Quote,0) --Incorrect syntax near '0'. Expecting '.', ID, or QUOTED_ID
FOR isin IN(AB000001, AB000002, AB000003) --Incorrect syntax near 'AB000001'. Expecting '(', or SELECT
) piv
END
但我发现了错误信息,你可以看到代码中的消息。 PIVOT是有点复杂,我..
表下面你可以看到当输入日期@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
这表我想要实现:
quote_rate | AB000001 | AB000002 | AB000003
===========================================
100 | 2 | 1 |
98 | | | 1
70 | 1 | |
编辑:
静态解决方案:
CREATE PROCEDURE [dbo].[MonthRepo]
-- Add the parameters for the stored procedure here
@from datetime,
@to datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM (
SELECT ds.datum AS DateSum,
ds.ct_quot_rate AS Quote,
ds.isin
FROM ds
WHERE ds.datum >= @from AND ds.datum <= @to
) tbl
PIVOT (
COUNT(Quote)
FOR isin IN(AB000001, AB000002, AB000003)
) piv
END
新的问题是,我怎样才能定义我的静态代码为DYNAMIC查询?
你能告诉我我该怎么定义我的静态代码,以动态查询? – yuro