i am trying to run a sql query which will not show distinct/duplicate values.
For example if using distinct option it would display only one unique result, but i would like to skip all detected distinct values i.e dont display distinct values
is it possible?
select col1 d from tb_col where col1 = '123';
col1
------
123
123
(2 rows)
select distinct col1 d from tb_col where col1 = '123';
col1
------
123
(1 row)
SELECT col1
FROM tb_col
GROUP BY col1
HAVING count(*) = 1
Not showing duplicates at all:
SELECT col1 AS d
FROM tb_col
GROUP BY col1
HAVING COUNT(*) = 1 --- or perhaps HAVING COUNT(*) > 1
--- it's not clear what you want.
select col1
from tb_col
group by col1
having count(*) < 2
Try with DISTINCT it will works!
SELECT DISTINCT(col1) as d from tb_col where col1 = '123';
Related
I have a table called tableA containing two columns, 1,2. I am trying to remove from column 1 a value depending on whats in the array. If that value exists twice, I want to set col1 as col1 = col1 - 2.
I could use UPDATE tableA SET col1 = col1 - 1 WHERE col2 IN (?); .. [arr] and be on my way, if WHERE IN () didnt ignore duplicate values on the array.
Let me also note that this isnt about primary or unique keys and there is no problem with duplicate values. Below is the query where X is where I need to fix.
UPDATE tableA SET col1 = col1 - X WHERE col2 IN (?); .. [arr]
I know I can loop for the arr and run a simple query like: UPDATE tableA SET col1 = col1 - 1 WHERE col2 = ? ; .. [arr[i]] each time but am trying to find a better way - if there is.
You could use UPDATE ... JOIN:
UPDATE tableA a
JOIN (SELECT id, COUNT(*) cnt
FROM (SELECT 1 AS id UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 2 UNION ALL SELECT 2) x -- here goes IN values
GROUP BY id
)s
ON a.col2 = s.id
SET a.col1 = a.col1 - cnt;
DBFiddle Demo
Probably a big Title! sorry for that.. :(
Table 1
id col1 col2
1 10 one
2 11 two
3 10 three
Now, i would like to write a sql query to get distinct col1 from table1 which doesn't have three in col2. I need the output col1 - 11 only.
I tried like this select distinct col1 from table1 where col2 != 'three' but this gives the result as both 10 and 11. But for 10 it has corresponding row with three as col2 value. Kindly help me to find this.
Use group by and having.
select col1
from table1
group by col
having sum(case when col2='three' then 1 else 0 end)=0
If you are using MySQL, the having condition can be shortened to
select col1
from table1
group by col
having sum(col2='three')=0
as conditions are treated as booleans returning 1 for true and 0 for false.
using not in()
select distinct col1
from table1 t
where col1 not in (
select col1
from table1 i
where i.col2 = 'three'
)
or using not exists()
select distinct col1
from table1 t
where not exists (
select 1
from table1 i
where i.col1 = t.col1
and i.col2 = 'three'
)
Here is my sample table
Col1 Col2
A A
B B
A C
B D
C C
I want to be able to select distinct records where all rows have the same value in Col1 and Col2. So my answer should be
Col1 Col2
A A
B B
C C
Simply:
select distinct * from t where col1 = col2;
if both cols have null and you want to get that row too:
select distinct * from t where coalesce(col1, col2) is null or col1 = col2;
The query is already written in your request:
select distinct records where all rows have the same value in Col1 and Col2
SELECT DISTINCT *
FROM tbl
WHERE Col1 = Col2
How do I structure my query so I can count how many occurrences of a value in column 1 appears in column 2 and then store that result in a new column in the same table? (If a value is duplicated in the first column I still want to store the same value in the new column) For example if I had a table like this:
COL1 COL2
1 2
1 4
2 1
3 1
4 1
4 2
The resulting table will look like this:
COL1 COL2 COL3
1 2 3
1 4 3
2 1 2
3 1 0
4 1 1
4 2 1
Any help is appreciated I am new to sql! Thanks in advance!
Select
col1,
col2,
COALESCE(col3,0) as col3
FROM
mytable
LEFT JOIN
( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
And if you want the update (thanks Thorsten Kettner ) :
UPDATE mytable
LEFT JOIN ( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
SET mytable.col3 = COALESCE(temp.col3,0)
You can easily count on-the-fly. Don't store this redundantly. This would only cause problems later.
select
col1,
col2,
(
select count(*)
from mytable match
where match.col2 = mytable.col1
) as col3
from mytable;
If you think you must do it; here is the according UPDATE statement:
update mytable
set col3 =
(
select count(*)
from mytable match
where match.col2 = mytable.col1
);
To do that, you can try :
SELECT COL1, COL2, (SELECT COUNT(COL1) FROM `tablename` AS t2
WHERE t2.COL1 = t1.COL1) AS COL3 FROM `tablename` AS t1
Enjoy :)
I have a table 'table1' as follows:
col1
----
1
1
2
2
2
3
3
I want to get count by group and total count from this table as follows:
col1 group_count total_count
-------------------------------------
1 2 7
2 3 7
3 2 7
I tried as follows:
SELECT col1, group_count, total_count FROM
(SELECT col1, COUNT(col1) AS group_count FROM table1 GROUP BY col1) Temp1,
(SELECT COUNT(col1) AS total_count FROM table1) Temp2
How to do it in optimised way
the optimized way is to first calculate the count and then simply put the variable in your select statement:
set #rowCount = (select count(col1) from table1);
select col1, count(col1), #rowCount from table1 group by col1;
See the result
The approach given by #Meherzad will calculate the row count many times. But if you want to do this in a single query u can use:
select col1, count(col1), (select count(col1) from table1) rowCount
from table1 group by col1;
Try this query
select col1, count(*), tot
from tbl t1, (select count(*) as tot from tbl) t2
group by col1
Fiddle