Access/SQL Count Distinct Values - sql

Need a quick help with Access. I'm not comfortable around SQL but i will try to explain my problems. I'm trying to create a simple query to Count the Number of distinct of Numbers in the following way:
What I have:
Table 1:
What I need to have:
Table 2:
Is it possible?

MS Access doesn't support count(distinct). But you can use a subquery:
select col1, count(*)
from (select distinct col1, col2 from t) as t
group by col1

Related

Using sum in select statement without GROUP by

select
Col1,
Col1/sum(Col1) as Fraction
from MyTable
This one will not return the desired result. If I add Gruop by Col1 then it will return a bunch of 1'a in the second column. How can I get around the problem?
Basically I want to treat the sum(Col1) as constant parameter. I can use with statement but I want a solution where I don't add another select statement.
I am using Toad and selecting from an Oracle database
Use SUM in its analytic form:
select
col1,
col1 / sum(col1) over () fraction
from mytable

find duplicate records in the table with all columns are the same

Say If I have a table with hundreds of columns. The task is that I want to find out duplicate records with all the columns are the same, basically find out identical records.
I tried group by as the following
select *
from some_table
group by *
having count(*) > 1
but it seems like group by * is not allowed in sql. Anyone has some idea as to what kind of command I could run to find out identical records? Thanks in advance.
Just put comma separated list of columns instead of * in both places - select and group by. Buy not count - the count(*) should remain as is.
I verified it on SQL Server, but I am pretty sure it is ANSI SQL and should work on most (any?) ANSI SQL compatible RDBMS.
Postgresql solution, I think.
SELECT all rows, and use EXCEPT ALL to remove one of each (the SELECT DISTINCT). Now we will have the duplicates only.
select * from table
except all
select distinct * from table
You have to list out all the columns:
select col1, col2, col3, . . .
from t
group by col1, col2, col3, . . .
having count(*) > 1;
MSSQL 2016+
Add a new column in the table to hash all the columns, MSSQL HashBytes
notes to consider:
you need to convert all the columns to Varchar or Varbinary.
is you comparison case sensitive, if yes use upper() or lower()
Null values, use column sperator.
the hashing algorithm Performance on the server.
for me usualy go for something like
select col1 , col2, col3 , col4
,HASHBYTES ( 'MD5',
concat(
Convert (varbinary ,col1),'|'
,Convert (varbinary ,col2),'|'
,Convert (varbinary ,col3),'|'
,Convert (varbinary ,col4),'|'
)
) as Row_Hash
from table1
the the row_hash can be use as a singl column in the table/CTE to present the content of all the other columns
you can count by it and Order by it to find the duplicates

How to get all records within a specific group of a GROUP BY result in pure SQL

Do you know the best way to get all records within a specific group of a GROUP BY results in pure SQL (by index if it's possible)?
Update:
SELECT col1, col2 from my_table GROUP BY col1, col2 where col1 = 123
If I'm understanding the question correctly, you used the GROUP BY clause to group your data by particular column data. If you have a specific value you would like to pull by using that column's data then a WHERE clause would be your best bet at getting the data you're looking to get
Easiest way, if I understand your question properly is to use HAVING clause in your query.
CREATE TABLE my_table (col1 INT, col2 INT);
GO
INSERT INTO my_table VALUES
(1,2),(3,4),(123,1),(123,2);
GO
SELECT col1,col2 FROM my_table GROUP BY col1,col2 HAVING col1=123
GO
DROP TABLE my_table
GO

How to manipulate a column selected by * in SQLite?

I want a query to return all rows and all columns with one caveat: if, in a given row, colN is null, then instead return the string 'FOO'.
Why dont I just use SELECT col1, col2, ..., COALESCE(colN, 'FOO')?
I am implementing an abstract interface and thus I am required to use SELECT queries which SELECT * (because I cannot make assumptions on what columns there are). I can only assume 1 columns exists: colN.
What would this provide me?
I need this because this query is used in combination with a UNION and this allows me to keep track of the origin of the data.
Any ideas on how to do this?
One thing you could do is
SELECT *, COALESCE(colN, 'FOO') as CoalescedColN
if it's possible to adjust the other select(s) in the UNION accordingly
I don't know if SQL Lite can use this technique but this is what I would do in most other dbs:
select * from
(SELECT col1, col2, ..., COALESCE(colN, 'FOO') from table ) a

Is there a difference between DISTINCT colname and DISTINCT(colname)?

I've seen both versions around. On iSeries DB2 you can use either and as far as I can tell they do the same thing. Is there a difference?
No, there is no difference because DISTINCT is a keyword and not a function call.
It's the same difference as between SOME_COLUMN and (SOME_COLUMN) (without any keyword in front)
If you have only one column in your select, then there is no difference.
However when you use distinct outside as -
select disctinct col1, col2, col3 from table
It applies distinct on the group tuple of (col1, col2, col3).
Finally there is no difference in using distinct as select distinct or select distinct()