I have the following table:
date | id
-----------|------
unixtime_1 | 2
unixtime_1 | 7
unixtime_1 | 9
unixtime_1 | 24
unixtime_1 | 29
unixtime_1 | 21
unixtime_2 | 8
So far I get the results from such a table doing so:
SELECT date, id FROM table ORDER BY date DESC, id ASC
and I get
unixtime_1 | 2
unixtime_1 | 7
unixtime_1 | 9
unixtime_1 | 21
unixtime_1 | 24
unixtime_1 | 29
unixtime_2 | 8
I was wondering whether I could LIMIT the result so that in the range id=1-10 id=11-20 and id=21-30 I could get in the result only the record with the higher id.
So:
unixtime_1 | 9
unixtime_1 | 29
Since for id range=11-20 there isn't any record, it should be skip the range.
The range now are 1-10, 11-20, 21-30 but are custom ranges set by me according to the user request so I should be able to change them.
Is that possible via query?
Thank you
Your latest requirement should be possible to achieve merely by grouping by the date and (id - 1) / <some_number>, where in your example <some_number> would be 10.
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT date, (id - 1) / 10 AS id_grp, MAX(id) AS max_id
FROM yourTable
GROUP BY date, (id - 1) / 10
) t2
ON t1.date = t2.date AND t1.id = t2.max_id
ORDER BY
t1.date, t1.id;
You may choose any range you want 1 - num by simply replacing 10 in my query with the end of the range.
If i did understand correctly and if your range is id=1-3
you could simply do:
SELECT id, date FROM table WHERE id>=1 AND id<=3 ORDER by id DESC, date DESC limit 1;
this will give you only 1 record with the highest id in the range id=1-3.
you can store it and perform another query for another range and combine them later
Try this:
SELECT date, MAX(ID) FROM table GROUP BY date
Related
How can I create a column that has ranked the information of the table based on two or three keys?
For example, in this table the rank variable is based on Department and Name:
Dep | Name | Rank
----+------+------
1 | Jeff | 1
1 | Jeff | 2
1 | Paul | 1
2 | Nick | 1
2 | Nick | 2
I have found this solution but it's in SQL and I don't think it applies to my case as all information is in one table and the responses seem to SELECT and JOIN combine information from different tables.
Thank you in advance
You can count how many rows come before the current row in the current group:
UPDATE MyTable
SET Rank = (SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.Dep = MyTable.Dep
AND T2.Name = MyTable.Name
AND T2.rowid <= MyTable.rowid);
(The rowid column is used to differentiate between otherwise identical rows. Use the primary key, if you have one.)
Assume I have the following SQLite table "foobar":
id | start | duration
---+------------+---------
1 | 2016-05-12 | 2
2 | 2016-01-01 | 5
My goal is to get the sum of the start-date and the duration (durations are in years).
So my desired result is the following:
id | end
---+-----------
1 | 2018-05-12
2 | 2021-01-01
Is this possible with a single query?
I know it is possible to add static values as follows
SELECT date(start, "+2 years") FROM foobar;
but I could not find a way to replace the static 2 with the dynamic value of duration.
SELECT date(start, "+" || duration || " years")
FROM foobar;
SQLFiddle demo
I have a query result set like this:
value | id
500 | 1
400 | 1
300 | 2
and a product table:
product_name | id
product_1 | 1
product_2 | 2
product_5 | 3
product_6 | 4
product_9 | 5
product_0 | 6
Now I want to find the product_name from product table for every row in query result set and join the result to look like following:
The result after joining should be following:
product_name | id | value
product_1 | 1 | 500
product_1 | 1 | 400
product_2 | 2 | 300
I am getting confused how to use left join to achieve the result.
You'd join based on the value that's common between the two tables; in this case, it's id.
It doesn't seem like you want any NULL values for value, so you wouldn't use a LEFT JOIN; you'd want an INNER JOIN instead.
This would get you pretty much what you want; bear in mind that this is untested.
SELECT p.product_name, qr.id, value
FROM product p
INNER JOIN query_result_set qr ON qr.id = p.id
ORDER BY value DESC;
Also, your expectations are wrong - product_5 has an id of 3, so it wouldn't appear in this join result.
I have a sample table with following values
SNO | Mon
-----+-------
100 | 1
101 | 1
102 | 1
100 | 2
101 | 2
102 | 2
100 | 3
101 | 3
Now I need a query to count the total sno's which are in 3 months
The result should be 2, as 100 & 101 are in mon 1,2 and 3. However, 102 is only present in mon 1,2.
Thanks,
RK
This Query in theory should work.
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo));
The result would be,
sNo
----
100
101
I have used two SubQueries to get the result. Teh both are used in the HAVING clause of the SQL. First SqubQuery (inner most). Will get the number of Unique Month's available in your table, the outer SubQuery will then Count the number of Unique months. So the Overall Query can be translated as "SELECT the serial number FROM the table HAVING the Count of Month equal to the Number of unique records in the same table".
The reason I used SbQuery instead of a number is because of the fact this will also be applicable when your month number increases. Hope this helps !
EDIT
Here is the Query for getting the count.
SELECT
Count(*) As simpleCount
FROM
(
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo))
);
I have a table with this structure:
id | IDs | Name | Type
1 | 10 | A | 1
2 | 11 | B | 1
3 | 12 | C | 2
4 | 13 | D | 3
except id nothing else is a FOREIGN or PRIMARY KEY. I want to select a row based on it's column values that are not PRIMARY KEY. I have tried the following syntax but it yields no results.
SELECT * FROM MyTable WHERE Name = 'A', Type = 1;
what am I doing wrong? What is exactly returned by a SELECT statement? I'm totally new to Data Base and I'm currently experimenting and trying to learn it. so far my search has not yield any results regarding this case.
Use and to add multiple conditions to your query
SELECT *
FROM MyTable
WHERE Name = 'A'
AND Type = 1;