SAP HANA Order by without TOP n - sql-order-by

In a SAP HANA SQL calculation view,
I need to sort by date, if I use select TOP n the following code works perfectly, the same code without top n doesn't sort the result, Am I missing something?
SELECT TOP 5
MATNR,
MANDT,
DOCNO,
DELIVERYDATE,
MISSING,
MISSING_TYPE
FROM "TABLE"
ORDER BY DELIVERYDATE ;

Related

Oracle forms cursor issue

I am using Oracle Forms and facing issue in writing a cursor. My cursor is
CURSOR ss
IS
SELECT pjno, bdate, PROJECT
FROM billcrown
LEFT JOIN bank_details ON billcrown.pjno = bank_details.pjno
WHERE billcrown.bdate > '01-Jul-2017'
GROUP BY billcrown.pjno
HAVING SUM (billcrown.PAMT) <> NVL (SUM (bank_details.AMOUNT), 0);
s ss%ROWTYPE;
It's showing error on pining on left join.
Error is (Encountered the symbol 'JOIN' when expecting one of the following. , ; )
It is working fine at SQL prompt. Please suggest.
Which Forms version is it? If it complains on LEFT JOIN, that's probably 6i or something. Previous (very old) Forms versions' PL/SQL engine didn't quite follow database's PL/SQL engine so not everything, that worked in the database, worked in Forms as well.
Therefore, I'd suggest you to try with the old Oracle outer join operator, (+).
Furthermore, if BDATE column's datatype is DATE, you should use DATE values against it, not strings. '01-Jul-2017' is a string. DATE '2017-07-01' is date (literal).
Finally, saying that your code works fine in SQL*Plus - no, it is not. GROUP BY contains only c.pjno column, so bdate, PROJECT should be included in there as well (or you should rewrite that query).
Something like this:
CURSOR ss
IS
SELECT c.pjno
-- , bdate, PROJECT --> removed because of the GROUP BY clause
FROM billcrown c, bank_details d
WHERE c.pjno = d.pjno (+) --> this
and c.bdate > date '2017-07-01' --> use dates, not strings!
GROUP BY c.pjno
HAVING SUM (c.PAMT) <> NVL (SUM (d.AMOUNT), 0);

How to recreate SAP queries in Oracle?

I need to recreate some SAP stored procedures in Oracle. I've been trying to find tutorials, similar questions, examples, etc about this but apparently no one had to do this before
What Oracle SQL query can be similar to this SAP query ?
SELECT * FROM A
INTO CORRESPONDING FIELDS OF TABLE B
FOR ALL ENTRIES IN C
WHERE a = C-a
AND x = y.
LOOP AT B INTO D.
D-b = E-b.
INSERT c FROM D.
IF SY-SUBRC <> 0.
WRITE: / 'error on insert', D-b, D-a.
ENDIF.
Any help will be appreciated, Thanks.
I recommend you to use transaction 'ST05' to trace your program. This tool will show details of the queries on the database including the exact SQL executed.
EDIT:
As a demonstration of the queries generated by SAP for Oracle let's execute this code and trace it with transaction 'ST05'. Remember to run 'ST05' before executing the program.
tables: mara.
data: it_mara type standard table of mara,
it_eina type standard table of eina.
select-options so_matnr for mara-matnr.
start-of-selection.
select matnr from mara into corresponding fields of table it_mara
up to 100 rows where matnr in so_matnr.
check sy-subrc eq 0.
select * from eina into table it_eina for all entries in it_mara
where matnr eq it_mara-matnr.
After execution check the output in transaction 'ST05':
If you want more details select an SQL statement in the screen and then click the button 'Explain'. You will see the following:
For better reference on transaction 'ST05' check this link.
Hope it helps.
The FOR ALL ENTRIES statement usually produces many queries which results are then grouped by UNION or UNION ALL.
Here is a really nice analysis for Microsoft SQL Server.
Because of the fact that UNION and UNION ALL are part of SQL standard I think it is implemented exactly the same for any other SQL database.
[EDIT]
As Mr Miranda stated it looks differently when it comes to Oracle database. I googled a bit and found this article where it is said that IN-LISTs are used which seems also to be plausible.

SYS_CONNECT_BY_PATH and START WITH/CONNECT BY PostgreSQL Equivalent

I have a query that is part of a much larger query written in Oracle that I need to convert to PostgreSQL.
/*rn and cnt are defined earlier*/
SELECT wtn, LTRIM(SYS_CONNECT_BY_PATH(RESP_TCSI, ','),',') TCSI_CODES
FROM DATA
WHERE rn = cnt
START WITH rn = 1
CONNECT BY PRIOR rn = rn-1
AND PRIOR WTN = WTN
From what I'm able to tell, there's not an equivalent to SYS_CONNECT_BY_PATH() in Postgres. I know that Postgres has a CONNECTBY() function in tablefunc, but I don't think it does what either the start with and connect by bits do. I'm also aware of what the Postgres equivalent to LTRIM() is, but if I have to use CONNECTBY() or something similar, I'm not sure if trimming the string is important.
Reading and searching around I noticed that there is probably a way to do this with some recursive select, but I'm unsure how I would do that, and beyond that, I don't really understand what the code is doing. My assumption would be that it has something to do with a hierarchical tree, based on the Oracle equivalents, but even then I'm not sure. How would I do something equivalent or similar to this in Postgres?
Thanks.
Use a recursive common table expression:
with recursive tree as (
select wtn,
resp_tcsi as tcsi_codes
from data
where rn = 1 -- this is the "start with" part
union all
select ch.wtn,
p.tcsi_codes||','||ch.resp_tcsi
from data as ch
join tree p
on ch.rn -1 = p.rn -- this is the "connect by" part
and ch.wtn = p.wtn
)
select *
from tree;

Forcing Oracle to do distinct last

I have a quite complicated view (using several layers of views across several database links) which takes a second to return all of it's rows. But, when I ask for distinct rows, it takes considerably more time. I stopped waiting after 4 minutes.
To make my self as clear as possible:
select a, b from compicated_view; -- takes 1 sec (returns 6 rows)
select distinct a, b from compicated_view; -- takes at least 4 minutes
I find that pretty weird, but hey, that's how it is. I guess Oracle messed something up when planing that query. Now, is there a way to force Oracle to first finish the select without distinct, and then do a "select distinct *" on the results? I looked into optimizer hints, but I can't find anything about hinting the order in which distinct is applied (this is first time I'm optimizing a query, obviously :-/).
I'm using Oracle SQl Developer on Oracle 10g EE.
Try:
SELECT DISTINCT A,B FROM (
SELECT A,B FROM COMPLICATED_VIEW
WHERE rownum > 0 );
this forces to materialize the subquery and prevents from view merging/predicate pushing, and likely from changing the original plan of the view.
You may also try NO_MERGE hint:
SELECT /*+ NO_MERGE(alias) */
DISTINCT a,b
FROM (
SELECT a,b FROM COMPLICATED_VIEW
) alias
Since you haven't posted details... try the following:
SELECT DISTINCT A,B
FROM
(SELECT A,B FROM COMPLICATED_VIEW);

MS SQL statement using PARTIAL and DISTINCT in VB.NET

I have a table called Postcodes which has over half a million records.
Inside this table i have a column called PostCodeText which has the following:
NG1 1AA
NG1 1AB
NG1 1AC
NG2 1AA
NG2 5TH
NG17 3LP
DE15 4BP
NG17 5GL
DE19 4EE...
What I need is a MSSQL statement that return DISTINCT matches based on a partial string. For example: If I wanted to find all distinct NG postcodes I would want to return:
NG1
NG2
NG17
I've tried something like:
SELECT DISTINCT postcodetext
FROM postcodes
WHERE (postcode_text LIKE 'NG%')
ORDER BY postcodetext
I feel I might be close to the answer but its not there yet, any help would be much appricated.
Also I heard that using LIKE is a slower option then using = is there a faster way then doing this?
Try
SELECT LEFT(postcodetext,CHARINDEX(' ',postcodetext,0))
FROM postcodes
WHERE postcodetext like 'NG%'
GROUP BY LEFT(postcodetext,CHARINDEX(' ',postcodetext,0))
ORDER BY postcodetext
OK, I think I'm missing something here, the above statement work fine (if I drop the ORDER BY), when I run it in management studio. But when I run it in my VB.net code I'm having an issue:
After running the statement and assigning the values into my datatable, I then use the following code :
For Each rows In dt.Rows
Select Case opt
Case 1 '
frmSettings.lstAllPostcodes.Items.Add(rows.Item("postcodetext"))
frmSettings.ProgBarSettings.Value = i
i = i + 1
End Select
Next
It fails when trying to reference rows.item("postcodetext"). I realise that has something to do with the SELECT statement using the LEFT of. I would like my listbox to hold the partial results (ie NG1, NG2, NG17 etc etc)

Resources