I have data in which i have duplicates but in different columns for ex :
Column A
Column B
1
2
2
1
3
4
4
3
but now my output should look like
Column A
Column B
1
2
3
4
how can i achieve this using sql query ?
We can use a least/greatest trick here:
SELECT DISTINCT LEAST(a, b) AS a, GREATEST(a, b) AS b
FROM yourTable;
The idea is to, e.g., take the two tuples (1, 2) and (2, 1) and bring them both to (1, 2), using the LEAST() and GREATEST() functions. Then, we just retain one them using DISTINCT.
Related
I have a table in SQL with a structure like:
ID_COL
VALUE_1
VALUE_2
VALUE_3
A
2
4
3
A
3
2
5
B
2
8
6
B
4
7
6
B
3
2
1
C
7
9
6
...
...
...
...
For each distinct ID_COL value (A, B, C, etc.) I need to add a row. Every row being inserted will have the same values for the VALUE_X columns. For example, I'll add a row with values A, 1, 2, 3, B, 1, 2, 3, etc.
Is there any way to do this programmatically in SQL without having to generate a bunch of separate insert statements? I'm not super familiar with SQL, but in another language like Python I would do a for-each loop on the distinct ID_COL values.
If it makes a difference, this is in SQL Server.
Thanks!
There is no need to resort to looping for this kind of thing. Your question is lacking any real details about your table or what you really want to accomplish. So assuming you want the value 1, 2, 3 along with each distinct value of ID_COL it would be something like this.
insert YourTable
select distinct ID_COL
, 1
, 2
, 3
from YourTable
I have this simplified query:
SELECT a.p_key, b.p_key
FROM test AS a, test AS b
WHERE
a.p_key != b.p_key -- skip self
AND a.p_key < b.p_key -- symmetry breaker
-- missing extra condition to skip previously fetched results
This is the returned data set. The * marks already 'retrieved' b.p_keys:
a.p_key p.b_key
1 2
1 3
2 3* (already linked to a.p_key = 1)
2 4
3 4* (already linked to a.p_key = 2)
3 5
I have done much research and experimentation but can't seem to reach the result I need:
I want to exclude b.p_keys that have already been retrieved previously, so my actual result set should look like this:
a.p_key p.b_key
1 2
1 3
2 4
3 5
I am a little unclear on the exact purpose of this code, but you can put adjacent pairs in a row using lead():
select p_key, lead(p_key) over (order by p_key) as next_p_key
from test;
This seems to do what you want, although you might want to filter out the last row with NULL:
select p_key, next_p_key
from (select p_key, lead(p_key) over (order by p_key) as next_p_key
from test
) t
where next_p_key is not null
If I have a table with a data structure that looks like so:
a,b
1,2
2,1
3,1
What I would like to do is group these 3 rows into 2 rows based on the 2 values a and b if the combination resides in either row together into 1 row so I can do a count query. I'd like to get a result set similar to:
a,b,count
1,2,2
3,1,1
You could use greatest and least to get the pair of columns in a consistent order:
select count(*)
from things
group by greatest(a, b), least(a, b)
That will give you the 2 you're looking for.
I have a column COL in a table which has integer values like: 1, 2, 3, 10, 11 ... and son on. Uniqueness in the table is created by an ID. Each ID can be associated with multiple COL values. For example
ID | COL
——————————
1 | 2
————+—————
1 | 3
————+—————
1 | 10
————+—————
is valid.
What I want to do is select only the COL values from the table that are greater than 3, AND (the problematic part) also select the value that is the MAX of 1, 2, and 3, if they exist at all. So in the table above, I would want to select values [3, 10] because 10 is greater than 3 and 3 = MAX(3, 2).
I know I can do this with two SQL statements, but it's sort of messy. Is there a way of doing it with one statement only?
SELECT col FROM table
WHERE
col > 3
UNION
SELECT MAX(col) FROM table
WHERE
col <= 3
This query does not assume you want the results per id, because you don't explicitely mention it.
I don't think you need pl/sql for this, SQL is enough.
I have data in a table like following
Name indicator
A 1
A 2
A 3
B 1
B 2
C 3
I want to get count of Names, for which both indicator 1,2 exists. In the preeceding example, this number is 2 (A & B both have indicator as 1, and 2).
The data I am dealing with is moderately large, and i need to get the similar information of some other permutations of (pre defined ) indicators (which i can change, once i get base query).
Try this:
SELECT Name
FROM Tablename
WHERE indicator IN(1, 2)
GROUP BY Name
HAVING COUNT(DISTINCT indicator) = 2;
See it in action here:
SQL Fiddle Demo