i want to do a union of a query but getting error.
SELECT
NB.NETBANKID,
NB.BANKNAME,
NBMAP.SORTORDER,
NB.BANKCODE
FROM VTSMNETBNK NB
INNER JOIN CGCMN.VTMESTRNETBNKMAP NBMAP
ON NBMAP.NETBANKID =NB.NETBANKID
WHERE NBMAP.STOREID=133 AND
NBMAP.EFFDATE <= SYSDATE
AND NBMAP.STATUS ='A'
AND NB.STATUS = 'A'
ORDER BY NBMAP.SORTORDER
FETCH NEXT 6 ROWS ONLY
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 15 Column: 1
The FETCH clause is only available as of Oracle 12c. In Oracle 11g you can work with ROW_NUMBER instead:
select netbankid, bankname, sortorder, bankcode
from
(
select
nb.netbankid,
nb.bankname,
nbmap.sortorder,
nb.bankcode,
row_number() over (order by nbmap.sortorder) as rn
from vtsmnetbnk nb
inner join cgcmn.vtmestrnetbnkmap nbmap on nbmap.netbankid = nb.netbankid
where nbmap.storeid = 133
and nbmap.effdate <= sysdate
and nbmap.status = 'A'
and nb.status = 'A'
)
where rn <= 6
order by rn;
Related
WITH RECURSIVE cte (n) AS
(
SELECT 1
UNION ALL
SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte;
I copied the above code from Oracle documentation. It works fine when I run it in MySQL, but I get the error message "Error at line 1/16: ORA-00905: missing keyword" when I run it in Oracle's Apex workshop. It is strange that a sample from Oracle documentation does not work.
Any idea what the problem is?
It is syntactically invalid to have RECURSIVE in the query. Whichever documentation you used it was not for an Oracle database; you want this documentation.
Additionally, SELECT 1 is not valid as you have a SELECT without a FROM clause; it should be SELECT 1 FROM DUAL.
The fixed code should be:
WITH cte (n) AS
(
SELECT 1 FROM DUAL
UNION ALL
SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte;
Which outputs:
N
1
2
3
4
5
fiddle
In oralce, you can't use SELECT 1, that is only allowed in Mysql
As is aid ORACLE rdms is not Mysql RDMS
select * from V$VERSION;
BANNER
BANNER_FULL
BANNER_LEGACY
CON_ID
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Oracle Database 21c Express Edition Release 21.0.0.0.0 - ProductionVersion 21.3.0.0.0
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
0
WITH cte (n) AS
(
SELECT 1 FROm DUAL
UNION ALL
SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte;
N
1
2
3
4
5
fiddle
This is my code
WITH cte AS
(
SELECT
nr_CRM,
CLASSIFIC,
COUNT(*) OVER (PARTITION BY nr_CRM) AS total_cnt
FROM tmp_usr..tb
)
SELECT
COUNT(DISTINCT(nr_CRM) AS CRM_total
FROM
cte
WHERE
total_cnt = 3
AND CLASSIFIC = 'GOLD'
AND dt_Visita >= '2021-01-25' AND dt_Visita <= '2021-02-26'
It was written on SQLite, but now I need to use SQL Server and it doesn't work.
On line 10, it displays the following error:
Incorrect syntax near the keyword 'AS'.
How can I adjust it?
there is an extra opening parenthesis :
WITH cte AS (
SELECT
nr_CRM,
CLASSIFIC,
COUNT(*) OVER (PARTITION BY nr_CRM) as total_cnt
FROM tmp_usr..tb
)
SELECT
COUNT(DISTINCT nr_CRM) AS CRM_total -- <-- here
FROM cte
WHERE
total_cnt = 3
AND CLASSIFIC = 'GOLD'
AND dt_Visita >= '2021-01-25'
and dt_Visita <= '2021-02-26'
I want to find 2nd minimum cat1_in_flag and cat2_in_flag but in this case i got this error can u please anyone suggest me how to solve that.
select trip_id,
(SELECT T1||' to '||T2 FROM(
SELECT cat1_in_flag T1,cat2_in_flag T2,
DENSE_RANK()OVER(ORDER BY min(to_number(cat1_in_flag)),min(to_number(cat2_in_flag))) RM
FROM TRIP_DTL
WHERE TRIP_ID = A.TRIP_ID
GROUP BY cat1_in_flag,cat2_in_flag
)WHERE RM=2)
from trip_mst A
ORA-00904: "A.TRIP_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 17 Column: 20
You can try the following query to achieve the same result:
SELECT
TRIP_ID,
T1
|| ' to '
|| T2
FROM
(
SELECT
TRIP_ID,
CAT1_IN_FLAG T1,
CAT2_IN_FLAG T2,
ROW_NUMBER() OVER(
ORDER BY
TO_NUMBER(CAT1_IN_FLAG), TO_NUMBER(CAT2_IN_FLAG)
) RM
FROM
TRIP_MST A
JOIN TRIP_DTL B ON ( A.TRIP_ID = B.TRIP_ID )
)
WHERE
RM = 2
Cheers!!
----- Update -----
Updated the answer according to the situation mentioned in the comment.
Now, You can join TRIP_MST table with other tables:
SELECT
TRIP_ID,
T1
|| ' to '
|| T2
FROM
TRIP_MST A
JOIN (
SELECT
TRIP_ID,
CAT1_IN_FLAG T1,
CAT2_IN_FLAG T2,
ROW_NUMBER() OVER(
ORDER BY
TO_NUMBER(CAT1_IN_FLAG), TO_NUMBER(CAT2_IN_FLAG)
) RM
FROM
TRIP_DTL
) B ON ( A.TRIP_ID = B.TRIP_ID )
WHERE
B.RM = 2;
Cheers!!
The error is because the reference to a.trip_id is nested too deeply for your older Oracle version. It is equivalent to this:
select dummy
, ( select testcol
from ( select 123 as testcol from dual
where dummy = a.dummy )
) as testcol
from dual a;
This fails in Oracle 11.2 with
ORA-00904: "A"."DUMMY": invalid identifier
because the reference to A is two levels down and so it can't see it. It succeeds in dbfiddle if you change the version to a later one.
If it was clearer what data you had and what results you wanted to see it should be possible to suggest an alternative query for 11g.
This is the query I'm trying create using SQL on phpMyAdmin:
SELECT DISTINCT Client_Order.orderID, Client.Title, Client.First_Name, Set_Product.setName, Client_Order.orderDATE, count( * )
FROM Client_Order
INNER JOIN Set_Product ON Set_Product.setID = Set_Product.setID
INNER JOIN Client ON Client.clientID = Client.clientID
GROUP BY Client_Order.orderID
ORDER BY Client.First_Name ASC
having count(*) > 1
But I'm getting an error saying:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having count(*) > 1 LIMIT 0, 30' at line 7
How can i display my query because without the count I am just getting multiple duplicates.
The following query works in MS Access, but it does not work in MS SQL Server:
SELECT
tblSession.PatientID as PID,
max(tblSession.SessionAttend) -
min(tblSession.SessionAttend) + 1 as NumSA,
max(tblSession.SessionSched) -
min(tblSession.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
tblSession.SessionNumber,
tblSession.SessionDate,
tblSession.SessionAttend,
tblSession.SessionSched FROM
tblPatient INNER JOIN tblSession ON
tblPatient.PatientID = tblSession.PatientID) WHERE
(tblSession.SessionDate >= '12/8/2010') AND
(tblSession.SessionDate <= '5/18/2011') AND
(tblSession.Status = '2') ORDER BY
tblSession.PatientID, tblSession.SessionNumber
) GROUP BY tblSession.PatientID
In SQL Server, it gives the error "Incorrect syntax near the keyword 'GROUP'." When I hover over the GROUP keyword, the tooltip displays "Incorrect syntax near 'GROUP'. Expecting AS, ID, or QUOTED_ID." I don't understand. Can anyone tell me how to make this query work?
The derived table in () needs an alias, and its column references in the SELECT list updated accordingly:
SELECT top 100 percent
ALIASNAME.PatientID as PID,
max(ALIASNAME.SessionAttend) -
min(ALIASNAME.SessionAttend) + 1 as NumSA,
max(ALIASNAME.SessionSched) -
min(ALIASNAME.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
...
...
...
...
tblSession.PatientID, tblSession.SessionNumber
-- This derived table needs an alias
) ALIASNAME
GROUP BY ALIASNAME.PatientID