显示 if 子句是否从 SQL 找到结果
Displaying if clause finds results or not from SQL
我在 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 的条件:Select ID,???作为 tableexample 中的 checkfoundentry where 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
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)
我使用 innerquery 来计算您指定的过滤器...
可以用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;
你也可以像下面这样使用子查询,
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
我在 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 的条件:Select ID,???作为 tableexample 中的 checkfoundentry where 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
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)
我使用 innerquery 来计算您指定的过滤器...
可以用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;
你也可以像下面这样使用子查询,
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