如果ELSE在where子句sql server中

时间:2017-08-31 07:19:24

标签: sql-server

我正在尝试使用条件where子句,这意味着我有三个表rdsrds_distrproduct以及如果rds_distr_cd = 'A',那么我只想显示剩下的一行。

表格结构和一些数据 -

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 [rds_distr@rds] FOREIGN KEY([rds_cd])
REFERENCES [dbo].[rds] ([rds_cd])
GO
ALTER TABLE [dbo].[rds_distr] CHECK CONSTRAINT [rds_distr@rds]
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)

我试过这个 -

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)

获得结果 -

rds_cd      prod_cd        rds_distr_cd
INMHA142    DEN101050001    0
INMHM129    DEN101050001    1

我希望如果rds_distr_cd为0,则只显示一行,否则显示第二行。

2 个答案:

答案 0 :(得分: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') 
             )
        )

修改:包含OR作为第二个条件的一部分(在AND内)。

结果为SQL Fiddle

答案 1 :(得分: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