I am tryin to get the latest value from an oracle table based on server name. I have the following sql:
SELECT T."Node",T."Timestamp",T."MAX_User_CPU_Pct", T."MAX_System_CPU_Pct"
FROM DW.KPX_CPU_DETAIL_HV T where T."Node"='serverA%' and T."Timestamp"=
(select max(P."Timestamp") from DW.KPX_CPU_DETAIL_HV P where P."Node"='serverA%')
it does not seem to be working, any ideas what I might be doing wrong here?
Try this, might actually be faster than the sub-select (even if it was correct):
SELECT T."Node",
T."Timestamp",
T."MAX_User_CPU_Pct",
T."MAX_System_CPU_Pct"
FROM (
SELECT p.*,
row_Number() over (partition by p."Node" order by p."Timestamp" desc) as rn
FROM DW.KPX_CPU_DETAIL_HV p
) t
where rn = 1;
SELECT T."Node",T."Timestamp",T."MAX_User_CPU_Pct", T."MAX_System_CPU_Pct"
FROM (SELECT * FROM DW.KPX_CPU_DETAIL_HV T where T."Node" like 'serverA%' ORDER BY T."Timestamp" DESC) T
WHERE ROWNUM = 1
this did it for me, not sure it is the best solution but working for now.
Related
In SQL Server, I am attempting to pull the second latest NOTE_ENTRY_DT_TIME (items highlighted in screenshot). With the query written below it still pulls the latest date (I believe it's because of the grouping but the grouping is required to join later). What is the best method to achieve this?
SELECT
hop.ACCOUNT_ID,
MAX(hop.NOTE_ENTRY_DT_TIME) AS latest_noteid
FROM
NOTES hop
WHERE
hop.GEN_YN IS NULL
AND hop.NOTE_ENTRY_DT_TIME < (SELECT MAX(hope.NOTE_ENTRY_DT_TIME)
FROM NOTES hope
WHERE hop.GEN_YN IS NULL)
GROUP BY
hop.ACCOUNT_ID
Data sample in the table:
One of the "easier" ways to get the Nth row in a group is to use a CTE and ROW_NUMBER:
WITH CTE AS(
SELECT Account_ID,
Note_Entry_Dt_Time,
ROW_NUMBER() OVER (PARTITION BY AccountID ORDER BY Note_Entry_Dt_Time DESC) AS RN
FROM dbo.YourTable)
SELECT Account_ID,
Note_Entry_Dt_Time
FROM CTE
WHERE RN = 2;
Of course, if an ACCOUNT_ID only has 1 row, then it will not be returned in the result set.
The OP's statement "The row will not always be 2." from the comments conflicts with their statement "I am attempting to pull the second latest NOTE_ENTRY_DT_TIME" in the question. At a best guess, this means that the OP has rows with the same date, that could be the "latest" date. If so, then would simply need to replace ROW_NUMBER with DENSE_RANK. Their sampple data, however, doesn't suggest this is the case.
You can use window functions:
select *
from (
select
n.*,
row_number() over(partition by account_id order by note_entry_dt_time desc) rn
from notes n
) t
where rn = 2
I am executing a complex query in DB2 and the response time of which is quite high. After a lot of R&D, I found that the repetitive use of the max() function is causing hindrance in the execution time.
Thus I wish to know if there is an alternative for the max() function. I read a bit about rank() and was wondering if the same could be used, but wasn't able to get the result I wanted.
A part of my query is:
Select DISTINCT
(Select MAX(DATE(last_timestamp)) from Table_1 where ID = E.EID)
From Table_2 E
I am stuck at this for too long now. So any help would be appreciated.
row_number() is better than rank() since you don't want duplicates. Here it is. I'm thankful for this site as reference: https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/finding_the_maximum_row_and_more56?lang=en
SELECT DISTINCT last_timestamp
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY A.ID ORDER BY A.last_timestamp DESC) AS rn, DATE(A.last_timestamp) as last_timestamp
FROM Table_1 A, From Table_2 E where A.ID = E.EID)
WHERE rn = 1;
The syntax might not be exact since I did not test it and I don't have DB2 installed.
The issue may be the conversion of the timestamp to a date - DATE() - before doing the MAX() function - not a DB2 expert, but would guess that this may end up creating a temp table of dates that is then MAXed, and indexes are not used...
Suggest that the following be tried:
Select DISTINCT
DATE(
Select MAX(last_timestamp)
from Table_1
where ID = E.EID
)
From Table_2 E
Of course the query optimizer may be smart enough to do this...
Perhaps you can join on the following CTE :
WITH t1(i,t) AS
( SELECT id, MAX(last_timestamp)
FROM Table_1
GROUP BY id )
SELECT
...
DATE(t1.t),
...
FROM Table_2
LEFT JOIN t1 ON Table_2.EID = t1.i
Hello SQL/MS Access community! I have a problem! I have a query in SQL that gets the top record for each group and it works great! However, I need this to be a query in Microsoft Access. Is there anyone out there that can help me with translating it? I do not know coding at all (I can pick it apart to understand it, but I can't write it unfortunately). Any help is much appreciated!
SELECT *
FROM Table1;
WITH summary AS (
SELECT p.PK_ID,
p.Field1,
p.Field2,
ROW_NUMBER() OVER (PARTITION BY p.PK_ID
ORDER BY p.Field1 DESC) AS rk
FROM Table1 p)
SELECT s.*
FROM summary s
WHERE s.rk=1
You are trying to get the first record (based on Field1) for each pk_id. Something like this may work for you:
select p.*
from table1 as p
where p.field1 = (select max(p2.field1)
from table1 as p2
where p2.pk_id = p.pk_id
);
I want that select name and family of user in a table that User-Id (in this)
is FK in another table. my conditions are:
1.[User-Id](in this table) = [Resume-Owner-Id] in other table.
2.Row number is between 2 value that get from asp program.
I write below code. but this has error.
SELECT *
FROM (SELECT Family,
Name,
Row_number()
OVER(
ORDER BY [User-Id] DESC) AS RowNum
FROM [Members-Description-Tbl]) AS People
WHERE [User-Id] = (SELECT [Resume-Owner-Id]
FROM [Resume-Tbl])
AND ( RowNum BETWEEN #StartRowIndex + 1 AND #MaxRows );
This is your query:
Select *
From (select Family, Name,
ROW_NUMBER() OVER (ORDER BY [User-Id] desc) AS RowNum
from [Members-Description-Tbl]
) As People
Where [User-Id] = (select [Resume-Owner-Id]
from [Resume-Tbl]
) AND
RowNum between #StartRowIndex+1 and #MaxRows
Such a query could get many errors -- variables could be undefined, tables not present, columns not defined.
One potential error that stands out is the = in the where clause. If the subquery returns more than one row, it will generate an error. The simple solution is to use in instead of =.
Thanks for your answer Gordon Linoffred.
Thanks for your answer Pradeep.
That's right. Your answers are right. But my query rise previous error again. I read my query again and understand that I must write [People].RowNum or RowNum instead of [User-Id]. This works now. Thanks again. :)
I am stuck in a situation where I have to add a 'Serial No.' over a resultset in SQL.
The scenario is like below:
MyTable is like,
Now, after using the following query,
SELECT *
FROM MyTable
ORDER BY CHECKSUM(NEWID())
I am getting output like this,
Now, I want an output like this,
Using ROW_NUMBER() OVER (ORDER BY ID) AS SlNo gives me the serial number just same as my ID, which is not at serially ordered (and this is logically correct too).
Please help me out with some solutions. I am using SQL Server 2008 R2.
Pardon me for my mistakes. Thank you.
In SQL Server, the following will work - it will order by the first column of the result. It still provides a random order.
SELECT ROW_NUMBER() OVER (ORDER BY NEWID()) AS SlNo, *
FROM MyTable ORDER BY 1
You can order by the first column
SELECT CHECKSUM(NEWID()), *
FROM MyTable
ORDER BY 1
Or you can make a subquery so you can order by column name
SELECT * FROM
(
SELECT CHECKSUM(NEWID()) AS SLNo, *
FROM MyTable
) TBL
ORDER BY SLNo