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

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

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.

SQL select distinct by 2 or more columns

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

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?

What do comma-separated integers in a GROUP BY statement accomplish?

I have a query like this:
SELECT col1, col2, col3, col4, col5, SUM(col6) AS total
FROM table_name
WHERE col1 < 99999
GROUP BY 1,2,3,4,5
What does the GROUP BY statement actually accomplish here? The query does not work properly without the comma-separated integers.
It is equivalent to writing:
SELECT col1, col2, col3, col4, col5, SUM(col6) AS total
FROM table_name
WHERE col1 < 99999
GROUP BY col1, col2, col3, col4, col5
The numbers are the values/columns in the select-list expressed by ordinal position in the list, starting with 1.
The numbers used to mandatory; then the ability to use the expressions in the select-list was added. The expressions can get unwieldy, and not all DBMS allow you to use 'display labels' or 'column aliases' from the select-list in the GROUP BY clause, so occasionally using the column numbers is helpful.
In your example, it would be better to use the names - they are simple. And, in general, use names rather than numbers whenever you can.
My guess is that your database product allows for referencing columns in the Group By by position as opposed to only by column name (i.e., 1 for the first column, 2 for the second column etc.) If so, this is a proprietary feature and is not recommended because of portability and (arguably) readability issues (But can admittedly be handy for a quick and dirty query).
Tried kind a same query in MS SQL Server 2005
select distinct host from some_table group by 1,2,3
It error's out saying
Each GROUP BY expression must contain at least one column that is not an outer reference.
So this indicates that those 1,2,3 are nothing but column outer referrence