我有一個報告,其中用戶可以選擇一個過濾器值這三個當中,可以點擊提交按鈕前通過的日期範圍(「從日期」和「終止日期」)查詢:SQL服務器 - 棘手的where子句
日期範圍內- 活動
- 新的日期範圍內的客人
- 新客人除了日期範圍
我想在火選擇查詢myTable的表示所選的過濾器值。
如果用戶選擇第一個值(1),則查詢應提供所有記錄,其中dtEventDate位於輸入的日期範圍之間。
如果用戶選擇第二個值(2),查詢應該給出dtGuestCreatedDate在輸入日期範圍之間的所有記錄。
同樣,如果用戶選擇第三個值(3),則查詢應該給出dtGuestCreatedDate大於'To Date'的所有記錄。
我的查詢會是這樣,
Select * from myTable
where
if '#filterValue#'='Events within Date Range'
then myTable.dtEventDate between '' and ''
else if '#filterValue#'='New Guests withing Date Range'
then myTable.dtGuestCreatedDate between '' and ''
else myTable.dtGuestCreatedDate > ''
我不知道我該怎麼寫這個條件。
我有這個迄今爲止,這將使我的日期範圍內的事件:
select * from myTable
where myTable.dtEventDate between
case when '#filterValue#'='Events Within Date Range' then '10/01/2016' end
and '11/30/2016'
但我需要在where子句檢查於一身的三個濾值。
任何人都可以幫忙嗎?
你使用哪種編程語言?您應根據用戶選擇的過濾器值動態構建您的'WHERE'條件 – rbr94