如何在SQL中编写查询,只获取表中特定的组合?

huangapple go评论69阅读模式
英文:

How can I write the query in SQL of only certain combinations in a table?

问题

我必须创建一个查询,只提取那些仅在星期一、星期三和星期五拜访的客户,或者这些组合:星期一-星期三、星期一-星期五、星期三-星期五、星期一-星期三-星期五、仅星期一、仅星期三、仅星期五。我希望输出结果如下图所示 如何在SQL中编写查询,只获取表中特定的组合?

如何在SQL中编写这个查询?

英文:

I have a table where the days that certain clients must be visited are recorded (Monday, Tuesday, Wednesday, Thursday and Friday), I attach this to illustrate:

如何在SQL中编写查询,只获取表中特定的组合?

I must create a query that only extracts the clients that are visited only on the days (Monday, Wednesday and Friday), or combinations such as: Monday-Wednesday, Monday-Friday, Wednesday-Friday, Monday-Wednesday-Friday, only Monday, only Wednesday, only Friday. I looking forward the output like this image 如何在SQL中编写查询,只获取表中特定的组合?

How can I write this in SQL?

答案1

得分: 1

假设您的数据确切地按描述出现,那么您只需要筛选出单独的行。您对组合本身并不感兴趣,只想要那些所有访问都包含在星期一、星期三和星期五三天内的客户。反过来说,您想要根据其他四天来取消资格。写出最简洁的方式如下:

select * from T
where 'x' not in ('Tuesday', 'Thursday', 'Saturday', 'Sunday');

我假设这些标志都不是表示为空的。如果是这样的话,只需展开成更长的形式:

where coalesce(Tuesday, '') <> 'x'
and coalesce(Thursday, '') <> 'x'
and coalesce(Saturday, '') <> 'x'
and coalesce(Sunday, '') <> 'x';

或者等效且更加明确:

where Tuesday is null and Thursday is null and Saturday is null and Sunday is null;
英文:

Assuming your data appears exactly as described then you just need to filter out individual rows. You're not interested in combinations per se you just want only clients that have all of their visitation contained within the three days Monday, Wednesday, and Friday. Flipping that around then you want to disqualify based on the other four days. The most concise way to write that is this:

select * from T
where &#39;x&#39; not in (Tuesday, Thursday, Saturday, Sunday);

I'm assuming that none of these flags are represented are nulls. If that's the case then just expand to a longer form:

where   coalesce(Tuesday, &#39;&#39;)  &lt;&gt; &#39;x&#39;
    and coalesce(Thursday, &#39;&#39;) &lt;&gt; &#39;x&#39;
    and coalesce(Saturday, &#39;&#39;) &lt;&gt; &#39;x&#39;
    and coalesce(Sunday, &#39;&#39;)   &lt;&gt; &#39;x&#39;;

or equivalently and more to the point:

where Tuesday is null and Thursday is null and Saturday is null and Sunday is null;

答案2

得分: 0

这可以翻译为:选择未在周二、周四、周六或周日访问的客户。

您可以使用 group byhaving 子句来实现此目标:

select client
from mytable
group by client
having sum(case when Tuesday = 'x' then 1 else 0 end) = 0
       and sum(case when Thursday = 'x' then 1 else 0 end) = 0
       and sum(case when Saturday = 'x' then 1 else 0 end) = 0
       and sum(case when Sunday = 'x' then 1 else 0 end) = 0

如果每个客户只有一行数据,您可以简单地按如下方式执行:

select *
from mytable
where Tuesday = ''
       and Thursday = '' 
       and Saturday = '' 
       and Sunday = ''

在 SQL Server 上的演示

英文:

This can be translated to : select clients who are not visited Tuesday,Thursday, Saturday or Sunday.

You can use group by and having clause to achieve this :

select client
from mytable
group by client
having sum(case when Tuesday = &#39;x&#39; then 1 else 0 end ) = 0
       and sum(case when Thursday = &#39;x&#39; then 1 else 0 end ) = 0
       and sum(case when Saturday = &#39;x&#39; then 1 else 0 end ) = 0
       and sum(case when Sunday = &#39;x&#39; then 1 else 0 end ) = 0

If there is only one row per client then you can simply do it as follows :

select *
from mytable
where Tuesday = &#39;&#39;
       and Thursday = &#39;&#39; 
       and Saturday = &#39;&#39; 
       and Sunday = &#39;&#39;

Demo on sql server

答案3

得分: 0

我用这个查询解决了这个问题:

SELECT Client.Number,
    CASE 
        WHEN Cliente.MONDAY = 'X' 
        THEN (CASE WHEN Cliente.TUESDAY = 'X' OR Cliente.THURSDAY = 'X' OR 
        Cliente.SATURDAY = 'X' OR Cliente.SUNDAY = 'X' THEN 0
                ELSE 1 END)
        WHEN Cliente.WEDNESDAY = 'X'  
        THEN (CASE WHEN Cliente.TUESDAY = 'X' OR Cliente.THURSDAY = 'X' OR 
        Cliente.SATURDAY = 'X' OR Cliente.SUNDAY = 'X' THEN 0
                ELSE 1 END)
        WHEN Cliente.FRIDAY = 'X'  
        THEN (CASE WHEN Cliente.TUESDAY = 'X' OR Cliente.THURSDAY = 'X' OR 
        Cliente.SATURDAY = 'X' OR Cliente.SUNDAY = 'X' THEN 0
                ELSE 1 END)
        ELSE 0
    END Resultado
FROM TableClients Client

因为我也对不出现其他日期的组合很感兴趣。

英文:

I solve the problemn with this query:

SELECT Client.Number,
CASE 
WHEN Cliente.MONDAY = &#39;X&#39; 
THEN (CASE WHEN Cliente.TUESDAY = &#39;X&#39; OR Cliente.THURSDAY = &#39;X&#39; OR 
Cliente.SATURDAY = &#39;X&#39; OR Cliente.SUNDAY = &#39;X&#39; THEN 0
		ELSE 1 END)
WHEN Cliente.WEDNESDAY = &#39;X&#39;  
THEN (CASE WHEN Cliente.TUESDAY = &#39;X&#39; OR Cliente.THURSDAY = &#39;X&#39; OR 
Cliente.SATURDAY = &#39;X&#39; OR S2.SUNDAY = &#39;X&#39; THEN 0
		ELSE 1 END)
WHEN Cliente.FRIDAY = &#39;X&#39;  
THEN (CASE WHEN Cliente.TUESDAY = &#39;X&#39; OR Cliente.THURSDAY = &#39;X&#39; OR 
Cliente.SATURDAY = &#39;X&#39; OR Cliente.SUNDAY = &#39;X&#39; THEN 0
		ELSE 1 END)
ELSE 0
END Resultado
from TableClients Client

Because also to me I am interesting about combinations in where not appear the others days.

答案4

得分: 0

我将代码部分排除,只返回翻译好的内容:

我将参数作为表格添加,所以你可以根据表格行的任何组合来进行操作,@d表格中的数据。(你可以将其放入存储过程,输入参数为用户表格变量)

在示例中,我选择了星期一和星期日,结果如下图所示:

如何在SQL中编写查询,只获取表中特定的组合?

英文:

i add parameters as table, so you can make any combination base on table rows in @d table. (you can put in stored proc with input parameter as user table variable)

declare @t table (
  client varchar(20),
  Monday bit, 
  Tuesday bit, 
  Wednesday bit,  
  Thursday bit, 
  Friday bit, 
  Saturday bit, 
  Sunday bit
)

insert into @t values
(&#39;client 1&#39;, 1, 0, 0, 0, 0, 0, 0),
(&#39;client 2&#39;, 0, 0, 1, 0, 0, 0, 1),
(&#39;client 3&#39;, 1, 0, 1, 0, 1, 0, 0),
(&#39;client 4&#39;, 1, 0, 1, 0, 0, 0, 0),
(&#39;client 5&#39;, 0, 1, 1, 1, 1, 0, 0),
(&#39;client 6&#39;, 0, 1, 1, 1, 1, 0, 0);

declare @d table (Days varchar(20))
insert into @d values (&#39;Monday&#39;), (&#39;Sunday&#39;)


select * 
from @t t
where 
	t.Monday = case when exists (select top 1 Days from @d where days = &#39;Monday&#39;) then 1 else null end
or t.Tuesday = case when exists (select top 1 Days from @d where days = &#39;Tuesday&#39;) then 1 else null end  
or t.Wednesday = case when exists (select top 1 Days from @d where days = &#39;Wednesday&#39;) then 1 else null end  
or t.Thursday = case when exists (select top 1 Days from @d where days = &#39;Thursday&#39;) then 1 else null end
or t.Friday = case when exists (select top 1 Days from @d where days = &#39;Friday&#39;) then 1 else null end
or t.Saturday = case when exists (select top 1 Days from @d where days = &#39;Saturday&#39;) then 1 else null end
or t.Sunday = case when exists (select top 1 Days from @d where days = &#39;Sunday&#39;) then 1 else null end

in example i choose Monday and Sunday
result would be

如何在SQL中编写查询,只获取表中特定的组合?

huangapple
  • 本文由 发表于 2023年7月14日 03:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定