select min value in same row in sql - 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 ...

Related

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

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

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;

Subtract 2 rows using case statement in SQL Server 2008

My data is like below, it's in a single table
Column1 Column2
abc 100
abc 200
Now I need like below
abc 100 //here 200-100
I am banging my head on how to achieve this.
I have tried to use the row_number and then subtract using case statement like
Select
column1,
sum(
case when rownum=1
then column2
end
-
case when rownum=2
then column2
end
)
from table
group by column1
But this is giving me null.
Assuming there is no attribute which can define row ordering -
;with cte as(
select
row_number() over (order by (select null)) as IndexId,
Column1,
Column2
from #xyz
)
select sum(case when IndexID=1 then (-1 * Column2) else Column2 end), Column1
from cte
group by Column1
Input data-
declare #xyz table(Column1 varchar(10),Column2 int)
insert into #xyz
select 'abc' ,100 union all
select 'abc' ,200
Assuming you have an attribute rownum in table which is always 1 or 2 (it can be generated by some row_number() as you suggest in question, according to any order that is suitable for you)
Column1 Column2 Rownum
------------------------
abc 100 1
abc 200 2
then you can simply use
Select
column1,
sum(
case when rownum=1
then column2
else -column2
end
)
from table
group by column1
It performs a sum of the Column2 per Column1, however, in the row having rownum = 2 the Column2 value is negated. Therefore in our example you end up with 100 + (-200) = -100
You could do:
select column1, max(column2) - min(column2)
from t
group by column1;
Here is a short form of the answer above if you care:
SELECT
column1,
SUM(IIF(rownum=1,column2,-column2))
FROM table
GROUP BY column1

SQL Server : how to find ids where columns have different values

I have a table like this:
Column1 Column2
---------------
1 1
1 2
1 3
1 4
2 1
2 1
2 1
2 1
In column1 one there are 2 different ids, in column2 there are different values for each id from column1.
How can I get the id from column1 where not all ids from column2 are the same? So in this instance the output should be 1 - because they have all different values in column2, where id from column1 has all 1's in column2
Just use group by and having:
select column1
from table t
group by column1
having min(column2) <> max(column2);
Note: you could also use count(distinct), but that has more overhead than min() and max().
Similar logic can be used if the second column could be NULL. That doesn't appear in the sample data so it doesn't seem worth including it in the logic unless the OP specifically says this is a possibility.
Try like this:
select Column1
from yourTable
group by Column1
having count(DISTINCT column2) > 1;
I would think something like this should do the job:
SELECT t.column1 FROM table t
GROUP BY t.column1
HAVING COUNT(DISTINCT t.column2) > 1
This approach will handle the case where a null is an acceptable value in column2.
select column1
from
(
select distinct column1, column2
from yourTable
) t
group by column1
having count(*) > 1

Oracle sql, select First Row while selecting other things

What do I need to add to the query below to get just the first row that shows up after this query?
Query:
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2
Current Results:
column1 column2 column3 column4
30187 C 201330 1/3/2013 2:49:35 PM //I want this row only
33459 C 201330 1/3/2013 4:32:42 PM
90855 C 201390 3/28/2013 9:20:44 AM
96077 RA 201390 7/1/2013 11:31:46 AM
select * from (
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2)
where rownum = 1
or if you're using oracle 12c you can use the below,
SELECT column1,
column2,
MIN (column3),
MIN (colmun4)
FROM table
WHERE column0 = 'value'
GROUP BY column1, column2
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;