There is a column (REC_CREATE_TIMESTAMP) in a table which shows date in format YY-MM-DD. I would like select the latest (by time) of a certain date (let's say Aug 31, 2011). The first column should be the timestamp in hh24:mm:ss and the rest should be all columns (*) of this table. How do I do it?
Using Oracle 2.1.1.64
REC_CREATE_TIMESTAMP - NOT NULL - DATE
SELECT * FROM Table WHERE REC_CREATE_TIMESTAMP IN
(
SELECT
MAX(REC_CREATE_TIMESTAMP) as REC_CREATE_TIMESTAMP
FROM
Table
WHERE
TO_CHAR(REC_CREATE_TIMESTAMP, 'YYYYMMDD') = '20110831'
)
EDITED for Oracle
SELECT *
FROM tableName As tbl1
WHERE EXISTS
(
SELECT NULL
FROM tableName as tbl2
WHERE
tbl2.rec_create_timestamp > '2001-08-30'
tbl2.rec_create_timestamp < '2001-09-01'
HAVING MAX(tbl2.rec_create_timestamp) = tbl1.rec_create_timestamp
)
For a generalised answer that can work with Sets of data rather than individual dates...
WITH
sequenced_data AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY TRUNC(rec_create_timestamp) ORDER BY rec_create_timestamp DESC) AS daily_sequence_id,
TRUNC(rec_create_timestamp) as rec_create_date,
*
FROM
yourData
)
SELECT
*
FROM
sequenced_data
WHERE
daily_sequence_id = 1
AND rec_create_date = '20110831'
Related
I have a table Activity having data like below.It contains multiple rows of CreatedBY like IVR,Raghu and IT.
But I need to get the data only when the first row of CreatedBY='IVR'.
This following query will return firstcreated row for each user (CreatedBy)-
SELECT * FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY CreatedBy ORDER BY CreatedBy,[Date And Time]) RN
FROM your_table
)A
WHERE RN = 1
I suspect you want the first row per ticket_no. At least, that makes more sense as a query.
If so, in SQL Server, you can use a correlated subquery:
select a.*
from activity a
where a.createdby = 'Raghu' and
a.datetime = (select min(a2.datetime)
from activity a2
where a2.ticket_no = a.ticket_no
);
use exists
select a.*
from table a where createdby='IVR'
and datetime in
(select min(datetime) from table b where a.ticketno=b.ticketno
and createdby='IVR')
I have the following table:
username | timestamp | distance_location1 | distance_location2
jack 1532101253000 22.2541(km) 152.652(km)
and query which i am using is
select *
from table
where timestamp >= (select min(distance_location1) from table
AND timestamp <= (select min(distance_location2) from table
I want to get the records based on timestamp column.
starting value of timestamp is where minimum distance_location1.
and ending value of timestamp is where minimum distance_location2
above query is not working as it gives 0 records
If distance_location1 is data type is timestamp then below query will work
where your you put parenthesis in wrong place
select *
from table
where timestamp >=
( select min(distance_location1 from table )
AND timestamp <= (select min(distance_location2 from table)
But if distance_location1 and distance_location2 is not datatype timestamp
select * from table
where timestamp>= (select min(timestamp) from table t where t.distance_location1=(select min(distance_location1) from table)
) and
timestamp<=( select min(timestamp) from table t where t.distance_location2=(select min(distance_location2) from table))
You need to compare timestamps, not distances. In Oracle, you can use keep and analytic functions:
select t.*
from (select t.*,
min(timestamp) keep (dense_rank first over order by distance_location1) as distance_location1_timestamp,
min(timestamp) keep (dense_rank first over order by distance_location2) as distance_location2_timestamp
from table t
) t
where timestamp >= distance_location1_timestamp and
timestamp <= distance_location2_timestamp;
I was wondering if it was possible in SQL Server 2008 R2 to create a view with only the last column (DateTime DESC), but this last row should be copied in the view again.
So the end result would be a View with two rows with the same data.
The query to select one row is easy:
SELECT TOP 1 *
FROM Reporting
ORDER BY DateTime DESC
or
SELECT TOP 1 *
FROM Reporting
WHERE DateTime IN (SELECT MAX(DateTime)
FROM Reporting)
This returns only one row, but I want to duplicate this row in the view again.
Thanks
The syntax in the above answer is invalid. You are not allowed to have ORDER BY in each data source in the UNION ALL. You can have only one at the final statement. So, this is wrong:
SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC
UNION ALL
SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC
And should be done like this:
SELECT * FROM (SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC)
UNION ALL
SELECT * FROM (SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC);
I will advice using a different approach. Use fake data source and then cross apply.
SELECT SI.*
FROM
(
SELECT 1
UNION ALL
SELECT 2
) DS ([col])
CROSS APPLY
(
SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC
) SI;
You can test easily that the execution plan of this statement is better causing only one ordering and index scan:
Try Union all:
SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC
UNION ALL
SELECT TOP 1 * FROM Reporting ORDER BY DateTime DESC
I have an oracle DB.
My table has ID and DATE columns (and more).
I would like to select for every ID the next available record after a certain date. For only one ID the query would be:
SELECT * FROM my_table
WHERE id = 1 AND date >= '01.01.2018'
(just ignoring the to_date() function)
How would that look like for multiple IDs? And I do want to SELECT *.
Thanks!
We can use ROW_NUMBER here:
SELECT ID, date -- and maybe other columns
FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY date) rn
FROM my_table
WHERE date >= date '2018-01-01'
) t
WHERE rn = 1
The idea here is to assign a row number to each ID partition, starting with the earliest date which occurs after the cutoff you specify. The first record from each partition would then be the immediate next date, assuming it exists.
You have three fields ID, Date and Total. Your table contains multiple rows for the same day which is valid data however for reporting purpose you need to show only one row per day. The row with the highest ID per day should be returned the rest should be hidden from users (not returned).
To better picture the question below is sample data and sample output:
ID, Date, Total
1, 2011-12-22, 50
2, 2011-12-22, 150
The correct result is:
2, 2012-12-22, 150
The correct output is single row for 2011-12-22 date and this row was chosen because it has the highest ID (2>1)
Assuming that you have a database that supports window functions, and that the date column is indeed just date (and not datetime), then something like:
SELECT
* --TODO - Pick columns
FROM
(
SELECT ID,[Date],Total,ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY ID desc) rn
FROM [Table]
) t
WHERE
rn = 1
Should produce one row per day - and the selected row for any given day is that with the highest ID value.
SELECT *
FROM table
WHERE ID IN ( SELECT MAX(ID)
FROM table
GROUP BY Date )
This will work.
SELECT *
FROM tableName a
INNER JOIN
(
SELECT `DATE`, MAX(ID) maxID
FROM tableName
GROUP BY `DATE`
) b ON a.id = b.MaxID AND
a.`date` = b.`date`
SQLFiddle Demo
Probably
SELECT * FROM your_table ORDER BY ID DESC LIMIT 1
Select MAX(ID),Data,Total from foo
for MySQL
Another simple way is
SELECT TOP 1 * FROM YourTable ORDER BY ID DESC
And, I think this is the most simple way!
SELECT * FROM TABLE_SUM S WHERE S.ID =
(
SELECT MAX(ID) FROM TABLE_SUM
WHERE CDATE = GG.CDATE
GROUP BY CDATE
)