0
我有一個查詢,其中繪製了當前以及上個月的男性承諾的銷售數量,我想知道是否有效地執行此操作方式,因爲它看起來是重複的,如果我得到一年的報告,那麼查詢將會非常長。如果我能以任何方式改善這個查詢,請諮詢一下,我正在考慮性能改進或甚至是代碼縮減。謝謝。SQL Server +獲取當前和上個月的銷售查詢
Declare @CurrentMonth varchar(20)
Declare @PreviousMonth varchar(20)
Set @CurrentMonth =
(
select count(*) from transact t
join card c
on (t.cardno = c.cardno)
join member m
on (c.Memberid = m.id)
where mode ='1'
and voidby is null
and gender='M'
and month(transactdate) = month(getdate())
)
Set @PreviousMonth =
(
select count(*) from transact t
join card c
on (t.cardno = c.cardno)
join member m
on (c.Memberid = m.id)
where mode='1'
and voidby is null
and gender='M'
and month(transactdate) = month(dateadd(month, -1, (getdate())))
)
select @currentMonth, @PreviousMonth