Using sum in select statement without GROUP by - sql

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

Related

Access/SQL Count Distinct Values

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

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

Can GROUP BY query have non aggregate function call in projection list?

Is the following SQL query valid?
SELECT SOME_NON_AGGREGATE_FUNCTION(col1), SUM(col2)
FROM Table
GROUP BY col1, col2;
Or does it need to be:
SELECT FF, SUM(col2)
FROM Table
GROUP BY col1, SOME_NON_AGGREGATE_FUNCTION(col1) as FF;
Query 1 is valid ANSI SQL, but both SUM(col2) and GROUP BY col2 at the same time makes no sense. Either GROUP BY or aggregate function argument is the main rule!
Query 2 is invalid ANSI SQL syntax. The GROUP BY can only contain columns (some dbms allow general expressions too, but not in ANSI SQL.) You can not specify column aliases in the GROUP BY either.
Edit:
I suppose you want:
SELECT SOME_NON_AGGREGATE_FUNCTION(col1), SUM(col2)
FROM Table
GROUP BY col1;
I.e. GROUP BY col1 only, since col2 is used as argument to a aggregate function!

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

Row_number() function for Informix

Does informix has a function similar to the SQLServer and Oracle's row_number()?
I have to make a query using row_number() between two values, but I don't know how.
This is my query in SQLServer:
SELECT col1, col2
FROM (SELECT col1, col2, ROW_NUMBER()
OVER (ORDER BY col1) AS ROWNUM FROM table) AS TB
WHERE TB.ROWNUM BETWEEN value1 AND value2
Some help?
If, as it appears, you are seeking to get first rows 1-100, then rows 101-200, and so on, then you can use a more direct (but non-standard) syntax. Other DBMS have analogous notations, handled somewhat differently.
To fetch rows 101-200:
SELECT SKIP 100 FIRST 100 t.*
FROM Table AS T
WHERE ...other criteria...
You can use a host variable in place of either literal 100 (or a single prepared statement with different values for the placeholders on different iterations).