0

SQL Server 2008 で 2 つの特定の日付の間のすべての日付を表示する必要がありますか? テーブルには日付、数量などのフィールドが含まれているため、開始日 = '01/06/2013' および終了日 = '05/06/2013' を指定した場合、これらの日付間のすべての日付と数量を表示する必要があります。 .

出力は次のようになります。

Date(date datatype)         Day         Produced Qty    

01-06-13          Saturday          400.00  
02-06-13          Sunday        550.00  
03-06-13          Monday        200.00  
04-06-13         Tuesday        100.00  
05-06-13          Wednsdy       250.00  

Total                       1500.00 

助けてください?

4

2 に答える 2

5

これを使ってみて...

Select date, day, produce_qty from Table
where date >= '03/06/2013' and date < '05/06/2013'

または

Select date, day, produce_qty from Table
    where date BETWEEN '03/06/2013' and '05/06/2013'
于 2013-06-01T13:00:30.607 に答える
1

OUTER JOINこれにより、データで使用できる日付の表が得られます。

declare @Start as Date = '20130501';
declare @End as Date = '20130515';

with Dates as (
  select @Start as [ReportDate]
  union all
  select DateAdd( day, 1, ReportDate )
    from Dates
    where ReportDate < @End )
  select ReportDate
    from Dates option ( MaxRecursion 0 );

EDIT:または、サンプルデータで:

declare @Production as Table ( ActivityDate Date, ProductionQuantity Int );
insert into @Production ( ActivityDate, ProductionQuantity ) values
  ( '20130106', 400 ),
  ( '20130112', 550 ),
  ( '20130112', 50 );

declare @Start as Date = '20130101';
declare @End as Date = '20130115';

with Dates as (
  select @Start as [ReportDate]
  union all
  select DateAdd( day, 1, ReportDate )
    from Dates
    where ReportDate < @End )
  select ReportDate, Coalesce( Sum( P.ProductionQuantity ), 0 ) as Qty
    from Dates as D left outer join
      @Production as P on P.ActivityDate = D.ReportDate
      group by D.ReportDate
      option ( MaxRecursion 0 );
于 2013-06-01T15:07:05.297 に答える