English 中文(简体)
几种情况
原标题:Distinct with several conditions
  • 时间:2011-01-28 20:01:40
  •  标签:
  • sql
  • distinct

我有几栏数据基:

SightingID, PA1, PA2, Area, PlotID.

不同的电离层扰动(与PA1、PA2和相关的区域)都有相同的定位。 我想从几个条件中挑选出每个PlotID(即一行)一个SightingID(即获得一个拥有独特PlotID的DF)。

如果同一地有几处SightingID,则选择SightingID:

  1. First selecting the SightingID which have PA1=O, if there are several SightingID with PA1=0 with the same PlotID
  2. then select the one with the maximum value for Area.
  3. If for one PlotID, there is no SightingID with PA1=0, just take the one with the maximum value for Area.

我来了。

SELECT SightingID, PA1, PA2, PlotID, MAX(Area) FROM DF GROUP BY PlotID

我获得了与SightingID不同的PlotID,后者具有最高的地区价值,但我不知道如何纳入我的第一个条件。

我知道我应当使用<代码>。 区别fonction,但我不知道如何与它一起创造条件。 如果它有点混淆,但如果有人能帮助我,那将是巨大的。 增 编

问题回答

Group By can be used instead of distinct. Depending on the SQL engine, it may even rewrite the query plan to be the same.

以下两个问题在理论上相同:

SELECT DISTINCT a, b FROM foo;

and

SELECT a, b FROM foo GROUP BY a, b;

然而,如果你重新努力达到最大价值,你需要使用小组。 我不敢肯定你的问题是什么。

它没有完全清楚你想要什么。 如果你想选择与最大区域相关的SightingID,但希望与PA1=0记录,首先就是你如何这样做。

SELECT 
  case when maxPlotZero.Area is null then
          maxPlot.SightingID
        else 
          maxPlotZero.SightingID
  end SightingID

FROM 
 DF
 LEFT JOIN

 (

   SELECT 


         PlotID, 
         MAX(Area) Area
   FROM 
           DF
    WHERE
        PA1=0

    GROUP BY 
        PlotID
)  maxPlotZero

ON df.PlotId= maxPlotZero.PlotID
   and df.Area= maxPlotZero.Area
LEFT JOIN

(

SELECT 



         PlotID, 
         MAX(Area) 
FROM 
       DF

GROUP BY 
    PlotID
)  maxPlot

  ON df.PlotId= maxPlot.PlotID
   and df.Area= maxPlot.Area




相关问题
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: ...

热门标签