【问题标题】:Displaying if clause finds results or not from SQL显示 if 子句是否从 SQL 中找到结果
【发布时间】:2021-12-22 20:47:15
【问题描述】:

我在 tableexample 中有以下示例数据(我使用 MSSQL):

ID date tag
15551 2021-11-10 1
15551 2021-11-09 0
15551 2021-11-10 1
12123 2021-11-09 1
12123 2021-11-09 1
15551 2021-11-10 1
12123 2021-11-10 1
74141 2021-11-10 1
12345 2021-11-10 1
11111 2021-11-10 1
74141 2021-11-10 1
12345 2021-11-10 0

现在我想获取一组 ID (15551,12123,12345,74141) 的信息,如果它们包含至少一个满足条件的条目:日期 今天 (2021-11-10) 和标签= 1

所以我的这个例子的结果应该是这样的:

ID checkfoundentry
15551 0
12123 1
74141 0
12345 0

解释:12123 和 15551 包含昨天 (2021-11-09) 的日期,但 15551 包含标签 = 0 的日期。因此只有 12123 至少满足两个条件。

所以我很容易将它们分组在一起,但我不知道如何检查分组 ID 的条件:选择 ID,???作为 tableexample 中的 checkfoundentry 其中 ID in (15551,12123,12345,74141) Group By ID

这样可以吗?

这里是可以提供示例数据的sql:

Create Table table1 (
colID int,
coldate date,
coltag int
);

Insert Into table1 (colID, coldate, coltag)
values (15551, '2021-11-10', 1),
(15551, '2021-11-09', 0),
(15551, '2021-11-10', 1),
(12123, '2021-11-09', 1),
(12123, '2021-11-09', 1),
(15551, '2021-11-10', 1),
(12123, '2021-11-10', 1),
(74141, '2021-11-10', 1),
(12345, '2021-11-10', 1),
(11111, '2021-11-10', 1),
(74141, '2021-11-10', 1),
(12345, '2021-11-10', 0),
(12345, '2021-11-10', 1)

我找到了一个具体的解决方案,你能告诉我这是否有用吗?

Select ID, (CASE when (Select sum(Tag) from table1 t where date <> 2021-11-10 and tag = 1 and s.ID = t.ID Group By ID) > 0 then 1 Else 0 END) as checkfoundentry from table1 s Group By ID

【问题讨论】:

  • 我添加了一个答案,但请添加一个 sql fiddle,以便我们在发布答案之前进行测试;)

标签: sql-server grouping contains


【解决方案1】:
select t.ID ,
(select  count(*)
from tbl t2 
where t2.ID = t.ID and 
t2.date = cast(getdate() as date) and t2.tag = 1) checkfoundentry 
from tbl t 
where t.ID in (15551,12123,12345,74141)

我使用内部查询来计算您指定的过滤器...

【讨论】:

  • 谢谢!我试图在我的解决方案中使用您的想法。因为你计算找到的条目,你也会得到大于 2 的数字。这没问题,但我试图通过一个 case 语句来避免这种情况,将其减少到 0 和 1。
  • 是的。比如:如果 count(*) > 0 then 1 else 0 end 会有帮助
【解决方案2】:

你可以使用window functions来统计每个ID的条件

with x as (
    select * ,
        Count(*) over(partition by id) cnt, 
        Sum(tag) over(partition by id) tg, 
        Count(case when date !='2021-11-10' then 1 end) over(partition by id) dt
    from t
    where t.ID in (15551,12123,12345,74141)
)
select distinct id, 
    case when dt>0 and cnt=tg then 1 else 0 end CheckFoundEntry
from x;

【讨论】:

  • 谢谢,我想如果你计算 id 15551 的标签,你会得到 4,但如果你把它们相加,你会得到 3。所以如果你要从 2021-11-09 日期切换 0 标签到 1,从 2021-11-10 日期到 0。您当然会再次找到 cnt 4 和 tg 3,但现在 dt>0 尽管案例仍然会为 15551 产生 0
【解决方案3】:

您也可以使用下面的子查询,

SELECT sq.colID,
    MAX(sq.checkfoundentry) AS checkfoundentry
FROM(
    SELECT t.colID
       , t.coldate
       , t.coltag
       , CASE WHEN t.coldate <> cast(GETDATE() AS date) AND t.coltag = 1 THEN 1 ELSE 0 END AS  checkfoundentry
       --, CASE WHEN ((t.coldate <> cast(getdate() AS date)) OR t.coltag)  1 else 0 end --as checkfoundentry
    FROM dbo.table1 t
    WHERE t.colID IN (15551,12123,12345,74141)
) AS sq
GROUP BY sq.colID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多