declare @bid varchar(4) = '1007'
select sr.release_date,
sr.customer_id,
sr.customer_name,
sr.item_id,
sr.item_desc,
sr.order_no,
sr.qty_on_hand,
sr.release_qty,
sr.qty_after_released,
sr.extended_sell,
sr.enough,
sr.route_description
from sri_schedule_releases_view as sr
where
((case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'John')
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Jane')
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Doe')
end = @bid) or
(sr.contact_id = @bid))
order by sr.item_id asc
このクエリは正常に機能します。しかし、sr.route_description
PM 01、PM およびPM 05を持っている人のみsr.route_code
を表示したいと思います。
これを行う可能性はありますか?
私は解決策を見つけ、それは期待通りに機能しました
declare @bid varchar(4) = '1007'
declare @pm1 varchar(5) = (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'John')
declare @pm3 varchar(5) = (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Jane')
declare @pm5 varchar(5) = (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Doe')
if @bid in (@pm1,@pm3,@pm5)
begin
select sr.release_date,
sr.customer_id,
sr.customer_name,
sr.item_id,
sr.item_desc,
sr.order_no,
sr.qty_on_hand,
sr.release_qty,
sr.qty_after_released as available_qty,
sr.extended_sell as ext_sell,
sr.enough,
sr.route_description
from sri_schedule_releases_view as sr
where
((case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm1
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm3
end =@bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm5
end = @bid) or
(sr.contact_id = @bid))
end
else
begin
select sr.release_date,
sr.customer_id,
sr.customer_name,
sr.item_id,
sr.item_desc,
sr.order_no,
sr.qty_on_hand,
sr.release_qty,
sr.qty_after_released as available_qty,
sr.extended_sell as ext_sell,
sr.enough
from sri_schedule_releases_view as sr
where
((case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm1
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm3
end =@bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then @pm5
end = @bid) or
(sr.contact_id = @bid))
end
case
ステートメントをselect
リストに追加するだけです。
declare @bid varchar(4) = '1007'
select sr.release_date,
sr.customer_id,
sr.customer_name,
sr.item_id,
sr.item_desc,
sr.order_no,
sr.qty_on_hand,
sr.release_qty,
sr.qty_after_released,
sr.extended_sell,
sr.enough,
case when sr.route_code in ('pm 01','pm 03','pm 05') then sr.route_description else '' end as route_description
from sri_schedule_releases_view as sr
where
((case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'John')
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Jane')
end = @bid) or
(case
when sr.route_code in ('pm 01','pm 03','pm 05') then (select users.contact_id from users where users.delete_flag = 'n' and users.name = 'Doe')
end = @bid) or
(sr.contact_id = @bid))
order by sr.item_id asc
データが存在する場合にのみ列を表示したい場合は、IFステートメント内に2つの可能性がある関数を作成します。
IFデータが存在する(通常のクエリを実行する)ELSE
(同じクエリをコピーしますが、空の列はありません)