English 中文(简体)
Sql query to 回去一张表格中每个组合的单一记录
原标题:Sql query to return one single record per each combination in a table

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

因此,我基本上需要具有微型价值的通道。 e. 例如,从A到B,需要微型价值和补贴。

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

结果是:

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!

最佳回答

我理解这个问题,这将发挥作用:

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

然而,如果您对从ID到ID的组合具有重复价值,例如:

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

0.1         A         B          2

0.1         A         B          4

这将使两行都返回。

如果你在2005年或以后使用Kall-Server,你希望像上文所述的重复浏览器能够使用:

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

如果你不希望重复行,你可以这样做:

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
问题回答

你们怎么做更简单吗?

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

或者也许我会误解这个问题。

EDIT: 包括住宿

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签