I am trying to analyse a sqllite database and I use these data for a bar chart. I will count and do the avg of age group by each value in each column, in this case Class with the limit of only first 100 distinct values.
An example of this table:
Age Class
25 Worker
30 Student
48 Spy
I use LIMIT 100 to limit the result. To add more information for user, I want to let user know the number of values didn't get in account and the hidden rows, is there anyway to achieve this?
Simple solution: I am not very familiar with sql so I think to do two queries, with and without LIMIT, count the number of rows and substrat each other to find the answer. But because I have 42 columns so I would be very happy if I can have another solution.
If you want all but the first 100 rows, you can combine LIMIT with OFFSET.
select * from test01 LIMIT 1000000 OFFSET 100;
Related
I am learning SQL. I have a Select-From command and the output result is limited to 25 rows. I would like to increase the number of rows to say 50 or 75 rows displayed. What syntax do I include in my Select - From code to make this possible?
I tried typing 50 in front of the Select * Command.
If you mean limiting query result, depending on your database you can use some keywords.
For example, for SQL SERVER you can get only first 50 rows via the command below:
select top 50 * from yourTable;
But if you are limiting your output display via an editor/IDE, you need to check manual of it.
I would like to create a query that operates similar to a cash register. Imagine a cash register full of coins of different sizes. I would like to retrieve a total value of coins in the fewest number of coins possible.
Given this table:
id
value
1
100
2
100
3
500
4
500
5
1000
How would I query for a list of rows that:
has a total value of AT LEAST a given threshold
with the minimum excess value (value above the threshod)
in the fewest possible rows
For example, if my threshold is 1050, this would be the expected result:
id
value
1
100
5
1000
I'm working with postgres and elixir/ecto. If it can be done in a single query great, if it requires a sequence of multiple queries no problem.
I had a go at this myself, using answers from previous questions:
Using ABS() to order by the closest value to the threshold
Select rows until a sum reduction of a single column reaches a threshold
Based on #TheImpaler's comment above, this prioritises minimum number of rows over minimum excess. It's not 100% what I was looking for, so open to improvements if anyone can, but if not I think this is going to be good enough:
-- outer query selects all rows underneath the threshold
-- inner subquery adds a running total column
-- window function orders by the difference between value and threshold
SELECT
*
FROM (
SELECT
i.*,
SUM(i.value) OVER (
ORDER BY
ABS(i.value - $THRESHOLD),
i.id
) AS total
FROM
inputs i
) t
WHERE
t.total - t.value < $THRESHOLD;
In PostgreSQL: To retrieve 100 rows using an SQL query we can use LIMIT 100.
Is there a way to retrieve first 100 rows and later the next 100 like doing some kind of pagination? i.e.
if I do something like:
SELECT ..... LIMIT 100;
and later execute a command like:
SELECT ... LIMIT 100
I could get the next 100 rows to the previous retrieval of 100 rows and so on?
Thanks in advance
Yes, you need to use limit combined with offset. You can find more details here
select * from table LIMIT 100 OFFSET 100*0 --first 100
select * from table LIMIT 100 OFFSET 100*1 --second 100
I have an interesting question to ask you :)
Assumptions:
I am using Spring and Hibernate over Postgres database.
I have User table and every user has it's name and it's score.
I want to generate a sort of a rank where: Given the user's name I extrapolate 5 users that have more scores than the given user and 5 users that have less scores like this:
user1 p.105
user2 p.104
user3 p.103
user4 p.102
given user p.100
user6 p.99
user7 p.98
...
My question is: is it possible to make it in one query only? Will it be more optimal and faster in one query? Do you have any proposal of such a query?
Doing it in two queries is very simple but I want to know if there is a way to do it in one time?
PostgreSQL can do that with a single query. You might need to use native (raw) SQL in Hibernate. Here's one way.
with lowers as (
select *
from test
where score < (select score from test where user_id = 13)
limit 5
), highers as (
select *
from test
where score > (select score from test where user_id = 13)
limit 5
)
select * from lowers
union all
select * from highers
union all
select * from test where user_id = 13
order by score;
You should think about what should happen if there aren't five scores greater or five scores less than the target (user_id = 13). Also, this query won't bring in any rows that have scores identical to the target, which might or might not be what you're looking for. It also makes no attempt to get the 10 rows that are closest to the target.
On my dev box, which is no speedster, this query runs in 0.1 ms on a table of a million rows.
I have a list of 1000 records, and I shown there 1000 records on page load using display tag.
I enabled sorting on some columns, and when I click on the table header to sort, display tag is making a database call and loading all 1000 records again.
How can we restrict to make database calls on sorting for display tag?
Because, we have loaded all the 1000 records, so could we make use of those list without loading the records again?
You are missing a fundamental: Pagination.
You should not load 1000 records at once; load 10 records at once instead (assuming 10 records is what you show in a page), and load the other 10 when changing page / sorting.
Filter them directly in the query (if you are using queries), for example, selecting page 3 (results from 21 to 30) in a query to a minimal "person table", ordered by name, would result it the following SQL:
Oracle
SELECT id, name, age
FROM ( SELECT id, name, age
FROM table_person
ORDER BY name )
WHERE ROWNUM BETWEEN 21 and 30
PostgreSQL
SELECT id, name, age
FROM table_person
ORDER BY name
LIMIT 10
OFFSET 20
and so on.
Be sure to read the documentation related to your database, and simply use query parameters to specify order by, starting row and ending row values.
you can use setFirstResults(), setMaxResults(),instead unnecessarily load all 1000 records.
divde per page max result shown 10 records only, and setFirstResult is restricted records first 5 from your records list.
Criteria cr= database_session.createCriteria(Records.class);
cr.addOrder(Order.desc("Recordsdate"));
cr.setFirstResult(5);
cr.setMaxResults(10);
all_records_list = (List<Records>) cr.list();