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
);
Related
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
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
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.
this seems simple but I cannot figure out how to do it or the proper description to correcltly google it :(
Briefly, have a table with:
PatientID | Date | Feature_of_Interest...
I'm wanting to plot some results for patients with multiple visits, when they have the feature of interest. No problem filtering out by feature of interest, but then I only want my resulting query to contain patients who have multiple entries.
SELECT PatientID,Date,...
FROM myTable
WHERE Feature_Of_Interest is present
AND (Filter out PatientID's that only appear once)
So - just not sure how to approach this. I tried doing:
WITH X AS (Above SELECT, Count(*),...,Group by PatientID)
Then re-running query, but it did not work. I can post that all out if needed, but am getting the impression I am approaching this completely backward, so will defer for now.
Using SQL Server 2008.
Try this:
WITH qry AS
(
SELECT a.*,
COUNT(1) OVER(PARTITION BY PatientID) cnt
FROM myTable a
WHERE Feature_Of_Interest = 'present '
)
SELECT *
FROM qry
WHERE cnt >1
You'll want to join a subquery
JOIN (
SELECT
PatientID
FROM myTable
WHERE Feature_Of_Interest is present
GROUP BY PatientID
HAVING COUNT(*) > 1
) s ON myTable.PatientID = s.PatientID
You could start with a counting query for visits:
SELECT PatientID, COUNT(*) as numvisits FROM myTable
GROUP BY PatientID HAVING(numvisits > 1);
Then you can base further queries off this one by joining.
Quick answer as I head off to bed, so its untested code but, in short, you can use a sub query..
SELECT PatientID,Date,...
FROM myTable
WHERE Feature_Of_Interest is present
AND patientid in (select PatientID, count(patientid) as counter
FROM myTable
WHERE Feature_Of_Interest is present group by patientid having counter>1)
Im surprised your attempt didnt work, it sounds a little like it should have, except you didnt say having count > 1 hence it probably just returned them all.
You should be able to get what you need by using a window function similar to this:
WITH ctePatient AS (
SELECT PatientID, Date, SUM(1) OVER (PARTITION BY PatientID) Cnt
FROM tblPatient
WHERE Feature_Of_Interest = 1
)
SELECT *
FROM ctePatient
WHERE Cnt > 1
I'm stuck at a very simple t-sql query. Please help !
Following is my result set :
Percentage---FirstName---SessionId
34-----------ABC---------222
67-----------ABC---------333
11-----------ABC---------444
83-----------XYZ---------555
23-----------XYZ---------666
64-----------XYZ---------777
From above, I want records of each user with maximum percentage e.g.
Percentage---FirstName---SessionId
67-----------ABC---------333
83-----------XYZ---------555
I can't seem to do that and I'm in hurry. Please help at the earliest. Any help would be greatly appreciated.
Thanks.
SELECT MAX(Percentage) AS Percentage,
FirstName
FROM mytbl
GROUP BY FirstName
Or if you need session id:
SELECT mytbl.*
FROM mytbl
INNER JOIN (SELECT MAX(Percentage) AS Percentage,
FirstName
FROM mytbl
GROUP BY FirstName) x ON x.Percentage = mytbl.Percentage
AND x.FirstName = mytbl.FirstName
If you need to deal with ties, then you might want to use the windowing functions (assuming you're on SQL Server 2005 or later):
SELECT
*
FROM
(SELECT mytbl.*,RANK() OVER (PARTITION BY FirstName ORDER BY Percentage desc) as rn) t
WHERE
t.rn = 1
So if there are two rows with the same percentage, they'll both be returned. If you only want one result, then you can add additional "tie-breaker" columns to the ORDER BY clause, or switch from RANK() to ROW_NUMBER().