SQL Script using sum(column1$)/count(distinct(column2person) - sql

trying to use sum and count distint function & not getting results
Column1 column2 column3 column4 (3dividedby2)
personid count distinct sum$ sum$/count(distinct)
Above is the output i'm trying to get and what i see is this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 20,000
instead i would want to see this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 1,000
What am i doing wrong..
here is the query
select column1, count(distinct(column2)) as X, Sum(column3) as "COST"
, cost/ x as "Avg of column1 "
from table.table1
group by column1;
thanks!

You cannot re-use aliases in the select. Just repeat the expressions:
select column1, count(distinct column2) as X, Sum(column3) as cost,
sum(column3) / count(distinct column2) as avg_column1
from table.table1
group by column1;

Related

SQL self join to eradicate duplicate keys

I have a table with below columns:
Column1
Column2
Column3
A
Hello
NULL
A
NULL
WORLD
I want the above table to transform like below:
Column1
Column2
Column3
A
Hello
WORLD
I'm using Snowflake DataWarehouse. Need help in the above transformation using SQL
select column1,
max(column2) as column2,
max(column3) as column3
from your_table
group by column1;

Removing duplicates of column2 then group them based on column1 , then sum the values of column3 in sql

The table looks like
column1 column2 column3
400196 2021-07-06 33
400196 2021-07-06 33
400196 2021-08-16 33
I want to get the sum of column3 values based on grouping of column 1 but the duplicate values of date should not be added
The desired output is:
column1 column3
400196 66
The query I wrote is
select sum(column3)
from table_name
group by column1
But this gives me result 99
You can remove duplicate values in a subquery:
select t.column1, sum(t.column3)
from (select distinct t.column1, t.column2, t.column3
from t
) t
group by t.column1;
Note: This sort of problem can arise when you are joining tables together. Removing duplicates may not always be the right solution. Often it is better to do the calculation before joining, so you don't have duplicate values to deal with.
You could use a two step process here, first remove duplicates, then aggregate and sum:
SELECT column1, SUM(column3) AS column3
FROM (SELECT DISTINCT column1, column2, column3 FROM yourTable) t
GROUP BY column1;
Demo

select min value in same row in sql

I want to select min value of dates in same row of different columns.
e.g.
column1 column2 column3
2017-01-26 2017-01-28 2017-01-27
in above three columns i would like to select min date i.e. result of select should be 2017-01-26
Most databases support least() and greatest():
select least(column1, column2, column3) as min_column,
greatest(column1, column2, column3) as max_column
In any database, you can use ANSI standard case for the logic:
select (case when column1 >= column2 and column1 >= column3 then column1
when column2 >= column3 then column2
else column3
end) as max_column
(And then similar logic for the min.)
If using SQL Server, you can use this approach:
SELECT (SELECT MIN(columnX) FROM (VALUES(column1), (column2), (column3)) x(columnX))
FROM ...

Can I use TOP and Count in a single SELECT of SQL?

I am using SQL Server2008R2. I have below SQL select statement:
select column1, max(column2), min(column3)
from myTable
group by column1
order by column1
Let's say the above select statement returns 1001 records.
Let it return TOP 5 is not hard:
select top 5 column1, max(column2), min(column3)
from myTable
group by column1
order by column1
How can I modify the above statement so 1001 will also return and hence I know how many records in total?
I want some result like this:
1001 column1 max(column2) min(column3) -- top#1 row data
1001 column2 max(column2) min(column3) -- top#2 row data
1001 column1 max(column2) min(column3) -- top#3 row data
1001 column2 max(column2) min(column3) -- top#4 row data
1001 column2 max(column2) min(column3) -- top#5 row data
1001 is the total number of available records, and I only select top 5 of them. I want to know the total number and the details for top 5.
One method is with a subquery:
select top 5 *
from (select count(*) over () as cnt, column1, max(column2), min(column3)
from myTable
group by column1
) t
order by column1;
Although I prefer the subquery to prevent ambiguity, it also works without the subquery:
select top 5 count(*) over () as cnt, column1, max(column2), min(column3)
from myTable
group by column1
order by column1;
You can use the count() over() window function..
select top 5
count(*) over (),
column1,
max(column2),
min(column3)
from myTable
group by column1
order by column1

Return column with running sequence number Oracle

My simple query returns data like this:
SELECT column1, column2 FROM table1
COLUMN1 COLUMN2
------- -------
CA A
CA B
CB C
CB D
I want to return column3 with these values (for same COLUMN1 value, I want to return same sequence number):
COLUMN3
-------
1
1
2
2
You can use analytic function DENSE_RANK.
SELECT column1,
column2,
DENSE_RANK() OVER(ORDER BY column1) as "column3"
FROM table1
See the following for some examples - oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions.php#dense_rank
Try this query,
Select column1, column2,
dense_rank() over (order by column1) as column3
from table1;