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 |
+----+------+------+------+
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 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
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;
I am stuck in similar situation as this.
I have multiple columns with different types of data, and I want to select all columns but group by it with only one column.
My Table:
+--------+----------+----------+-------+-----------------------+
| id | b_group | col2 | col3 | col4 |
+--------+----------+----------+-------+-----------------------+
| 1 | 1 | abcd | 100 | www.google.com |
| 2 | 1 | xyz | 200 | www.yahoo.com |
| 3 | 2 | dfs | 200 | www.stackoverflow.com |
| 4 | 3 | asda3 | 78 | www.imdb.com |
| 5 | 4 | zsdvf4 | 65 | www.youtube.com |
| 6 | 5 | sdf4 | 101 | www.ymail.com |
| 7 | 5 | ssdfsd | 200 | www.gmail.com |
| 8 | 1 | zxcgdf4 | 200 | www.club.com |
| 9 | 6 | yujhgj | 202 | www.thunderbird.com |
+--------+----------+----------+-------+-----------------------+
After reading the solution provided there, what I understood is to use aggregate function so my query is like:
select MIN(b_group),id,col2,col3,col4 from myTable where col3='200' group by id,col2,col3,col4;
But this is not working in my case, it is giving all the records where col3=200.
My desired Output:
+--------+----------+----------+-------+-----------------------+
| id | b_group | col2 | col3 | col4 |
+--------+----------+----------+-------+-----------------------+
| 2 | 1 | xyz | 200 | www.yahoo.com |
| 3 | 2 | dfs | 200 | www.stackoverflow.com |
| 6 | 5 | sdf4 | 200 | www.ymail.com |
+--------+----------+----------+-------+-----------------------+
I don't care which record is picked, order don't matter.
I just want to select all columns with group by only one.
By applying a group by clause, you get a result row per unique combination of all the columns in it (in this case, per unique combination of id, col2, col3, and col4). Instead, you could use the row_number window function to number rows per b_group, and then select just the (arbitrary) first of each group:
SELECT id, b_group, col2, col3, col4
FROM (SELECT id, b_group, col2, col3, col4,
ROW_NUMBER() OVER (PARTITION BY b_group ORDER BY 1) AS rn
FROM mytable
WHERE col3 = 200)
WHERE rn = 1
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