'how to convert this top 1 SQL server into SQL ORACLE DEVELOPER [duplicate]
I am trying to convert this from SQL server to oracle:
select top 1 @StatusCount=Count(*),@logidnew=logid
from KioskStatuslog
where kioskid=@KioskId
and cast(logdate as date)=Cast(getdate() as date) --and lanstatus=@LANStatus
group by logid order by logid desc
Solution 1:[1]
TOP(n) is SQL Server's propriatary counterpart to standard SQL's FETCH FIRST n ROWS.
select count(*) as status_count, logid
from kioskstatuslog
where kioskid = :kioskid
and trunc(logdate) = trunc(sysdate)
group by logid
order by logid desc
fetch first row only;
As to
and trunc(logdate) = trunc(sysdate)
it is recommended to use
and logdate >= trunc(sysdate) and logdate < trunc(sysdate) + interval '1' day
instead, so the DBMS may use an index on that column if such exists. (Of course you could also have an index on trunc(logdate) instead and keep the simpler expression.)
Solution 2:[2]
Should be something among the lines:
SELECT *
FROM (SELECT COUNT(1), LOGID
FROM KIOSKSTATUSLOG
WHERE KIOSKID = &KIOSKID --argument
AND TRUNC(TO_DATE(LOGDATE,<FORMAT_OF_THE_DATE>)) = TRUNC(SYSDATE)
AND LANSTATUS = &LANStatus --argument
GROUP BY LOGID
ORDER BY LOGID DESC)
WHERE ROWNUM = 1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
