Sql Get the last 6 - sql

I need a sql query that use the Records ID to get the last 6 times of the trips also the number of time.
So the records in the table are like the following,
RecordID Nooftime Day&Time
1001 1 12/11/2009 14:11
1001 2 13/11/2009 12:11
1001 3 14/11/2009 11:11
1001 4 16/11/2009 14:11
1001 5 17/11/2009 14:11
1001 6 20/11/2009 13:11
1001 7 25/11/2009 09:11
I Need a query that show only the last 6 vist's and in one line.

If you're dealing with SQL Server try
Select Top 6 RecordID, NoOfTime, Day&Time from (table) order by Day&Time DESC

Oracle
SELECT x.*
FROM (SELECT t.*
FROM TABLE t
ORDER BY day&time DESC) x
WHERE ROWNUM <= 6
SQL Server
SELECT TOP 6 t.*
FROM TABLE t
ORDER BY day&time DESC
MySQL/Postgres
SELECT t.*
FROM TABLE t
ORDER BY day&time DESC
LIMIT 6

How about
SELECT TOP(6) *
FROM [Trips]
ORDER BY [Day&Time] DESC
I'm not sure what you mean by "in one line," but this will fetch the 6 most recent records.

For the same line portion, you can use a cursor if you are using SQL server 2008 (might work with 2005 I am not sure).

SELECT RecordID, NoofTime, Day&Time FROM table ORDER BY Day&Time DESC LIMIT 6;
depending on your SQL engine you will probably have to put some quotes around Day&Time.
The above syntax works with Mysql and PostgreSQL, for varous syntax used for this kins of request you can have a look there select-limit

Related

SQL Modification

I have a query (Main Query) is like this. I am executing this in Toad connected to Netezza DB.
SELECT *
FROM db1.schema1.Table1
WHERE (pd_num, pd_num_mtr, pd_num_prefix, sqr_num) IN
(SELECT pd_num,
pd_num_mtr,
pd_num_prefix,
max (sqr_num) sqr_num
FROM db1.schema1.table1
WHERE create_date >= '01/01/2012' AND cd_operator <> 'N'
GROUP BY pd_num, pd_num_mtr, pd_num_prefix)
When I execute this I get some 1 million records as my output. I further executed a query (Query2) to analyze the number of records belonging to the group as follows.
select pd_num_mtr,pd_num_prefix,count(*)
from db1.schema1.table1
GROUP BY pd_num, pd_num_mtr
order by count(*) desc
I get the below out put for this.
pd_num pd_num_mtr count(*)
001 15 500
002 15 200
003 30 100
Which means I have some 500 records pulled for the pd_num and pd_num_mtr combination with each of these records having an update_timestamp value. Now this needs to be modified as follows.
So among these 500 records, I need to pull only the one with maximum update_timestamp which will limit the count to only 1 record instead of 500.1 from 200 records, 1 record from 100 records with the max update timestamp value.
How can I modify the first query (main query) to acheive this? So that if the run the query2, I get the below as the output.
pd_num pd_num_mtr count(*)
001 15 1
002 15 2
003 30 3
Appreciate your help again. Thank you.
We will have to use row_number function for this. Assuming 'update_timestamp' as your timestamp column.
SELECT PD_NUM_MTR,PD_NUM_PREFIX
FROM
(
SELECT PD_NUM_MTR,PD_NUM_PREFIX,ROW_NUMBER() OVER (PARTITION BY PD_NUM_MTR,PD_NUM_PREFIX ORDER BY update_timestamp desc ) AS RK
FROM DB1.SCHEMA1.TABLE1
)
WHERE RK=1;

counting subquery in SQL

I have the following query to count how many times each process_track_id occurs in a table:
SELECT
a.process_track_id,
COUNT(1) AS 'num'
FROM
transreport.process_name a
GROUP BY
a.process_track_id
This returns the following results:
process_track_id | num
1 14
2 44
3 16
5 8
6 18
7 17
8 14
This is great. Now is the part where I am stuck. I would like to get the following table:
num count
8 1
14 2
16 1
17 1
18 1
44 1
Where num are the distinct counts from the first table, and count is how many times that frequency occurs.
Here is what I have tried (it's a subquery, but I'm not sold on the method) and I haven't been able to get it to work just yet. I'm new to SQL and I think I'm missing out on some some key aspects of the syntax.
SELECT
X.id_count,
count(1) as 'num_count'
FROM
(SELECT
a.process_track_id,
COUNT(1) AS 'id_count'
FROM
transreport.process_name a
GROUP BY
a.process_track_id
--COUNT(1) AS 'id_count'
) X;
Any ideas?
It's probably good to keep in mind that this may have to be run on a database with at least 1 million records, and I don't have the ability to create a new table in the process.
Thanks!
Here's the subquery method you were driving at:
SELECT id_count, COUNT(*) AS 'num_count'
FROM (SELECT a.process_track_id
,COUNT(*) AS 'id_count'
FROM transreport.process_name a
GROUP BY a.process_track_id
)sub
GROUP BY id_count
Not sure there's a better method as the aggregation needs to run once anyway.
Try this
SELECT x.num, COUNT(*) AS COUNT
FROM (
SELECT
a.process_track_id, -- <--- You may removed this column
COUNT(*) AS 'num'
FROM
transreport.process_name a
GROUP BY
a.process_track_id
) X
GROUP BY X.num

Sqlite: Selecting records spread over total records

I have a sql / sqlite question. I need to write a query that select some values from a sqlite database table. I always want the maximal returned records to be 20. If the total selected records are more than 20 I need to select 20 records that are spread evenly (no random) over the total records. It is also important that I always select the first and last value from the table when sorted on the date. These records should be inserted first and last in the result.
I know how to accomplish this in code but it would be perfect to have a sqlite query that can do the same.
The query Im using now is really simple and looks like this:
"SELECT value,date,valueid FROM tblvalue WHERE tblvalue.deleted=0 ORDER BY DATE(date)"
If I for example have these records in the talbe and to make an easier example the maximum result I want is 5.
id value date
1 10 2010-04-10
2 8 2010-04-11
3 8 2010-04-13
4 9 2010-04-15
5 10 2010-04-16
6 9 2010-04-17
7 8 2010-04-18
8 11 2010-04-19
9 9 2010-04-20
10 10 2010-04-24
The result I would like is spread evenly like this:
id value date
1 10 2010-04-10
3 8 2010-04-13
5 10 2010-04-16
7 8 2010-04-18
10 10 2010-04-24
Hope that explain what I want, thanks!
Something like this should work for you:
SELECT *
FROM (
SELECT v.value, v.date, v.valueid
FROM tblvalue v
LEFT OUTER JOIN (
SELECT min(DATE(date)) as MinDate, max(DATE(date)) as MaxDate
FROM tblvalue
WHERE tblvalue.deleted = 0
) vm on DATE(v.date) = vm.MinDate or DATE(v.date) = vm.MaxDate
WHERE tblvalue.deleted = 0
ORDER BY vm.MinDate desc, Random()
LIMIT 20
) a
ORDER BY DATE(date)
I think you want this:
SELECT value,date,valueid FROM tblvalue WHERE tblvalue.deleted=0
ORDER BY DATE(date), Random()
LIMIT 20
In other words you want select rows with date column, so that date is from the sorted list of dates, from where we take every odd element? And add the last recorded element (with the latest date)? And everything limited to max 20 rows?
If that's the case, then I think this one should do:
SELECT id,value,date FROM source_table WHERE date IN (SELECT date FROM source_table WHERE (rowid-1) % 2 = 0 OR date = (SELECT max(date) FROM source_table) ORDER BY date) LIMIT 20

SQL: how do write the ORDER BY based on value within a group?

I have a table
project issues updated
1 1 2009-09-03
1 2 2009-09-08
2 1 2009-09-12
2 2 2009-09-01
and I would like to sort so that the projects are sorted in descending order so that the project with the latest updated issue is first etc., but all issues of a project are kept together and the issues are in ascending order within the project based on issue number
the result should be:
project issues updated
2 1 2009-09-12
2 2 2009-09-01
1 1 2009-09-03
1 2 2009-09-08
Something like this should do the job:
SELECT mt.*
FROM mytable mt
JOIN (SELECT MAX(updated) AS LastUpdated, project
FROM mytable GROUP BY project) lu ON lu.project = mt.project
ORDER BY lu.LastUpdated DESC, mt.Issues
Oops, I just saw the MySQL tag. I don't know if this solution will work in MySQL
I think this would work (sorry, no mysql at hand, so I just tested on sqlite...):
select t.project, t.issues, t.updated from t
join (select project, max(updated) as dat
from t group by project) as t1
on (t.project = t1.project)
order by t1.dat desc, t.issues asc
SELECT p.project, p.issues, p.updated, max(r.updated)
FROM table p INNER JOIN table r ON r.project=p.project
GROUP BY p.project, p.issues, p.updated
ORDER BY 4 DESC, p.project, p.issues
I've tried an equivalent query in mysql & it looks like it works the way you want it to.

Sybase Select WHERE IN given order

Is there a way to keep the order when using SELECT WHERE IN() in Sybase
There are two examples in mysql:
SELECT * FROM table WHERE id IN (118,17,113,23,72)
ORDER BY FIELD(id,118,17,113,23,72)
SELECT * FROM table WHERE id IN (118,17,113,23,72)
ORDER BY FIND_IN_SET(id, '118,17,113,23,72')
I need it in Sybase Ase.
You could break down the argument list with a case statement the explicitly assigns an ascending counter to the arguments and order according to that:
SELECT *
FROM sometable
WHERE id IN (118,17,113,23,72)
ORDER BY CASE id WHEN 118 THEN 1
WHEN 17 THEN 2
WHEN 113 THEN 3
WHEN 23 THEN 4
WHEN 72 THEN 5
END ASC
It's clunky as hell, but it should work.