繁体   English   中英

SQL根据列值选择

[英]SQL select based on column value

我们有一个包含公司工作代码的数据库表,如下所示:

ID       Code       Company
1        EW10       ***
2        EW10       DEU
3        EW10       DEC
4        EW20       ***
5        EW30       DEU
6        EW40       DEC

公司中的“ ***”表示企业级别的职务代码,可以在雇主级别(DEU,DEC)覆盖。

我需要一条选择语句,该语句返回具有以下条件的行:

  • 如果没有公司特定代码(例如EW20),则返回企业级行(例如第4行)

  • 如果有公司特定的行,则返回所有公司特定的行

我需要的结果集如下:

ID       Code       Company
2        EW10       DEU
3        EW10       DEC
4        EW20       ***
5        EW30       DEU
6        EW40       DEC

使用窗口功能。 这比必需的要复杂一些,因为使用的是***而不是NULL 以下将来回转换为NULL

select t.*,
       coalesce(nullif(company, '***'),
                max(nullif(company, '***')) over (partition by code),
                '***'
               )
from t;

编辑:

您的问题表明您要返回所有行。 但是,您的样本数据则相反。

我认为您可能想要:

select t.*
from t
where t.company <> '***' or
      not exists (select 1
                  from t t2
                  where t2.code = t.code and
                        t2.company <> '***'
                 );

或者,具有窗口功能:

select t.*
from (select t.*,
             sum(case when company <> '***' then 1 else 0 end) over (partition by code) as num_notstars
      from t
     ) t
where (num_notstars > 0 and company <> '***') or
      (num_notstarts = 0);

对于2种情况,使用UNION ALL:

select * from tablename
where Company <> '***'
union all
select * from tablename t
where not exists (
  select 1 from tablename
  where Code = t.Code and Company <> '***'
)
order by Id

要么:

select * from tablename
where Company <> '***'
or Code in (
  select Code from tablename
  group by Code
  having min(Company) = '***' and max(Company) = '***'
)

参见演示
结果:

> ID | Code | Company
> -: | :--- | :------
>  2 | EW10 | DEU    
>  3 | EW10 | DEC    
>  4 | EW20 | ***    
>  5 | EW30 | DEU    
>  6 | EW40 | DEC  

另一个选择

Select ID,Code,Company
 From  (
        Select *
              ,RN  = row_number() over (partition by [Code] order by [Company] desc) 
         From YourTable
       ) A
 Where (Company<>'***' and RN=1)
    or RN>1

返回

ID  Code    Company
2   EW10    DEU
3   EW10    DEC
4   EW20    ***
5   EW30    DEU
6   EW40    DEC
SELECT id, Code, Company
FROM table main
WHERE
(
    main. Company = '***'
    AND NOT EXISTS (
        SELECT code
        FROM table sub
        WHERE main.code = sub.code
        AND main.id != sub.id
    )
) OR company != '***'

希望能满足您的需求! :)

这将为您工作。

Select * from tablename Where Company <> '***' 
UNION 
Select * from tablename  Where Code Not In ( Select Code from #Sejaltable Where Company <> '***')

您可以在两种情况下使用“或”操作:

SELECT *
FROM   YourTable yt1
WHERE  Company <> '***'
   OR  NOT EXISTS (
          SELECT 1 FROM YourTable yt2
          WHERE yt2.Code = yt1.Code AND yt2.Company <> '***'
       )

您想对行进行排名; 公司级行胜过企业级行。 为此请使用RANKROW_NUMBER

如果您正在寻找一家特定的公司,则可以对行进行排名:

select *
from
(
  select 
    mytable.*,
    row_number() over (partition by code
                       order by case when company = '***' then 2 else 1 end) as rnk
  from mytable
  where company in ('DEC', '***')
) ranked
where rnk = 1
order by code;

对于所有公司,我建议先选择公司,然后加入他们的比赛:

select *
from
(
  select 
    c.company, m.id, m.code, m.company as setting_company,
    row_number() over (partition by c.company, m.code
                       order by case when m.company = '***' then 2 else 1 end) as rnk
  from (select distinct company from mytable where company <> '***') c
  left join mytable m on m.company = c.company or m.company = '***'
) results
where rnk = 1
order by company, code;

演示: https : //dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=bd8dd25c81f35f91043dcf0a0a1743c9 (此演示还包含编写查询的另一种方法。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM