This question already has answers here:
LISTAGG in Oracle to return distinct values
(24 answers)
Closed 4 years ago.
I have a table that looks like this:
+---------------------------+
| col1 | col2 | col3 |
+--------+---------+--------+
| 1 | A | apple |
| 1 | A | banana |
| 2 | B | grapes |
| 2 | A | orange |
+--------+---------+--------+
What I wanted is to combine all in col3 that has the same col1 and col2
The output that I wanted is like this:
+----------------------------------+
| col1 | col2 | col3 |
+--------+---------+---------------+
| 1 | A | apple, banana |
| 2 | B | grapes |
| 2 | A | orange |
+--------+---------+---------------+
You could use this:
SELECT col1, col2,
LISTAGG(col3, ', ') WITHIN GROUP (ORDER BY col1, col2) AS col3
FROM table_name
GROUP BY col1, col2
ORDER BY col1, col2;
Related
Is there a way to skip all rows that result in zero after division. For example
+------+------+
| Col1 | Col2 |
+------+------+
| 5 | 5 |
| 3 | 0 |
| 12 | 6 |
+------+------+
Then col3 = col1 /col2 giving:
+------+------+------+
| Col1 | Col2 | col3 |
+------+------+------+
| 5 | 5 | 1 |
| 12 | 6 | 2 |
+------+------+------+
You can try the below -
select col1, col2, col1/col2
from tablename
where col2!=0
I have two MS Access tables Table 1 and Table 2 where I am interested in values in Col1, Col2 and Col3, where Col2 could be empty. Using SQL, I want to be able to combine both tables, eliminate duplicates and find all rows for which non-empty Col2 values are different for the same value in Col3.
A example for the combined two tables without empty values in Col2
+-----------+---------+---------+
| Col1 | Col2 | Col3 |
+-----------+---------+---------+
| James | bar | 3 |
| Bob | red | 2 |
| Jess | red | 3 |
| Don | foo | 1 |
| James | bar | 1 |
| Mike | red | 3 |
| Paula | foo | 4 |
| Paula | red | 2 |
| Tom | red | 2 |
+-----------+---------+---------+
Would give:
+-----------+---------+---------+
| James | bar | 3 |
| Jess | red | 3 |
| Mike | red | 3 |
| Don | foo | 1 |
| James | bar | 1 |
+-----------+---------+---------+
To combine the tables and eliminate duplicates where Col2 is not empty, I can use:
SELECT [Col1], [Col2], [Col3]
FROM [Table 1]
WHERE [Col2] <> ''
UNION ALL
SELECT [Col1], [Col2], [Col3]
FROM [Table 2]
WHERE [Col2] <> ''
GROUP BY [Col1], [Col2], [Col3]
Using the result from the above statement, I would like to know which SELECT statement should be used to accomplish what I want.
You seem to want exists:
select distinct t.*
from table1 as t
where t.col2 is not null and
exists (select 1
from table1 as t2
where t2.col3 = t.col3 and t2.col2 <> t.col2
);
I have sql table as follows
+-----------------------------+
| |col1 | col2 | col3| col4| |
+-----------------------------+
| _______________________ |
| | a | 3 | d1 | 10 | |
| | a | 6 | d2 | 15 | |
| | b | 2 | d2 | 8 | |
| | b | 30 | d1 | 50 | |
+-----------------------------+
I would like transform the above table into below, where the transformation is
col4 = col4 - (col4 % min(col2) group by col1)
+------------------------------+
| |col1 | col2 | col3| col4| |
+------------------------------+
| ____________________________ |
| |a | 3 | d1 | 9 | |
| |a | 6 | d2 | 15 | |
| |b | 2 | d2 | 8 | |
| |b | 30 | d1 | 50 | |
| |
+------------------------------+
I could read the above table in application code to do transformation manually, was wondering if it was possible to offload the transformation to sql
Just run a simple select query for this:
select col1, col2, col3,
col4 - (col4 % min(col2) over (partition by col1))
from t;
There is no need to actually modify the table.
You can use a multi-table UPDATE to achieve your desired result, joining your table to a table of MIN(col2) values:
UPDATE table1
SET col4 = col4 - (col4 % t2.col2min)
FROM (SELECT col1, MIN(col2) AS col2min
FROM table1
GROUP BY col1) t2
WHERE table1.col1 = t2.col1
Output:
col1 col2 col3 col4
a 3 d1 9
a 6 d2 15
b 2 d2 8
b 30 d1 50
Demo on dbfiddle
I need something like this in MS ACCESS SQL
SELECT
ID,
col1,
col2,
random(col3)
FROM
table
GROUP BY
ID,
col1,
col2
NOTE:
I want to remove duplicates choosing random value of col3.
INPUT:
+----+------+------+------+
| Id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A | B | 7 |
+----+------+------+------+
| 1 | A | B | 10 |
+----+------+------+------+
RESULT:
+----+------+------+------+
| Id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A | B | 7 |
+----+------+------+------+
REQUERY:
+----+------+------+------+
| Id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A | B | 10 |
+----+------+------+------+
I have the table like this :
| Col1 | Col2 | col3 |
|:-----------|------------:|:------------:|
| type1 | 1 | aaaa |
| type3 | 101 | bbbb |
| type2 | 21 | cccc |
| type1 | 2 | aaa |
| type2 | 22 | bbb |
| type3 | 102 | ccc |
| type1 | 3 | aaax |
| type2 | 23 | bbbx |
| type3 | 103 | cccx |
I need output in following way...
| Col1 | Col2 | col3 |
|:-----------|------------:|:------------:|
| type1 | 1 | aaaa |
| type1 | 2 | aaa |
| type1 | 3 | aaax |
|
| type2 | 21 | cccc |
| type2 | 22 | bbb |
| type2 | 23 | bbbx |
|
| type3 | 101 | bbbb |
| type3 | 102 | ccc |
| type3 | 103 | cccx |
Please find some way to get such kind of output
And i have lot of records in this table but i need to get top 5 of each TYPE in same order.....
Try:
SELECT Col1,
Col2,
Col3
FROM(
SELECT
Col1,
Col2,
Col3,
ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1, Col2, Col3) RNum
FROM YourTable
)X WHERE RNum<=5
Use the ORDER BY function
SELECT COL1, COL2, COL3
FROM MyTable ORDER BY COl1, COl2, COl3
You don't need to group this data because GROUP BY needs to aggregate some columns. but instead, use only ORDER BY clause.
SELECT *
FROM tableName
ORDER BY Col1 ASC, Col2 ASC, Col3 ASC