How to select maximum value of two identical columns in same table? - sql

id1 id2 name
1 1 2 a
2 3 4 b
3 5 6 c
4 7 8 d
5 9 10 e
select id1, id2, name
from Emp3
where id2 in (select MAX(id2) from Emp3)
How can I print only the maximum number?

USe
select id2
from Emp3
where id2 in (select MAX(id2) from Emp3)
This will print only 10
if you want the maximum among those 2 columns then use
SELECT
CASE
WHEN MAX(id1) >= MAX(id2) THEN MAX(id1)
WHEN MAX(id2) >= MAX(id1) THEN MAX(id2)
END AS MaxValue
FROM Emp3

Use TOP and ORDER BY to get the result
SELECT TOP 1 ID2 FROM Emp3 ORDER BY ID2 DESC

select max(id) from (
select max(id1) as id from Emp3
union
select max(id2) as id from Emp3
)

DECLARE #T TABLE (ID1 INT ,ID2 INT ,NAME VARCHAR(80))
INSERT INTO #T VALUES (1,2,'NME1')
INSERT INTO #T VALUES (3,4,'NME2')
INSERT INTO #T VALUES (5,6,'NME3')
INSERT INTO #T VALUES (7,8,'NME4')
INSERT INTO #T VALUES (9,10,'NME5')
INSERT INTO #T VALUES (11,12,'NME6')
SELECT * FROM #T
SELECT MAX(ID1) ID FROM
(
SELECT ID1 FROM #T T1
UNION
SELECT ID2 FROM #T T2
)TT

If both id columns are indexed, use derived table with UNION ALL:
select max(id)
from
(
select max(id1) as id from Emp3
union all
select max(id2) from Emp3
)
If they are not indexed, use CASE:
select max(case when id1 > id2 then id1
when id2 > id1 then id2
else coalesce(id1,id2) end)
from Emp3
This answer uses COALESCE to handle NULLs (if there are any...) The table will be read only once. (With the UNION solution the table will be read twice, and you don't want to do that without any index!)
Older answers:
I guess you want the row with the highest id1/id2 value?
Return a row when no other row with higher id value exists:
select id1, id2, name
from Emp3 e1
where not exists (select 1 from Emp3 e2
where e2.id1 > e1.id1
or e2.id1 > e1.id2
or e2.id2 > e1.id1
or e2.id2 > e1.id2)
Will return both rows if there's a tie. (Two or more rows with same highest value.)
Alternative solution, use TOP, combined with ORDER BY with CASE to find each rows larger id value:
select TOP 1 id1, id2, name
from Emp3
order by case when id1 > id2 then id1 else id2 end desc
Alternative 3, a sub-query with UNION ALL to find largest id:
select TOP 1 id1, id2, name
from Emp3
where (select max(case when id1 > id2 then id1 else id2 end) from Emp3) in (id1,id2)

Related

how to get unique data from multiple columns in db2

I wanted to get data from 2 columns in below way:
Id1 id2 id3
1 1 2
2 3 null
2 4 null
O/p
Id1 data
1 1,2
2 3,4
Here id1 is pk and id2 and id3 is fk of other table.
Try this as is:
WITH TAB (ID1, ID2, ID3) AS
(
VALUES
(1, 1, 2)
, (2, 3, NULL)
, (2, 4, NULL)
)
SELECT ID1, LISTAGG(DISTINCT ID23, ',') AS DATA
FROM
(
SELECT T.ID1, CASE V.ID WHEN 2 THEN T.ID2 ELSE T.ID3 END AS ID23
FROM TAB T
CROSS JOIN (VALUES 2, 3) V(ID)
)
WHERE ID23 IS NOT NULL
GROUP BY ID1;
This is a bit strange -- concatenating both within the same row and across multiple rows. One method is to unpivot and then aggregate:
select id1, listagg(id2, ',') within group (order by id2)
from (select id1, id2 from t union all
select id1, id3 from t
) t
where id2 is not null
group by id1;
Assuming that only id2 could be NULL, you can also express this as:
select id1,
listagg(concat(id2, coalesce(concat(',', id3), '')), ',') within group (order by id2)
from t
group by id1;

Select a subcategory ID to associate with a primary ID based off which has the highest sum

I have a primary ID, ID1, and a secondary ID, ID2. ID1 can be associated with multiple ID2 values, and vice versa. I want to sum a third Values column by ID2 under each ID1, and pull the ID2 with the highest sum. The source data is structured like:
ID1 ID2 Value
1 10 1
1 10 2
1 20 1
2 10 1
2 30 2
And I want the final results to look like:
ID1 ID2
1 10
2 30
So far, I only have a nonfunctioning query:
SELECT ID1,
CASE WHEN ID2_Value = MAX(ID2_Value) THEN ID2
ELSE NULL
END AS PrimaryID2
FROM ( SELECT ID1,
ID2,
SUM(Value) AS ID2_Value
FROM SOME_SCHEMA
GROUP BY ID1, ID2
) AS ID2_Value
GROUP BY ID1;
My query doesn't work right now because it expects me to include ID2_Value in the GROUP BY statement, but I don't want to group by those values.
I would use row_number():
select id1, id2
from (select id1, id2, sum(value) as sumv,
row_number() over (partition by id1 order by sum(value) desc) as seqnum
from t
group by id1, id2
) t
where seqnum = 1;

SQL Query to retrieve values that belong exclusively to a group

Suppose I have one table with the following values and columns:
ID1 | ID2
1 | 1
2 | 1
3 | 1
4 | 1
4 | 2
3 | 3
4 | 3
4 | 4
4 | 4
I'd like to retrieve the ID2 values that belong exclusively to records where ID1 = 4. So for the above example, I'd like to see the following response:
ID1 | ID2
4 | 2
4 | 4
Try working it out contrapositively like this.
Finding all elements where ID1 is only 4 is the same as finding all elements that don't not have ID1 = 4.
CREATE TABLE #temp (ID1 NVARCHAR(10), ID2 NVARCHAR(10))
INSERT INTO #temp(ID1,ID2) VALUES (N'1',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'2',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'3',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'1')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'2')
INSERT INTO #temp(ID1,ID2) VALUES (N'3',N'3')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'3')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'4')
INSERT INTO #temp(ID1,ID2) VALUES (N'4',N'4')
SELECT * FROM #temp AS t
SELECT DISTINCT * FROM #temp AS t
WHERE id2 NOT IN (SELECT ID2 FROM #temp AS t WHERE ID1 <> 4)
These queries will probably be useful to you for the more general cases (and by general I mean when ID1 is something other than 4):
select distinct t1.id1, t1.id2
from T as t1
where not exists (
select 1
from T as t2
where t2.ID1 <> t1.ID1 and t2.ID2 = t1.ID2
)
select t1.id1, count(distinct t1.id2)
from T as t1
where not exists (
select 1
from T as t2
where t2.ID1 <> t1.ID1 and t2.ID2 = t1.ID2
)
group by t1.id1
You can also do this:
select 4,id2 from
(select distinct ID1 , ID2 from t) t1
group by id2
having count(*)=1
There are a few ways to do this:
SELECT t1.id1, t1.id2
FROM mytable t1
WHERE t1.id1 = 4
AND NOT EXISTS ( SELECT 1 FROM mytable t2
WHERE t2.id2 = t1.id2
AND t2.id1 != 4 );
or:
SELECT id1, id2 FROM (
SELECT id1, id2
FROM mytable
GROUP BY id1, id2
HAVING COUNT(*) = 1
) WHERE id1 = 4;
or:
SELECT id1, id2 FROM (
SELECT id1, id2, COUNT(*) OVER ( PARTITION BY id2 ) AS cnt
FROM mytable
) WHERE id1 = 4
AND cnt = 1;

How to select in SQL Server 2008

Have an issue
Examle
ID ID2
1 100
3 100
5 100
1 110
2 110
4 110
select * from table where ID in (1,4) ---executing not correctly
select * from table where ID = '1' and ID = '4' ---not work
I need that ID2 will '110' (select ID2 which have 2 value ID)
Thanks.
If you have few ID's you can use EXISTS:
SELECT ID, ID2
FROM dbo.Table1 t1
WHERE EXISTS
(
SELECT 1 FROM dbo.Table1 t2
WHERE t2.ID=1 AND t2.ID2=t1.ID2
)
AND EXISTS
(
SELECT 1 FROM dbo.Table1 t2
WHERE t2.ID=4 AND t2.ID2=t1.ID2
)
AND ID IN (1, 4)
This returns the two records that have the same ID2 and ID=1 AND ID=4.
ID ID2
1 110
4 110
Demo-Fiddle
SELECT ID FROM (
SELECT ID, COUNT(*) OVER(PARTITION BY ID2) as cnt FROM Table) t
WHERE t.cnt>1

Join two different columns from two different tables

i have two temporary table
Table 1
ID1 Name ID2 Single
----------------------------------------------------
1 ABC 1 100
2 DEF 1 200
Table 2
ID1 Name ID2 Monthly
----------------------------------------------------
3 PQR 2 500
4 LMN 2 600
I want Output
ID1 Name ID2 Single Monthly
--------------------------------------------------------
1 ABC 1 100 NULL
2 DEF 1 200 NULL
3 PQR 2 NULL 500
4 LMN 2 NULL 600
I used all Joins nothing working
thanks in advance
JOIN won't work for that case, you need to use UNION here:
SELECT ID1, NAME, ID2, Single, NULL FROM Table1
UNION ALL
SELECT ID1, NAME, ID2, NULL, Monthly FROM Table2
Just in case, for some mad reason, you really do need it as a JOIN rather than (as other's have pointed out) a UNION ALL:
create table #T1 (ID1 int,Name varchar(10),ID2 int,Single int)
insert into #T1 (ID1 , Name , ID2 , Single)
select 1 ,'ABC', 1 , 100 union all
select 2 ,'DEF', 1 , 200
create table #T2 (ID1 int,Name varchar(10),ID2 int,Monthly int)
insert into #T2 (ID1 , Name , ID2 , Monthly)
select 3 ,'PQR', 2 , 500 union all
select 4 ,'LMN', 2 , 600
select COALESCE(t1.ID1,t2.ID1) as ID1,COALESCE(t1.Name,t2.Name) as Name,
COALESCE(t1.ID2,T2.ID2) as ID2,t1.Single,t2.Monthly
from #T1 t1 full outer join #T2 t2 on 1=0
Gives the result you asked for
You don't need JOINS, you need a UNION (ALL).
UNION (Transact-SQL)
Combines the results of two or more queries into a single result set
that includes all the rows that belong to all queries in the union.
The UNION operation is different from using joins that combine columns
from two tables.
SQL Statement
SELECT ID1, Name, ID2, Single, NULL as Monthly
FROM Table1
UNION ALL
SELECT ID1, Name, ID2, Null, Monthly
FROM Table2
try to use UNION
SELECT * FROM Single
UNION
SELECT * FROM Monthly
Hope this helps.
Please use the following query...
Select
ID1,
Name,
ID2,
Single,
NULL AS 'Monthly"
from Table1
Union
Select
ID1,
Name,
ID2,
NULL AS 'Single',
Monthly
from Table2