I have a SQL query that returns 500,000 rows. Really slow to return that many records. I like to use TOP and return a certain amount of rows, but return specific columns as well, like:
Select Top 1000, col1, col2, col3 from table
Can I do something like this? If I can't use TOP, then how to return only 1000 rows, but include specific columns?
You can but you have to remove the coma after the number of rows you want to return:
Select Top 1000 col1, col2, col3 from table
Related
I want a row count above my query results. I found an article that suggested using sections but the summary and select query do not have matching columns/data types.
Ex.
Total Records 25
Col1 Col2 Col3...
XXXX XXXX XXXX
example from the suggestion I found but my columns and datatypes do not match between the two queries
SELECT * FROM (SELECT [Section]=2, Col1, Col2, ..., Value1, Value2
FROM #TEMP
UNION ALL
SELECT [Section]=1, 'Total', '----', ..., SUM(Value1), SUM(Value2)
FROM #TEMP
) AS T
ORDER BY [Section], Col1, ...
To be polite, you are not using the tool the way it is meant to be used. The contents of those columns are strongly typed. Each one contains strings, dates, numbers, etc, and you're adding another row with strings on top.
The only way I can see this working is if you were to convert all of your columns to VARCHAR columns and cast all of your data to VARCHAR(MAX) in the query.
Otherwise, I think that the most reasonable solution would be to perform a second query for the totals.
Which would be more cost effective way to create a basic SELECT query.
Option one:
SELECT id
FROM table
WHERE COL0 NOT IN (2,3,4,5,6,7,8,9...)
AND COL1 >= 20
AND COL2 <= 10
AND .... ;
Or option two:
SELECT id FROM table WHERE COL0 NOT IN (2,3,4,5,6,7,8,9...);
The COL0 is FK column.
The first thing necessary would be index on the COL0. But from there..
The number included in the NOT IN clause could be from 1 to 1000 for example.
Questions:
Would the additional values in the WHERE clause help the DB to perform the query faster by eliminating stuff that should not be in the response, or will it just be additional work to check the accordance to the additional values?
Theoretically having hundreds of ID values in the NOT IN clause would be considered as bad and "expensive" design?
I'm using Firebird 2.5 .
The db query optimizer will use the best index to filter the most number of rows.
So you should use first aproach and add either:
separate index for col0, col1 and col2
composite index for both (col0, col1, col2)
so imagine you have 1000 rows but only 10 are > 20 optimizer will use the col1 index to filter out 990 rows making the rest of the query faster.
Also instead of use NOT IN you could save those value in a separated table tblFilter
SELECT id
FROM table T1
LEFT JOIN tblFilter T2
ON T2.col0 = T2.col0
WHERE T2.col0 IS NULL
I have a table with a lot of columns and what I need to do is to write select that would take only unique values. The main problem is that I need to check three columns at the same time and if all three columns have same values in their columns(not between them, but in their own column) then distinct. Idea should be something like distinct(column1 and column2 and column3)
Any ideas? Or you need more information, because I'm not sure if everybody gets what I have in mind.
This is example. Select should return two rows from this, one where last column would have Yes and other row withNo`.
This is exactly what the distinct keyword is for:
SELECT distinct col1, col2, col3
FROM mytable
I have a table with 6 to 7 columns in a Postgres database.
I have to get the product of two columns, col1 and col2, for each row in the table and order by the result (descending order).
I have tried following things but haven't gotten the results I expected...
First:
SELECT SUM(col1 * col2) AS res
FROM movies_b order by res desc;
But this gives me a single value for all rows.
Second:
SELECT col3, col4, col5, col6, SUM(col1 * col2) AS res
FROM table
ORDER BY res DESC
But this fails with the error:
ERROR: column "table.col3"must appear in the GROUP BY clause or be
used in an aggregate function
I want to get all columns' values for each row and order the rows by the product of the two columns in descending order.
I hope I am clear.
Where you're going wrong is trying to use aggregate functions like SUM. If you use SUM it will add up every row's result and return that single number (which you found out).
What you want doesn't involve actually aggregating any data from multiple rows. All you want to do is order by the result of col1*col2.
Here is an example query:
SELECT col1, col2, col3, col4, (col1*col2) AS product
FROM movies_b
ORDER BY (col1*col2) DESC
You can even delete that last part of the SELECT line if you want, just make sure you keep the (col1*col2) in the ORDER BY part.
SQL Fiddle is up here.
Does including an extra column in group by change the number of rows in the results ?
I was doing a select query on a table A(col1,col2....col9) and I first included
select col1,col2,col3
from A where col1 = (condition)
group by col1, col2, col3
which yielded me certain number of results.
now I changed the query to this
`select col1,col2,col3, col8,col9
from A where col1=(condition)
group by col1,col2,col3, col8,col9'
and I got a different number of rows in the results. What could be the possible explanation ?
If the combination of col1, col2 and col3 is not unique, you can have more than one row with the same combination of those three.
If that happens, and those duplicates have different values for col8 and/or col9, then grouping by those extra columns will result in more rows.
Note that you can use select distinct to get the same results. group by is especially used if you want to aggregate over other columns, for instance, calculate a sum or a count, like so:
select
col1, col2, col3,
sum(col8) as total8
from A
group by col1, col2, col3
The query above will give you each unique combination of col1, col2 and col3 plus the sum over all col8's for each combination.
By grouping on those columns you are, in essence, making the results distinct on the grouped columns. So if there were rows that had columns 1, 2, 3, 18, and 19 in common, they would be folded together.
Adding GROUP BY isn't really the correct way to go about this as instead of grouping by the one column it tries to group across the board so you may end up with fewer or greater results depending on the data you're querying.
May I ask for what reason you're grouping the columns?