db2 select distinct rows, but select all columns - sql

Experts, I have a single table with multiple columns. col1, col2, col3, col4, col5, col6
I need to select distinct (col4), but I need all other columns also on my output.
If I run, this ( select distinct(col4 ) from table1 ), then I get only col4 on my output.
May I know, how to do it on db2?.
Thank you

You simply do this...
Select * From Table1 Where col4 In (Select Distinct(col4) From Table1)

I'm not sure if you will be able to do this.
You might try to run group by on this column. You will be able to run some aggregate functions on other columns.
select count(col1), col4 from table1 group by (col4);

none of the answers worked for me so here is one that i got working. use group by on col4 while taking max values of other columns
select max(col1) as col1,max(col2) as col2,max(col3) as col3
, col4
from
table1
group by col4

At least in DB2, you can execute
SELECT
DISTINCT *
FROM
<YOUR TABLE>
Which will give you every distinct combination of your (in this case) 6 columns.
Otherwise, you'll have to specify what columns you want to include. If you do that, you can either use select distinct or group by.

Related

Select multiple columns but distinct only one in SQL?

Lets say I have a table called TABLE with the columns col1, col2, col3 and col4
I want to select col1, col2 and col3 but distinct col2 values from the others, but I can't do it.
I tried something like this:
SELECT DISTINCT "col1", "col2", "col3" FROM [Table] WHERE col1 = Values
But the output brings me more than one record of col 2 with the same value.
I know that is because the distinct filtered all the columns that i specified, but i don't know how to get all the columns and filter only the values of col2.
Is it possible to SELECT more than 1 column but filter only one of them with SELECT DISTINCT ?
As you said, distinct just limits the full set of columns to eliminate duplicates. Instead, I'd just use an aggregate function with a GROUP BY statement.
SELECT MAX(col1) AS col1, col2,
MAX(col3) AS col3
FROM tbl
GROUP BY col2
That will take the top value alphanumerically from the supplied columns. Or, to list all values separated by commas:
SELECT STRING_AGG(col1,',') AS col1, col2,
STRING_AGG(col3,',') AS col3
FROM tbl
GROUP BY col2

SQL - Transposing rows from some columns in a table to each record in thesame table

I am using a platform which accepts minimal SQL functions to write a SQL code. The UNPIVOT function cannot be used on the platform so I have to do this manually. I am thinking along the line of UNION ALL and then CROSS JOINING (which I attempted but ended up with the wrong record counts. Please see image attached.
Any help / pointer will be highly appreciated!
I don't know how you used UNION ALL but it can be done like this:
select col1, col2, col3 as NewCol from Table1
union all
select col1, col2, col4 from Table1
You could also use an ORDER BY clause, so that rows with the same col1 and col2 appear in subsequent rows:
select col1, col2, NewCol
from (
select col1, col2, col3 as NewCol, 1 as ord from Table1
union all
select col1, col2, col4, 2 from Table1
) t
order by col1, col2, ord
A portable approach uses union all:
select col1, col2, col3 as newcol from mytable
union all
select col1, col2, col4 from mytable
If your database supports lateral joins (also called cross apply in some databases) and values(), this can be simplified:
select t.col1, t.col2, x.newcol
from mytable t
cross join lateral (values(col3), (col4)) x(newcol)
You can use a cross join, but it requires some case logic. The exact syntax depends on the database, but something like this:
select t.col1, t.col2,
(case when n.n = 1 then t.col3 else t.col4 end) as newcol
from t cross join
(select 1 as n union all select 2) n;
To load another table, you would do one of the following:
insert these results into a table that has already been created.
Use select into or create table as depending on the database.
If you care about the ordering, then you can add order by t.col1, t.col2, n.n.
In most cases, a simple union all approach is fine (such as GMB suggests). That approach requires scanning the table twice, which incurs some additional overhead. However, if the "table" is really a complex query or view, then only processing it once is a bigger advantage.

How to use values from another table in a SELECT statement?

select max(select col0 from table0) as col0, col1, col2
from table1
I hope it is clear from my "pseudo code" what I am trying to do. I basically want to add one single value from table0 to table1 so all rows of the first column is the same value.
But Toad cannot execute this form of code. Can you suggest a way for me to solve my problem?
The aggregate functions goes within the subquery, not around it:
select
(select max(col0) from table0) col0,
col1,
col2
from table1
select (select max(col0) from table0) as col0, tb1.col1, tb1.col2 from table1 tb1

SQL Server Group by clause issues

Suppose I have a query
select id, sum(col1), col2, col3, ......... col10
from table
If I run this without group by clause it gives an error
Column 'dbo.table.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If i use group by clause
select id, sum(col1), col2, col3, ......... col10
from table
group by col4
again the same error
Column 'dbo.table.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Until i haven't specified all those columns that hasn't implemented any aggregate function on it.
Now i cant apply an aggregate function on my all columns or i have to explicitly include all of my columns in group by clause
I'm not sure what you are trying to achieve.
As for your first query though, you could get the SUM without grouping the rows, if you use an analytic function:
select id,
sum(col1) over () as sum_col1, -- here you have the analytic function
col2,
col3,
......... col10
from table
This way, you still get all the rows in the table, but on each row you will have the sum of col1.
You could also have the sum over col4 (as for your second query), if you add a partition by clause to the analytic function:
select id,
sum(col1) over (partition by col4) as sum_col1,
col2,
col3,
......... col10
from table
You will still get the same number of rows, but the sum will be grouped by col4.
You can use join to get all Columns
Select id, col2,col3, ......... col10,sumcol1
From table t1 inner join
(
select sum(col1) as sumcol1, col4 as coln4 from table
group by col4
) t2
on
t1.col4 =t2.coln4

join two tables into one big table

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:
SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2
However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).
May it work to just do:
SELECT col1, col2, col3
INTO Table1
FROM Table2
Start with union all:
select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2
Your query is trying to deduplicate things, which would slow it down considerably.
I think the best option is to create a view in sql server, this will optimize the performance of the query:
SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2
(As other users said: "union" is used to select distinct values from two tables
where as "union all" is used to select all values including
duplicates from the tables.)
At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.
You could use this to fill the second table:
Insert into table2 select * from table1;
Or if you want to be more specific:
Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;
(Note: some DBMSs might require putting parenthesis around the SELECT clause.)
select * into new table(your new table name)
from table1.col1,table1.col2,table2.col1;
here columns can be your required columns .
select * into newtable from table1
union all
select * from table2
Worked well. Guidelines, both tables have exact same column names :)