![](/img/trans.png)
[英]Exclude columns where all rows in that column are null or equal to 0 in LINQ
[英]Find rows where columns are the same but exclude where another column is equal to X
我有一个表,其中包含以下列 active, timeUpdated, firstName, lastName, Property
我需要显示属性重复且活动设置为 1 的完整行。
到目前为止,我已经找到了一个语句来显示重复列的行,但我无法弄清楚在哪里放置 WHERE 语句以仅显示活动行
select * from [Table] where property in (
select property from [table]
group by property having count(*) > 1
)
Order by property DESC
此外,我需要搜索活跃的重复名字和姓氏,以确保它们在多个属性中不活跃。
感谢您对此提供的任何帮助,如果之前已回答此问题,我深表歉意。 我无法找到它。 如果有什么不同,我正在使用 Microsoft SQL Server Management Studio
根据要求,示例数据类似于(Active、DateUpdated、First、Last、propertyID)
1, 2018-09-17, John, Doe, 1408
1, 2018-10-20, 艾米丽, 史密斯 1408
0, 2018-11-15, 格雷格·琼斯 1408
1, 2018-12-01, 理查德·史密斯 1209
我希望它显示 John Doe 和 Emily 的结果,因为他们都活跃并且共享一个属性 ID
对于“附加”,它就像
1, 2018-09-17, John, Doe, 1408
1, 2018-10-20, 艾米丽, 史密斯 1408
0, 2018-11-15, 格雷格·琼斯 1408
1, 2018-12-01, 理查德·史密斯 1209
1, 2018-10-17, John, Doe, 1103
0, 2018-3-17, John, Doe, 1001
我希望它返回活跃的 John Doe
我需要显示属性重复且活动设置为 1 的完整行。
您可以使用exists
:
select t.*
from t
where exists (select 1
from t t2
where t2.property = t.property and t.active = 1
);
CREATE TABLE Test (
FirstName varchar(50),
LastName varchar(50),
Property varchar(50),
Active int
);
INSERT INTO Test(FirstName, LastName, Property, Active)
VALUES
('Jitendra', 'Gupta', 'MyProperty', 1),
('Jitendra', 'Gupta', 'MyProperty', 1),
('Jay', 'Kumar', 'YourProperty', 0),
('Vinod', 'Singh', 'YourProperty', 0),
('Amit', 'Sharma', 'HisProperty', 1),
('Manoj', 'Yadav', 'HerProperty', 1),
('Pankaj', 'Gupta', 'OurProperty', 1);
WITH T1 (Property, DuplicateCount)
AS
(
SELECT Property,
ROW_NUMBER() OVER(PARTITION BY Property ORDER BY Property) AS DuplicateCount
FROM Test
WHERE Active = 1
)
SELECT *
FROM Test
WHERE Property IN (SELECT Property FROM T1 WHERE DuplicateCount > 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.