我有一个存储客户端的表,然后将这些客户端分组在一个被视为该组“头部”的客户端下。因此,如果客户出现在“群组”列中,即使他们可能不属于他们自己的“群组”,也会被视为“头”。该表可能如下所示:
+--------+-------+------+
| Client | Group | Head |
+--------+-------+------+
| ABC | ABC | Yes |
| DEF | ABC | No |
| GHI | GHI | Yes |
| JKL | MNO | Yes |
| MNO | PQR | Yes |
| PQR | MNO | No |
| STU | STU | Yes |
+--------+-------+------+
在这里我们可以看到客户端JKL和PQR的'Head'记录不正确。我需要的是一个列表,其中只有“Head”列不正确且应该是什么的客户(Yes / 1或No / 0)。这样做的最佳方法是什么?
答案 0 :(得分:0)
使用SQL select语句WHERE和double equal sign ==
select * from tableName where Head=="No";
您可以将记录更新为您喜欢的内容 ;
UPDATE tableName
SET Head = 0
WHERE Head="No";
希望这有帮助。
答案 1 :(得分:0)
declare @t table(Client varchar(50), [Group] varchar(50), Head varchar(50))
insert into @t values( 'ABC','ABC','Yes'),
('DEF','ABC','No'),
('GHI','GHI','Yes'),
('JKL','MNO','Yes'),
('MNO','PQR','Yes'),
('PQR','MNO','No'),
('STU','STU','Yes')
select * from @t t1
where t1.client not in (select distinct [Group] from @t t3 where t3.Head = 'Yes' and t3.Client = t3.[group])
--and Head = 'Yes' --please uncomment this line and check the result, if any issue to desire your result, tell me
答案 2 :(得分:0)
Declare @a Table (Client Varchar(10),[Group] Varchar(10),Head Varchar(3))
Insert into @a Values ('ABC','ABC','YES')
Insert into @a Values ('DEF','ABC','No')
Insert into @a Values ('GHI','GHI','YES')
Insert into @a Values ('JKL','MNO','YES')
Insert into @a Values ('MNO','PQR','YES')
Insert into @a Values ('PQR','MNO','No')
Insert into @a Values ('STU','STU','YES')
-- uncomment here to get only wrong ones
--Select * from (
Select a.*, Coalesce(h.RealHead,'No') as RealHead
, CASE WHEN Coalesce(h.RealHead,'No')<>a.Head then 'ERROR' else 'OK' end as Info
FROM @a a
LEFT JOIN (Select Distinct [Group], 'YES' as RealHead from @a) h
ON h.[GROUP]=a.Client -- Join real Heads with clients
-- uncomment here to get only wrong ones
--) s where Info='ERROR'