--查詢指定供應商指定的一段時間內出票的張數
如果每查詢一個月,修改一次時間太麻煩, 寫個循環的!
declare @date1 date
declare @date2 date
declare @startdate date
declare @enddate date
declare @countsum int
declare @count int
set @startdate='2010-06-01'
set @enddate='2011-07-01'
set @count=1
set @countsum=DATEDIFF(MM,@startdate,@enddate)
set @date1=@startdate
set @date2=dateadd (mm,1,@startdate)
while @count<@countsum
begin
--執行sql
SELECT SUM (PassengerCount) AS 張數 FROM dbo.Orders WHERE ProviderID =
(SELECT UserId FROM dbo.User
WHERE Username='wind2006')
and OrderTime Between @date1 and @date2
set @date1=dateadd(mm,1,@date1)
set @date2=dateadd(mm,1,@date2)
set @count=@count+1
end
查看本欄目