需要大于1比1的结果

时间:2012-03-06 15:39:44

标签: sql sql-server sql-server-2008 sql-server-2005 tsql

我有一个查询,我正在下拉匹配到某个国家/地区的用户的结果,这是我目前的查询:

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID

但我想要做的就是让它只返回一个国家有多个用户ID。所以,如果我的数据是。

Bob   United States
Tom   United States
Steve United Stated

Frank Canada
Billy Canada

Lou   China

我希望结果是

Bob
Tom
Steve

表示美国,

Frank
Billy

对于加拿大而言,并没有拉下中国,因为它只有一个用户。但是拉下另一个因为美国和加拿大有创造者然后有1个用户绑定它。这可以在查询中实现吗?

谢谢!

7 个答案:

答案 0 :(得分:2)

试试这个:

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser CU
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
WHERE (SELECT COUNT(CountryUser.[User ID])
       FROM dbo.CountryUser
       WHERE [Foreign ID] = CU.[Foreign ID]) > 1

我无法解析语句以确保语法正确,但请尝试告诉我。

答案 1 :(得分:2)

我不确定没有数据库的语法在我面前,但排名功能对此有好处:

SELECT * FROM
(
SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  Country.Name,
  Country.ID,
  COUNT(UserColder.ContactName) OVER (PARTITION BY Country.ID) _RANK
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
) A
WHERE A._RANK > 1

编辑感谢评论:)还添加了概念证明:

SELECT * 
INTO #TEMP
FROM
(
SELECT 'UK' Country, 'Dave' Name
UNION
SELECT 'UK' Country, 'Bob' Name
UNION
SELECT 'UK' Country, 'Alex' Name
UNION
SELECT 'China' Country, 'Mike' Name
UNION
SELECT 'Holland' Country, 'Paul' Name
UNION
SELECT 'Holland' Country, 'James' Name
) A

SELECT * FROM #TEMP

SELECT * FROM
(SELECT Country, Name, Count(Name) OVER (PARTITION BY Country) _RANK FROM #TEMP) A
WHERE A._RANK > 1

答案 2 :(得分:2)

如果存在具有相同国家/地区ID但具有不同用户ID的另一行,则以下将仅从CountryUser中选择行。即该国家/地区的多个用户ID:

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
WHERE
  EXISTS
  (SELECT
    NULL
   FROM
    CountryUser CU2
   WHERE
    CU2.[Foreign ID] = CountryUser.[Foreign ID]
    AND CU2.[User ID] <> CountryUser.[User ID])

答案 3 :(得分:0)

如果您必须提取用户信息,这会变得乏味,但是这会有用吗?它将返回国家列表。可以添加用户信息,但查询有点粗略

SELECT
  COUNT(*) count,
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
GROUP BY Country.ID, Country.Name
HAVING COUNT(*) > 1

答案 4 :(得分:0)

您可以使用HAVING子句

举个例子:     SELECT SalesOrderID,SUM(LineTotal)AS SubTotal     FROM Sales.SalesOrderDetail     GROUP BY SalesOrderID     拥有和(LineTotal)&gt; 100000.00     按SalesOrderID订购

答案 5 :(得分:0)

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
where Country.ID in (select CountryID from CountryUser group by CountryID having count(countryid)>1)

答案 6 :(得分:0)

;WIth countries AS
(
SELECT
UserColder.ContactName
,CountryUser.[User ID],Country.Name,Country.ID 
,COUNT(UserColder.ContactName) OVER (PARTITION BY Country.ID) AS num_people
FROM dbo.Country
INNER JOIN dbo.CountryUser  
ON Country.ID = CountryUser.[Foreign ID]
INNER JOIN dbo.UserColder  
ON CountryUser.[User ID] = UserColder.ID 
)
SELECT
ContactName
,[User ID]
FROM countries
WHERE num_people > 1