2017-08-31 28 views
0

に私は3つのテーブルrdsrds_distrproductを持っているし、rds_distr_cd = 'A'ならば、私は他に一つだけの行残りの行を表示したい意味条件where句をやろうとしています。ELSE IFは、where句のSQL Serverの

テーブル構造のスクリプトと一部のデータ - rds_distr_cdは0のみである場合

rds_cd  prod_cd  rds_distr_cd 
INMHA142 DEN101050001 0 
INMHM129 DEN101050001 1 

そして、私が欲しい -

select rds_cd, prod_cd,rds_distr_cd from 
(select distinct r.rds_cd,prod_cd,case when r.rds_distr_cd = 'A' then 0 when r.rds_distr_cd = 'B' then 1 when r.rds_distr_cd = 'P' THEN 2 END AS rds_distr_cd 
           from product p,rds r left join rds_distr rd on r.rds_cd = rd.rds_cd 
           where r.rds_distr_cd ='A' 
           AND (r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
           and p.prod_cd in ('DEN101050001') 
           ) 
           OR (r.rds_distr_cd in ('B','p') 
           AND r.rds_cd = rd.rds_cd 
           and (rd.prod_brand_cd =p.prod_brand_cd 
           or rd.prod_line_cd = p.prod_line_cd) 
           and r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
           and p.prod_cd in ('DEN101050001') 
           )) as a 
           where rds_distr_cd = IIF(rds_distr_cd = 0,0,0) 
           OR rds_distr_cd =IIF(rds_distr_cd = 1,1,1) 

の取得結果 - 私はこれを試してみました

CREATE TABLE [dbo].[product](
    [prod_cd] [varchar](20) NOT NULL, 
    [prod_name] [varchar](100) NOT NULL, 
    [prod_short_name] [varchar](100) NOT NULL, 
    [uom_cd] [varchar](4) NOT NULL, 
    [inner_qty_mul_val] [decimal](5, 2) NULL, 
    [prod_catg_cd] [varchar](4) NOT NULL, 
    [prod_line_cd] [varchar](4) NOT NULL, 
    [prod_brand_cd] [int] NOT NULL, 
    [prod_grp_cd] [varchar](4) NULL, 
    [prod_subgrp_cd] [varchar](4) NULL, 
    [combi_prod_flg] [varchar](1) NULL, 
    [bar_code] [varchar](50) NULL, 
    [remark] [varchar](250) NULL, 
    [active_flg] [varchar](1) NOT NULL, 
    [time_stamp] [bigint] NOT NULL, 
    [simpl_prod_desc] [varchar](100) NULL, 
    [inside_schm_desc] [varchar](100) NULL, 
    [prod_type] [varchar](1) NULL, 
    [mbl_active_flg] [varchar](1) NULL, 
    [hsn_cd] [varchar](20) NULL, 
CONSTRAINT [product_pk] PRIMARY KEY CLUSTERED 
(
    [prod_cd] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

/****** Object: Table [dbo].[rds] Script Date: 8/31/2017 11:42:24 AM ******/ 

CREATE TABLE [dbo].[rds](
    [rds_cd] [varchar](10) NOT NULL, 
    [rds_name] [varchar](100) NOT NULL, 
    [rds_short_name] [varchar](50) NOT NULL, 
    [comp_cd] [varchar](4) NULL, 
    [locn_cd] [varchar](4) NULL, 
    [hq_town_cd] [int] NULL, 
    [rds_type_cd] [varchar](1) NULL, 
    [rds_distr_cd] [varchar](1) NULL, 
    [apoint_dt] [datetime] NULL, 
    [resign_dt] [datetime] NULL, 
    [eff_start_dt] [datetime] NULL, 
    [eff_end_dt] [datetime] NULL, 
    [agreement_dt] [datetime] NULL, 
    [sec_amt] [numeric](9, 2) NULL, 
    [ref_no] [varchar](50) NULL, 
    [area] [varchar](100) NULL, 
    [block_flg] [varchar](1) NULL, 
    [vacant_flg] [varchar](1) NULL, 
    [active_flg] [varchar](1) NOT NULL, 
    [mobile_no_1] [varchar](20) NULL, 
    [mobile_no_2] [varchar](20) NULL, 
    [time_stamp] [bigint] NOT NULL DEFAULT ((0)), 
CONSTRAINT [rds_pk] PRIMARY KEY CLUSTERED 
(
    [rds_cd] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

/****** Object: Table [dbo].[rds_distr] Script Date: 8/31/2017 11:42:24 AM ******/ 

CREATE TABLE [dbo].[rds_distr](
    [rds_distr_seq] [int] IDENTITY(1,1) NOT NULL, 
    [rds_cd] [varchar](10) NULL, 
    [prod_line_cd] [varchar](4) NULL, 
    [prod_brand_cd] [int] NULL, 
    [start_dt] [datetime] NULL, 
    [end_dt] [datetime] NULL, 
CONSTRAINT [rds_distr_pk] PRIMARY KEY CLUSTERED 
(
    [rds_distr_seq] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 


GO 

ALTER TABLE [dbo].[rds_distr] WITH NOCHECK ADD CONSTRAINT [[email protected]] FOREIGN KEY([rds_cd]) 
REFERENCES [dbo].[rds] ([rds_cd]) 
GO 
ALTER TABLE [dbo].[rds_distr] CHECK CONSTRAINT [[email protected]] 
GO 

INSERT [dbo].[rds] ([rds_cd], [rds_name], [rds_short_name], [comp_cd], [locn_cd], [hq_town_cd], [rds_type_cd], [rds_distr_cd], [apoint_dt], [resign_dt], [eff_start_dt], [eff_end_dt], [agreement_dt], [sec_amt], [ref_no], [area], [block_flg], [vacant_flg], [active_flg], [mobile_no_1], [mobile_no_2], [time_stamp]) VALUES (N'INMHA142', N'Amar Trading Co', N'Amar Trading Co', N'SEN', N'PNE', 50, N'R', N'A', CAST(N'2013-07-29 00:00:00.000' AS DateTime), NULL, CAST(N'2013-07-29 00:00:00.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, N'N', N'N', N'Y', NULL, NULL, 0) 

INSERT [dbo].[rds] ([rds_cd], [rds_name], [rds_short_name], [comp_cd], [locn_cd], [hq_town_cd], [rds_type_cd], [rds_distr_cd], [apoint_dt], [resign_dt], [eff_start_dt], [eff_end_dt], [agreement_dt], [sec_amt], [ref_no], [area], [block_flg], [vacant_flg], [active_flg], [mobile_no_1], [mobile_no_2], [time_stamp]) VALUES (N'INMHA141', N'Atharv Traders', N'Atharv Traders', N'SEN', N'PNE', 152, N'A', N'A', CAST(N'2013-07-01 00:00:00.000' AS DateTime), NULL, CAST(N'2013-07-01 00:00:00.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, N'N', N'N', N'Y', NULL, NULL, 0) 

INSERT [dbo].[rds] ([rds_cd], [rds_name], [rds_short_name], [comp_cd], [locn_cd], [hq_town_cd], [rds_type_cd], [rds_distr_cd], [apoint_dt], [resign_dt], [eff_start_dt], [eff_end_dt], [agreement_dt], [sec_amt], [ref_no], [area], [block_flg], [vacant_flg], [active_flg], [mobile_no_1], [mobile_no_2], [time_stamp]) VALUES (N'INMHM129', N'Mittal Agency', N'Mittal Agency', N'SEN', N'PNE', 50, N'R', N'B', CAST(N'2014-04-03 00:00:00.000' AS DateTime), NULL, CAST(N'2014-04-03 00:00:00.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, N'N', N'N', N'Y', NULL, NULL, 1500890410) 

INSERT [dbo].[rds] ([rds_cd], [rds_name], [rds_short_name], [comp_cd], [locn_cd], [hq_town_cd], [rds_type_cd], [rds_distr_cd], [apoint_dt], [resign_dt], [eff_start_dt], [eff_end_dt], [agreement_dt], [sec_amt], [ref_no], [area], [block_flg], [vacant_flg], [active_flg], [mobile_no_1], [mobile_no_2], [time_stamp]) VALUES (N'INMHK083', N'Krishna Traders', N'Krishna Traders', N'SEN', N'PNE', 50, N'R', N'P', CAST(N'2014-12-26 00:00:00.000' AS DateTime), NULL, CAST(N'2014-12-26 00:00:00.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, N'N', N'N', N'Y', NULL, NULL, 1504005202) 

set identity_insert rds_distr on 
INSERT [dbo].[rds_distr] ([rds_distr_seq], [rds_cd], [prod_line_cd], [prod_brand_cd], [start_dt], [end_dt]) VALUES (1099, N'INMHK083', N'01', NULL, CAST(N'2017-08-01 00:00:00.000' AS DateTime), NULL) 

INSERT [dbo].[rds_distr] ([rds_distr_seq], [rds_cd], [prod_line_cd], [prod_brand_cd], [start_dt], [end_dt]) VALUES (1100, N'INMHK083', N'06', NULL, CAST(N'2017-08-01 00:00:00.000' AS DateTime), NULL) 

INSERT [dbo].[rds_distr] ([rds_distr_seq], [rds_cd], [prod_line_cd], [prod_brand_cd], [start_dt], [end_dt]) VALUES (1098, N'INMHM129', NULL, 1, CAST(N'2001-01-01 00:00:00.000' AS DateTime), NULL) 

INSERT [dbo].[product] ([prod_cd], [prod_name], [prod_short_name], [uom_cd], [inner_qty_mul_val], [prod_catg_cd], [prod_line_cd], [prod_brand_cd], [prod_grp_cd], [prod_subgrp_cd], [combi_prod_flg], [bar_code], [remark], [active_flg], [time_stamp], [simpl_prod_desc], [inside_schm_desc], [prod_type], [mbl_active_flg], [hsn_cd]) VALUES (N'DEN101050001', N'Aps Pltnm Rt Pncl/Lpsh,Nder/10X10', N'Aps Pltnm Rt Pncl/Lpsh,Nder/10X10', N'BOX', CAST(0.10 AS Decimal(5, 2)), N'FG', N'01', 1, NULL, NULL, N'N', NULL, NULL, N'Y', 1500634997, N'Aps Pltnm Rt Pncl/Lpsh,Nder/10X10 101050001', NULL, N'M', N'Y', NULL) 

INSERT [dbo].[product] ([prod_cd], [prod_name], [prod_short_name], [uom_cd], [inner_qty_mul_val], [prod_catg_cd], [prod_line_cd], [prod_brand_cd], [prod_grp_cd], [prod_subgrp_cd], [combi_prod_flg], [bar_code], [remark], [active_flg], [time_stamp], [simpl_prod_desc], [inside_schm_desc], [prod_type], [mbl_active_flg], [hsn_cd]) VALUES (N'DEN101049001', N'Aps Stn Pncl/Lpsh,Nder/10X100', N'Aps Stn Pncl/Lpsh,Nder/10X10', N'BOX', CAST(0.10 AS Decimal(5, 2)), N'FG', N'01', 1, NULL, NULL, N'N', NULL, NULL, N'Y', 1500634997, N'Aps Stn Pncl/Lpsh,Nder/10X100 101049001', N'Sharpner', N'M', N'Y', NULL) 

INSERT [dbo].[product] ([prod_cd], [prod_name], [prod_short_name], [uom_cd], [inner_qty_mul_val], [prod_catg_cd], [prod_line_cd], [prod_brand_cd], [prod_grp_cd], [prod_subgrp_cd], [combi_prod_flg], [bar_code], [remark], [active_flg], [time_stamp], [simpl_prod_desc], [inside_schm_desc], [prod_type], [mbl_active_flg], [hsn_cd]) VALUES (N'DEN101054001', N'Aps Ntrl R/T Pncl/LPSh,NDEr/10x10', N'Aps Ntrl R/T Pncl/LPSh,NDEr/10x10', N'BOX', CAST(0.10 AS Decimal(5, 2)), N'FG', N'01', 1, NULL, NULL, N'N', NULL, NULL, N'N', 1, N'Aps Ntrl R/T Pncl/LPSh,NDEr/10x10 101054001', NULL, N'M', N'N', NULL) 

1つの行は2番目に表示されます。

答えて

0

試してみるのはあまりした後、私は解決策に答えため

IF EXISTS (select distinct r.rds_cd,prod_cd, r.rds_distr_cd 
from product p,rds r 
where r.rds_distr_cd ='A' 
AND r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
and p.prod_cd in ('DEN101050001')) 
BEGIN 
select distinct r.rds_cd,prod_cd, r.rds_distr_cd 
from product p,rds r 
where r.rds_distr_cd ='A' 
AND r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
and p.prod_cd in ('DEN101050001') 
END 
ELSE 
BEGIN 
select distinct r.rds_cd,prod_cd, r.rds_distr_cd 
from product p,rds r left join rds_distr rd on r.rds_cd = rd.rds_cd 
where r.rds_distr_cd in ('B','p') 
AND r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
and p.prod_cd in ('DEN101050001') 
END 
0

where句には、両方ともAND & ORという複数の条件があります。括弧を正しく使用しない限り、期待どおりに動作しません。

はこれにあなたの where句を変更

where r.rds_distr_cd ='A' 
    AND ((r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
      and p.prod_cd in ('DEN101050001') 
     ) 
     OR (r.rds_distr_cd in ('B','p') 
      AND r.rds_cd = rd.rds_cd 
      and (rd.prod_brand_cd =p.prod_brand_cd 
        or rd.prod_line_cd = p.prod_line_cd 
       ) 
      and r.rds_cd in ('INMHA142','INMHK083','INMHM129') 
      and p.prod_cd in ('DEN101050001') 
      ) 
     ) 

変更を:(AND内部の)第二の条件の一部としてOR含まれています。

SQL Fiddleに結果があります。

+0

感謝を得たが、「A」のレコードがない場合、それはすべてのレコードを与えていません。それは2番目のレコードを与える必要があります。 – Anagha

+0

この状態では、 – Anagha

+0

からrds 'INMHA142'を削除してください。@Anagha:これは、適切に括弧を使用することを指摘していました。あなたのロジックに従って 'where'節を変更してください。 –