Find average and then use that value to calculate other values - sql

I have a query with a few columns. I need to find the avg of a particular column and then use the average value to calculate other values.
I am finding avg with select avg(col1) as mean from tablea.
Now I want to use the mean value to calculate other values.
I tried:
select avg(col1) as mean, col2 - 2(avg(col1)
from table a
group by col2.
The problem is query should return only one row, but it is returning many rows based on col2.

Nest query.
SELECT col2, col2 - 2*(SELECT Avg(col1) AS mean FROM tablea) AS Calc
FROM tablea
GROUP BY col2

Related

Adding a Records Count row to top of query results in SQL

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.

How to include other columns with TOP in a query?

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

SQL Query that gives me last column with a value in it

So let's say i have a table with 6 columns col1-col6
Sometimes i will have values in the all 6 columns and sometimes maybe just in 3 or 5. It depends. The other columns will be null.
In my query i want a select from col1 and the last column with value in. It's becuase this column hold a total that need.
If the total would be fix in for example col6 i could make a easy query and say
SELECT col1,col6 from mytable
but the problem is i have to find out in with column the total is. It could be in any of col2-col6.
pls look at my fiddle for better understanding, in the example in fiddle i want to have col1, col5 back.
http://sqlfiddle.com/#!6/e3aeb/2
Use COALESCE to pick "last" non-null column.
select col1, coalesce(col6, col5, col4, col3, col2) from mytable

How to get product of two columns for each row in table and order by result of two column desc postgre?

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.

How does group by statement in SQL affect the results ?

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?