标识组在多个sql列中具有相同的值

hlswsv35  于 2021-07-24  发布在  Java
关注(0)|答案(4)|浏览(354)

我试图找出如何编写一个sql查询代码,该查询将标识一个表中的一个组,该表在多个列上的组中的记录中包含相同的值。
例如,给定以下示例表:

我希望在本案中找出所有审判都有相同起点和终点的人的名字。根据数据,只有詹姆斯和弗兰克符合这个标准。我不想过滤和获取查询输出,只是给我所有的组与“是”或“否”。我确实希望查询输出整个表,并适当地标记组。怎么办?
下面是我能看到的。我确实看到,当我分组时,在感兴趣的列中显示所有相同值的组会折叠成一个组。但是如果我加入一个新的组,其中有多个行(试验),其中每个试验可能有相同的起点和终点,但该组的不同试验不是相同的点,那么实际上该组应该产生“否”,即使每个单独的试验显示相同的起点和终点。

CREATE TABLE Trial
(Name nvarchar(100),
Trial nvarchar(10),
Start nvarchar(100),
[End] nvarchar(100))

INSERT INTO Trial

VALUES
('Julie', 'A', 'Ranch St', 'Ranch St'),
('Julie', 'B', 'Ranch St', 'North Blvd'),
('James', 'A', 'Deer Rd', 'Deer Rd'),
('James', 'B', 'Deer Rd', 'Deer Rd'),
('Emily', 'A', 'Oak St', 'Oak St'),
('Emily', 'B', 'Oak St', 'Elm Rd'),
('Frank', 'A', 'Main Ave', 'Main Ave'),
('Frank', 'B', 'Main Ave', 'Main Ave'),
('Frank', 'C', 'Main Ave', 'Main Ave'),
('Jeff', 'A', 'Main Ave', 'Main Ave'),
('Jeff', 'B', 'Oak St', 'Oak St')

SELECT
    a.[Name]
    ,CASE
    WHEN a.[Start] = a.[END] THEN 'Yes' ELSE 'No'
    END AS 'TrialChk'

FROM Trial AS a

GROUP BY a.[Name]
        ,a.[Start]
        ,a.[End]

ORDER BY Name ASC;

我在上面的示例表中没有jeff试验,但是jeff会对每个试验都给出“是”,但是由于所有的点都不相同,所以组的输出应该是“否”。

neekobn8

neekobn81#

简单使用 case 具有窗口功能:

select t.*,
       (case when min(start) over (partition by name) = max(start) over (partition by name) and
                  min(end) over (partition by name) = max(end) over (partition by name) and
                  start = end
             then 'Yes' else 'No'
        end) as flag
from trial t;

这是一把小提琴。
注: end 是一个非常糟糕的列名称,因为它是一个sql关键字。

jk9hmnmh

jk9hmnmh2#

你可以试试窗口功能 Row_Number() 如下图所示:

Create table MyTable (Name varchar(50),
Trial Char(1),
Start Varchar(20),
[End] Varchar(20))

Insert into MyTable Values
('Julie', 'A', 'Ranch St', 'Ranch St'),
('Julie', 'B', 'Ranch St', 'North Blvd'),
('James', 'A', 'Deer Rd', 'Deer Rd'),
('James', 'B', 'Deer Rd', 'Deer Rd')

;with cte as (Select name
,trial
,start
,[End]
,Row_number() Over(Partition By [Start], [End] order by [Start], [End]) as SrNo
From MyTable
)
Select 
   name, trial, start, [End], 'Yes' as [Expected Output] 
from cte where name in ( Select name from cte where SrNo > 1)
union all
Select 
   name, trial, start, [End], 'No' as [Expected Output] 
from cte where name not in (Select name from cte where SrNo > 1)
order by name

现场演示

y4ekin9u

y4ekin9u3#

检查给定的查询

DECLARE @Counter int  
DECLaRE @CMS_Query varchar(MAX)
DECLARE @total_Row int
DECLARE Cur_1 CURSOR FOR select Name from Trial group by Name

Create Table #MainTrial(
[Name] varchar(MAX),
Trial varchar(MAX),
[Start] varchar(MAX),
[END] varchar(MAX),
[TrialChk] varchar(3)
)
OPEN Cur_1
    DECLARE @Name varchar(MAX)
    FETCH NEXT FROM Cur_1 INTO @Name
        WHILE (@@FETCH_STATUS = 0)
            Begin

                SELECT  a.[Name],Trial,Start,[End],CASE WHEN a.[Start] = a.[END] THEN 'Yes' ELSE 'No'END AS 'TrialChk' into #Trial1 FROM Trial AS a  where Name = @Name

                select @total_Row = count(*) from #Trial1
                select @Counter = COUNT(*) from #Trial1 where TrialChk = 'Yes'

                IF @total_Row != @Counter
                    BEGIN
                        UPDATE #Trial1 SET TrialChk = 'No' where [Name] = @Name
                    END
                insert into #MainTrial select * from #Trial1
                DROP Table #Trial1
                FETCH NEXT FROM Cur_1 INTO @Name
            END
CLOSE Cur_1
DEALLOCATE Cur_1
select * from #MainTrial
drop table #MainTrial

w41d8nur

w41d8nur4#

如果你 GROUP BY Name 数一数 Start s和 End s、 就像这个查询一样:

SELECT Name, COUNT(DISTINCT Start) start_counter, COUNT(DISTINCT [End]) end_counter
FROM Trial
GROUP BY Name

你会得到这些结果:

> Name  | start_counter | end_counter
> :---- | ------------: | ----------:
> Emily |             1 |           2
> Frank |             1 |           1
> James |             1 |           1
> Jeff  |             2 |           2
> Julie |             1 |           2

因此,您可以将上述查询连接到表并设置 'Yes' 其中2个计数器都等于1,否则设置 'No' :

SELECT t.*,
  CASE WHEN c.start_counter = 1 AND c.end_counter = 1 THEN 'Yes' ELSE 'No' END Result
FROM Trial t 
INNER JOIN (
  SELECT Name, COUNT(DISTINCT Start) start_counter, COUNT(DISTINCT [End]) end_counter
  FROM Trial
  GROUP BY Name
) c ON c.Name = t.Name

请看演示。
结果:

> Name  | Trial | Start    | End        | Result
> :---- | :---- | :------- | :--------- | :-----
> Emily | A     | Oak St   | Oak St     | No    
> Emily | B     | Oak St   | Elm Rd     | No    
> Frank | A     | Main Ave | Main Ave   | Yes   
> Frank | B     | Main Ave | Main Ave   | Yes   
> Frank | C     | Main Ave | Main Ave   | Yes   
> James | A     | Deer Rd  | Deer Rd    | Yes   
> James | B     | Deer Rd  | Deer Rd    | Yes   
> Jeff  | A     | Main Ave | Main Ave   | No    
> Jeff  | B     | Oak St   | Oak St     | No    
> Julie | A     | Ranch St | Ranch St   | No    
> Julie | B     | Ranch St | North Blvd | No

相关问题