SELECT子查询中的ORACLE ORDER BY

时间:2017-02-21 07:32:02

标签: sql oracle sql-order-by

我想在子查询中选择一个varchar字段SUBJECT,按日期字段DATETO排序。具体:最新条目的ONE主题字段,因此按日期字段排序。

SELECT MDKAMVP.MDKAMDA_SID, 
(select SUBJECT from mdkndlst where rownum=1 and SUBJECT is not null 
order by DATETO) NAKTION, MDKAMVP.ROWID ROW_ID 
FROM SPRINGV2.MDKAMVP order by MDKAMVP.INDSTATUS

错误:

ORA-00907: Rechte Klammer fehlt
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:
Fehler in Zeile: 8 Spalte: 99

没有order by DATETO

4 个答案:

答案 0 :(得分:1)

Oracle 12c 中,您可以使用新的FETCH FIRST子句:

SELECT
    mdkamvp.mdkamda_sid,
    (SELECT subject
        FROM mdkndlst
        WHERE subject IS NOT NULL
        ORDER BY dateto
        FETCH FIRST 1 ROW ONLY) naktion,
    mdkamvp.rowid row_id
FROM springv2.mdkamvp
ORDER BY mdkamvp.indstatus

答案 1 :(得分:1)

在应用rownum子句之前已将ORDER BY分配到一行,因此此查询

SELECT subject
FROM   mdkndlst
WHERE  rownum = 1
       AND subject IS NOT NULL
ORDER  BY dateto

实际上返回一个随机行。我相信这不是你想要的。您可能希望首先应用ORDER BY然后取第一行。这就是子查询可以派上用场的地方:

SELECT subject
FROM   (SELECT subject
        FROM   mdkndlst
        WHERE  subject IS NOT NULL
        ORDER  BY dateto)
WHERE  rownum = 1

此类查询您现在可以用作标量子查询

SELECT mdkamvp.mdkamda_sid
      ,(SELECT subject
        FROM   (SELECT subject
                FROM   mdkndlst
                WHERE  subject IS NOT NULL
                ORDER  BY dateto)
        WHERE  rownum = 1) naktion
      ,mdkamvp.rowid row_id
FROM   springv2.mdkamvp
ORDER  BY mdkamvp.indstatus

答案 2 :(得分:0)

您应该使用 first_value

using Plugin.Geolocator;
using SQLite;
using System;
using System.IO;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using XamarinForms.SQLite;
using System.Data.SqlClient;
using System.Data;
using Android.App;

namespace WorkingWithMaps  
{
public class PinPage : ContentPage 
{
    int Col0 { get; set; }
    string Col1 { get; set; }
    double Col2 { get; set; }
    double Col3 { get; set; }

    Map map;

    public PinPage()
    {
        map = new Map {
            IsShowingUser = true,
            HeightRequest = 100,
            WidthRequest = 960,
            VerticalOptions = LayoutOptions.FillAndExpand
        };


    }

    public interface IDbDataFetcher
    {
        string GetData(string connection);




    }
    protected async override void OnAppearing()
    {

            // create pins

            base.OnAppearing();
            var morePins = new Button { Text = "Show Tests Near Me" };

        //create con to db





        //current pos
        var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;


            var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
            var reLocate = new Button { Text = "Re-center" };
            reLocate.Clicked += (sender, e) => {
            map.MoveToRegion(MapSpan.FromCenterAndRadius(
            new Position(position.Latitude, position.Longitude), Distance.FromMiles(0.5)));


            };


            var buttons = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Children = {
                morePins, reLocate
            }
            };




        foreach(int Col0 in allRecords)
        {
            morePins.Clicked += (sender, e) =>
            {
                map.Pins.Add(new Pin
                {
                    Position = new Position(Col2, Col3),
                    Label = Col1

                });

            };


            //map.Pins.Add(new Pin {
            //  Position = new Position(-26.080752, 28.026094),
            //  Label = "Mercedes-Benz - Bryanston"
//            });
//            map.MoveToRegion(MapSpan.FromCenterAndRadius(
//            new Position(position.Latitude, position.Longitude), Distance.FromMiles(2))); // this distance shows dealerships to current position

//            };


        // put the page together
        Content = new StackLayout { 
            Spacing = 0,
            Children = {
                map,
                buttons
            }};

        }
    }
}

答案 3 :(得分:0)

不使用选择列表中的标量子查询,而是使用派生表,其中您没有订单限制:

SELECT MDKAMVP.MDKAMDA_SID, 
       x.subject as NAKTION, 
       MDKAMVP.ROWID ROW_ID 
FROM SPRINGV2.MDKAMVP 
  cross join (
    select SUBJECT 
    from mdkndlst 
    where rownum=1 
      and SUBJECT is not null 
    order by DATETO DESC
  ) x
order by MDKAMVP.INDSTATUS;

由于派生表保证只返回一行,cross join不会更改整体结果。

如果您想要最新主题,您还应该订购DATETO降序。没有它,你将获得最古老的主题。

如果您使用的是12c,那么您可能希望使用Jiri所示的fetch first选项。