该查询的输出应该是什么

时间:2019-06-08 11:46:14

标签: sql oracle11g oracle10g

显示在同一地点的推销员ID,推销员姓名和位置。

我已经使用自我连接解决了

package main

import (
    "fmt"
    "gopkg.in/yaml.v2"
)

type TestStruct struct {
    FieldA string `yaml:"a"`
    FieldB string `yaml:"b"`
}

func main() {
    input := []byte(`{a: 1}`)
    var output TestStruct

    _ = yaml.Unmarshal(input, &output)
}
select s.sid, s.sname, s.location 
from salesman s 
inner join salesman ss on s.location = ss.location 
where s.location=ss.location

预期产量

Salesman  Table

SID SNAME   LOCATION
1   Peter   London
2   Michael Paris
3   John    Mumbai
4   Harry   Chicago
5   Kevin   London
6   Alex    Chicago

4 个答案:

答案 0 :(得分:0)

您可以使用子查询来查找重复的位置,然后在主查询中找到所有在这些位置上的推销员,如:

select *
from salesman
where location in (
  select location
  from salesman s
  group by location
  having count(*) > 1 
)

答案 1 :(得分:0)

具有EXISTS:

select s.*
from salesman s
where exists (
  select 1
  from salesman
  where sid <> s.sid and location = s.location
) 

答案 2 :(得分:0)

我假设您对在预期输出中显示的序列并使用连接要快得多的顺序并不特别。

select s.sid, s.sname, s.location 
from salesman s 
inner join (
  select location from salesman group by location having count(*) >1
) ss on s.location = ss.location 

答案 3 :(得分:0)

大概是这样的:

select s.sid, s.sname, s.location 
from salesman s 
inner join salesman ss 
on s.sid <> ss.sid
and s.location = ss.location