SQL查询的输出需要与输入的顺序相同

时间:2013-11-08 17:36:55

标签: sql oracle

我已经浏览了网站上的其他一些答案,但似乎没有一个与我的问题相符(或者至少看起来不像我的眼睛!)。

我正在调整Excel宏,它需要一个客户引用列表,并在Oracle数据库上通过Microsoft ODBC运行SQL查询。查询本身工作正常并返回正确的结果。但是,用户希望结果与原始客户参考列表的顺序相同。

例如,2001145,2001101,2200110的清单应按该顺序返回结果,而不是2001101,2001145,2200110。

我正在寻找一种在SQL命令中执行此操作的方法,这可能吗?

提前谢谢。


以下是执行查询的代码。我不需要对输入进行排序,我需要输出与输入相同(未排序)的顺序。

Sql = " select b.column Customer, (b.CURR_BAL + b.CURR_BAL_V) Balance"
Sql = Sql & " from table b"
Sql = Sql & " WHERE b.column = 5"
Sql = Sql & " and b.column in (" & custRefList & ")"

'Runs the SQL Query, result applied to the destination
'Don't need to worry about any other settings exceot the UID,PRD and SERVER depending on system (same as toad)
    With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DRIVER={Microsoft ODBC for Oracle};UID=*****;PWD=*****;SERVER=*****;" _
        , Destination:=Range("A1"))
        .CommandText = (Sql)
        .Name = "Query from *****"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With

1 个答案:

答案 0 :(得分:1)

将此查询集成到您的vb中,它应该没问题:

SELECT 
    t.cli Customer, 
    (b.CURR_BAL + b.CURR_BAL_V) Balance
FROM table b
        JOIN (
            SELECT 
                level,
                REGEXP_SUBSTR(custRefList, '[^ |,]+', 1, level) cli
            FROM dual
            CONNECT BY LEVEL <= REGEXP_COUNT(custRefList, '[^ |,]+')
        ) t ON b.column = t.cli
WHERE b.column = 5
ORDER BY t.level