Sql query to return one single record per each combination in a table

怎甘沉沦 提交于 2019-12-31 04:39:04

问题


I need the result for every combination of (from_id, to_id) which has the minimun value and the loop matching a criteria.

So basically I need the loop that has the minimun value. e.g. From A to B i need the minimun value and the loop_id .

The table has the following fields:

value     from_id   to_id     loop_id
-------------------------------------

2.3         A         B          2

0.1         A         C          2

2.1         A         B          4

5.4         A         C          4

So a result will be:

value     from_id   to_id     loop_id
-------------------------------------

2.1         A         B          4

0.1         A         C          2

I have tried with the following:

SELECT t.value, t.from_id, t.to_id,t.loop_id
FROM myresults t 
INNER JOIN (
     SELECT min(m.value), m.from_id, m.to_id, m.loop_id
     FROM myresults m where m.loop_id % 2 = 0
     GROUP BY m.from_id, m.to_id, m.loop_id
) x
   ON  (x.from_id = t.from_id and x.to_id=t.to_id and x.loop_id=t.loop_id )
   AND x.from_id = t.from_id and x.to_id=t.to_id and x.loop_id=t.loop_id

But it is returning all the loops. Thanks in advance!


回答1:


As I understand the problem this will work:

SELECT  t.value, t.from_id, t.to_id, t.loop_id
FROM    MyResults t
        INNER JOIN
        (   SELECT  From_ID, To_ID, MIN(Value) [Value]
            FROM    MyResults
            WHERE   Loop_ID % 2 = 0
            GROUP BY From_ID, To_ID
        ) MinT
            ON MinT.From_ID = t.From_ID
            AND MinT.To_ID = t.To_ID
            AND MinT.Value = t.Value

However, if you had duplicate values for a From_ID and To_ID combination e.g.

value     from_id   to_id     loop_id
-------------------------------------

0.1         A         B          2

0.1         A         B          4

This would return both rows.

If you are using SQL-Server 2005 or later and you want the duplicate rows as stated above you could use:

SELECT  Value, From_ID, To_ID, Loop_ID
FROM    (   SELECT  *, MIN(Value) OVER(PARTITION BY From_ID, To_ID) [MinValue]
            FROM    MyResults
        ) t
WHERE   Value = MinValue

If you did not want the duplicate rows you could use this:

SELECT  Value, From_ID, To_ID, Loop_ID
FROM    (   SELECT  *, ROW_NUMBER() OVER(PARTITION BY From_ID, To_ID ORDER BY Value, Loop_ID) [RowNumber]
            FROM    MyResults
        ) t
WHERE   RowNumber = 1



回答2:


Can't you do this a lot more simply?

SELECT
  from_id,
  to_id,
  MIN(value)
FROM
  myresults
WHERE
  loop_id % 2 = 0
GROUP BY
  from_id,
  to_id

Or maybe I'm misunderstanding the question.

EDIT: To include loop_id

SELECT
  m2.from_id,
  m2.to_id,
  m2.value,
  m2.loop_id
FROM
  myresults m2 INNER JOIN
    (SELECT
      m1.from_id,
      m1.to_id,
      MIN(m1.value)
    FROM
      myresults m1
    WHERE
      m1.loop_id % 2 = 0
    GROUP BY
      m1.from_id,
      m1.to_id) minset
  ON
        m2.from_id = minset.from_id
    AND m2.to_id = minset.to_id
    AND m2.value = minset.value


来源:https://stackoverflow.com/questions/10311152/sql-query-to-return-one-single-record-per-each-combination-in-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!