识别不同用户的相同金额

时间:2018-08-13 10:29:18

标签: sql sql-server tsql

请考虑下表Orders

OrderID  Name    Amount
-----------------------
 1       A          100
 2       A            5
 3       B           32
 4       C         4000
 5       D          701
 6       E           32
 7       F          200
 8       G          100
 9       H           12
10       I           17
11       J          100
12       J          100
13       J           11
14       A            5

对于每个唯一的“金额”,我需要确定是否有2个或更多已订购该确切金额的用户,然后列出这些订单的详细信息。因此,所需的输出将是:

OrderID  Name  Amount
---------------------
 1       A        100
 8       G        100
11       J        100
12       J        100
 3       B         32
 6       E         32
  • 请注意,用户A已订购了2倍于5的订单(订单214),但这不应原样出现在输出中同一用户内。仅当另一个用户下达5的订单时,它才应该出现在输出中。

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

我只会使用exists

select o.*
from orders o
where exists (select 1
              from orders o2
              where o2.amount = o.amount and o2.name <> o.name
             );

答案 1 :(得分:2)

您可以:

job_pid = Application.get_env(:exq, :name) |> Exq.Api.Server.server_name
job_id = "935440ba-44c7-47d6-a973-2e23860dc54c"
{:ok, queued_for_retry_job} =  Exq.Api.find_retry(job_pid, job_id)

Exq.Api.find_failed(job_pid, job_id)
Exq.Api.find_jobs(job_pid, "queue" ,job_id)

答案 2 :(得分:1)

如果您只想选择字段,那么

    SELECT Amount,name,
     count(*) AS c
    FROM TABLE
    GROUP BY Amount, name
    HAVING c > 1
    ORDER BY c DESC

如果要整行显示

select * from table where Amount in (
  select Amount, name from table
  group by Amount, name having count(*) > 1)