1

私は以下のようなテーブルを持っています

Date   |field1  | qty1 | qty2 | qty3
1 Aug  | xyz    | 0    | 0    | 3
1 Aug  | xyz    | 3    | 0    | 0
1 Aug  | abc    | 0    | 5    | 0
2 Aug  | abc    | 0    | 15   | 0
2 Aug  | xyz    | 0    | 12   | 0
2 Aug  | xyz    | 5    | 0    | 0

以下のように、各数量を個別に表示するための3つのストアドプロシージャを作成しました。

これが私の最初の手順です

create procedure firstprocedure 
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty1) as XYZ,
sum (case when field1 = 'abc', qty1) as ABC 
from table1
where date between @startdate and @enddate
group by date

これは私の2番目の手順です

Create procedure secondprocedure
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty2) as XYZ,
sum (case when field1 = 'abc', qty2) as ABC 
from table1
where date between @startdate and @enddate
group by date

これは私の3番目の手順です

Create procedure thirdprocedure 
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty3) as XYZ,
sum (case when field1 = 'abc', qty3) as ABC 
from table1
where date between @startdate and @enddate
group by date

列(qty1、qty2、またはqty3)をパラメーターに配置し、実行中にqty1、qty2、またはqty3が必要かどうかを言及するだけの可能性はあるのでしょうか。それに応じて出力を生成する必要があります。

4

2 に答える 2

1

これを試して:

次のようなプロシージャを作成します。

create procedure my_procedure(@startdate datetime, @enddate datetime,@qty_type varchar(50))
as
begin 
    with cte as( 
    select 'qty1' [col_name],date, sum (case when field1 = 'xyz' then qty1 end) as XYZ,
    sum (case when field1 = 'abc' then qty1 end) as ABC 
    from table1
    where date between @startdate and @enddate
    group by date
    union all 
    select 'qty2' [col_name],date, sum (case when field1 = 'xyz' then qty2 end) as XYZ,
    sum (case when field1 = 'abc' then qty2 end) as ABC 
    from table1
    where date between @startdate and @enddate
    group by date
    union all
    select 'qty3' [col_name],date, sum (case when field1 = 'xyz' then qty3 end) as XYZ,
    sum (case when field1 = 'abc' then qty3 end) as ABC 
    from table1
    where date between @startdate and @enddate
    group by date
    )select * from cte where [col_name]=@qty_type
end

次に、次のように実行します。

exec '2012-04-01','2012-05-01','qty1'

また

exec '2012-04-01','2012-05-01','qty2'
于 2012-08-16T08:04:44.873 に答える
1

以下に示すように、動的 SQL を使用できます。

CREATE PROCEDURE procedureName
    @ColumnName NVARCHAR(100),
    @startdate datetime, 
    @enddate datetime
AS
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'
    select date, sum (case when field1 = ''''xyz'''', ' + @ColumnName + ') as XYZ,
    sum (case when field1 = ''''abc'''', ' + @ColumnName + ') as ABC 
    from table1
    where date between ' + CONVERT(DateTime, @startdate, 101) + 
        ' and ' + CONVERT(DateTime, @enddate, 101) +
    'group by date'

EXEC(@SQL)

この方法で (SQL から) ストアド プロシージャを呼び出すことができます。

EXEC procedureName 'qty1', '01/01/2012', '01/01/2011'

ノート

を使用した場合、使用している日付形式の要件を満たすためにCONVERT(DateTime, @startdate, 101)スタイル値 (現在は ) を変更する必要があります。101詳細については、このページを参照してください: MSDN CONVERT

于 2012-08-16T08:07:03.820 に答える