Check if column value exists in another column in SQL - sql-server

I need a SQL query that compares two values and returns an ID.
I have this table:
ID Calling_ID Called_ID
1 27 10
2 15 20
3 80 90
4 90 88
5 60 30
6 88 40
7 15 60
8 30 40
9 27 95
10 40 30
How do I check if each value in the Calling_ID column exists in the Called_ID column and then return the ID? The above data would return 88, 30, 40.

This should work:
SELECT ID FROM [TableName]
WHERE Calling_ID IN
(
SELECT Called_ID FROM [TableName]
)

SELECT ID,Calling_ID FROM [Table]
where Calling_ID in (SELECT Called_ID FROM [Table])

This should do it:
SELECT DISTINCT t1.ID FROM [Table] as t1
JOIN [Table] as t2 ON t1.Calling_ID = t2.Called_Id

Based on your statement, "The above data would return 88, 30, 40," it seems you were looking for Calling_ID / Called_ID to be returned, not ID. Here are two fully correct ways to do that:
Using JOIN
SELECT DISTINCT t1.Calling_ID FROM [Table] AS t1
JOIN [Table] AS t2 ON t1.Calling_ID = t2.Called_Id
Using IN
SELECT DISTINCT Calling_ID FROM [Table]
WHERE Calling_ID IN (SELECT Called_ID FROM [Table])

Try this,
SELECT Calling_ID FROM [Table] where Calling_ID = Called_ID;

Related

Find recursively the original Id from data in one table

First look at simple data and expected result that I want to achieve:
SAMPLE DATA
Id ParentId Mode
----------- ----------- ---------
28 0 A
29 30 B
30 0 R
31 32 C
32 33 T
33 34 Y
34 0 G
I can get my expected results using this query:
select
t1.Id,
coalesce(t5.Id,t4.Id,t3.Id,t2.Id,t1.Id) as BaseId,
coalesce(t5.Mode,t4.Mode,t3.Mode,t2.Mode,t1.Mode) as BaseMode
from
#Table t1
left join
#Table t2 on t2.ParentId = t1.Id
left join
#Table t3 on t3.ParentId = t2.Id
left join
#Table t4 on t4.ParentId = t3.Id
left join
#Table t5 on t5.ParentId = t4.Id
Expected result:
Id BaseId BaseMode
----------- ----------- ---------
28 28 A
29 29 B
30 29 B
31 31 C
32 31 C
33 31 C
34 31 C
But the problem is - I don't know how many times I will have to left join..
I could be any number.
I tried to use recursive cte - but it blows my mind. And I see a problem to figure it out. Can anyone show me how to achieve it?
Here are simple data to paste in your management studio:
select *
into #Table
from (select 28 as Id, 0 as ParentId, 'A' as Mode
union all select 29, 30, 'B'
union all select 30, 0, 'R'
union all select 31, 32, 'C'
union all select 32, 33, 'T'
union all select 33, 34, 'Y'
union all select 34, 0, 'G') data
You can try with this:
WITH ParentIdCTE (Id, BaseId, BaseMode, RecursionLevel)
AS
(
SELECT
Id,
Id,
Mode,
0 As RecursionLevel
FROM
#Table
UNION ALL
SELECT
p.Id,
e.Id,
e.Mode,
p.RecursionLevel + 1
FROM
ParentIdCTE As p
INNER JOIN
#Table AS e
ON
e.ParentId = p.BaseId
WHERE
p.BaseId <> 0
AND p.RecursionLevel < 10
)
SELECT
Id,
BaseId,
BaseMode
FROM
ParentIdCTE
INNER JOIN
(
SELECT
Id As MaxRecId,
MAX(RecursionLevel) as MaxRecLevel
FROM
ParentIdCTE AS s
GROUP BY
Id
) AS MaxRec
ON
MaxRecId = Id
AND MaxRecLevel = RecursionLevel
Of course, it can be improved in many ways. It's just an example. There's a limit to the recursion level, just to have a stronger query.

How do I force my T-SQL query to output unmatched values as zero?

I am using SQL Server 2014 and I have a simple T-SQL query (shown below) with its corresponding output.
use mydatabase
select *
from Table1
where ID in (101, 102, 103)
Output is as follows (meaning ID 102 does not exist in Table1):
ID Age
101 46
103 50
I want the output to be as follows:
ID Age
101 46
102 0
103 50
When there is no match for an ID in the look-up table, the output omits those IDs. How do I change my T-SQL query to ensure that unmatched IDs are also output but with zeroes.
If you have 2 or more tables and you want to display some default values if no match is found in one table, then you can use an outer join in SQL.
For Example :
I have 2 tables TableA and TableB.
When I'm doing an inner join I will get records that have matches in both Tables. Suppose I need to select id 1,2 and 3 from both tables and even if one id does not exist in TableB I want the records as NULL, then I can use this
select
A.Id,
A.Name,
Age = ISNULL(B.Age,0)
from TableA a
left join TableB b
on A.id = b.id
Here Age will be displayed as 0 if there is no matching record for a id in tableB
If don't have table that has the values which you are looking, then you could do that by using join and replace the mismatch values by 0 with help of coalesce() or isnull() function
select a.id, coalesce(t.age, 0) [Age] from <table> t
right join (
values (101), (102), (103)
) a(id) on a.id = t.id
Result :
id Age
101 46
102 0
103 50
SELECT T0.ID
ISNULL(T1.Age,0) AS Age
FROM Table0 T0
LEFT JOIN Table1 T1 ON T0.ID = T1.ID
WHERE T0.ID IN (101,102,103);

How to find the cumulative sum in SubQuery? [duplicate]

declare #t table
(
id int,
SomeNumt int
)
insert into #t
select 1,10
union
select 2,12
union
select 3,3
union
select 4,15
union
select 5,23
select * from #t
the above select returns me the following.
id SomeNumt
1 10
2 12
3 3
4 15
5 23
How do I get the following:
id srome CumSrome
1 10 10
2 12 22
3 3 25
4 15 40
5 23 63
select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from #t t1
inner join #t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id
SQL Fiddle example
Output
| ID | SOMENUMT | SUM |
-----------------------
| 1 | 10 | 10 |
| 2 | 12 | 22 |
| 3 | 3 | 25 |
| 4 | 15 | 40 |
| 5 | 23 | 63 |
Edit: this is a generalized solution that will work across most db platforms. When there is a better solution available for your specific platform (e.g., gareth's), use it!
The latest version of SQL Server (2012) permits the following.
SELECT
RowID,
Col1,
SUM(Col1) OVER(ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId
or
SELECT
GroupID,
RowID,
Col1,
SUM(Col1) OVER(PARTITION BY GroupID ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId
This is even faster. Partitioned version completes in 34 seconds over 5 million rows for me.
Thanks to Peso, who commented on the SQL Team thread referred to in another answer.
For SQL Server 2012 onwards it could be easy:
SELECT id, SomeNumt, sum(SomeNumt) OVER (ORDER BY id) as CumSrome FROM #t
because ORDER BY clause for SUM by default means RANGE UNBOUNDED PRECEDING AND CURRENT ROW for window frame ("General Remarks" at https://msdn.microsoft.com/en-us/library/ms189461.aspx)
Let's first create a table with dummy data:
Create Table CUMULATIVESUM (id tinyint , SomeValue tinyint)
Now let's insert some data into the table;
Insert Into CUMULATIVESUM
Select 1, 10 union
Select 2, 2 union
Select 3, 6 union
Select 4, 10
Here I am joining same table (self joining)
Select c1.ID, c1.SomeValue, c2.SomeValue
From CumulativeSum c1, CumulativeSum c2
Where c1.id >= c2.ID
Order By c1.id Asc
Result:
ID SomeValue SomeValue
-------------------------
1 10 10
2 2 10
2 2 2
3 6 10
3 6 2
3 6 6
4 10 10
4 10 2
4 10 6
4 10 10
Here we go now just sum the Somevalue of t2 and we`ll get the answer:
Select c1.ID, c1.SomeValue, Sum(c2.SomeValue) CumulativeSumValue
From CumulativeSum c1, CumulativeSum c2
Where c1.id >= c2.ID
Group By c1.ID, c1.SomeValue
Order By c1.id Asc
For SQL Server 2012 and above (much better performance):
Select
c1.ID, c1.SomeValue,
Sum (SomeValue) Over (Order By c1.ID )
From CumulativeSum c1
Order By c1.id Asc
Desired result:
ID SomeValue CumlativeSumValue
---------------------------------
1 10 10
2 2 12
3 6 18
4 10 28
Drop Table CumulativeSum
A CTE version, just for fun:
;
WITH abcd
AS ( SELECT id
,SomeNumt
,SomeNumt AS MySum
FROM #t
WHERE id = 1
UNION ALL
SELECT t.id
,t.SomeNumt
,t.SomeNumt + a.MySum AS MySum
FROM #t AS t
JOIN abcd AS a ON a.id = t.id - 1
)
SELECT * FROM abcd
OPTION ( MAXRECURSION 1000 ) -- limit recursion here, or 0 for no limit.
Returns:
id SomeNumt MySum
----------- ----------- -----------
1 10 10
2 12 22
3 3 25
4 15 40
5 23 63
Late answer but showing one more possibility...
Cumulative Sum generation can be more optimized with the CROSS APPLY logic.
Works better than the INNER JOIN & OVER Clause when analyzed the actual query plan ...
/* Create table & populate data */
IF OBJECT_ID('tempdb..#TMP') IS NOT NULL
DROP TABLE #TMP
SELECT * INTO #TMP
FROM (
SELECT 1 AS id
UNION
SELECT 2 AS id
UNION
SELECT 3 AS id
UNION
SELECT 4 AS id
UNION
SELECT 5 AS id
) Tab
/* Using CROSS APPLY
Query cost relative to the batch 17%
*/
SELECT T1.id,
T2.CumSum
FROM #TMP T1
CROSS APPLY (
SELECT SUM(T2.id) AS CumSum
FROM #TMP T2
WHERE T1.id >= T2.id
) T2
/* Using INNER JOIN
Query cost relative to the batch 46%
*/
SELECT T1.id,
SUM(T2.id) CumSum
FROM #TMP T1
INNER JOIN #TMP T2
ON T1.id > = T2.id
GROUP BY T1.id
/* Using OVER clause
Query cost relative to the batch 37%
*/
SELECT T1.id,
SUM(T1.id) OVER( PARTITION BY id)
FROM #TMP T1
Output:-
id CumSum
------- -------
1 1
2 3
3 6
4 10
5 15
Select
*,
(Select Sum(SOMENUMT)
From #t S
Where S.id <= M.id)
From #t M
You can use this simple query for progressive calculation :
select
id
,SomeNumt
,sum(SomeNumt) over(order by id ROWS between UNBOUNDED PRECEDING and CURRENT ROW) as CumSrome
from #t
There is a much faster CTE implementation available in this excellent post:
http://weblogs.sqlteam.com/mladenp/archive/2009/07/28/SQL-Server-2005-Fast-Running-Totals.aspx
The problem in this thread can be expressed like this:
DECLARE #RT INT
SELECT #RT = 0
;
WITH abcd
AS ( SELECT TOP 100 percent
id
,SomeNumt
,MySum
order by id
)
update abcd
set #RT = MySum = #RT + SomeNumt
output inserted.*
For Ex: IF you have a table with two columns one is ID and second is number and wants to find out the cumulative sum.
SELECT ID,Number,SUM(Number)OVER(ORDER BY ID) FROM T
Once the table is created -
select
A.id, A.SomeNumt, SUM(B.SomeNumt) as sum
from #t A, #t B where A.id >= B.id
group by A.id, A.SomeNumt
order by A.id
The SQL solution wich combines "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" and "SUM" did exactly what i wanted to achieve.
Thank you so much!
If it can help anyone, here was my case. I wanted to cumulate +1 in a column whenever a maker is found as "Some Maker" (example). If not, no increment but show previous increment result.
So this piece of SQL:
SUM( CASE [rmaker] WHEN 'Some Maker' THEN 1 ELSE 0 END)
OVER
(PARTITION BY UserID ORDER BY UserID,[rrank] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Cumul_CNT
Allowed me to get something like this:
User 1 Rank1 MakerA 0
User 1 Rank2 MakerB 0
User 1 Rank3 Some Maker 1
User 1 Rank4 Some Maker 2
User 1 Rank5 MakerC 2
User 1 Rank6 Some Maker 3
User 2 Rank1 MakerA 0
User 2 Rank2 SomeMaker 1
Explanation of above: It starts the count of "some maker" with 0, Some Maker is found and we do +1. For User 1, MakerC is found so we dont do +1 but instead vertical count of Some Maker is stuck to 2 until next row.
Partitioning is by User so when we change user, cumulative count is back to zero.
I am at work, I dont want any merit on this answer, just say thank you and show my example in case someone is in the same situation. I was trying to combine SUM and PARTITION but the amazing syntax "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" completed the task.
Thanks!
Groaker
Above (Pre-SQL12) we see examples like this:-
SELECT
T1.id, SUM(T2.id) AS CumSum
FROM
#TMP T1
JOIN #TMP T2 ON T2.id < = T1.id
GROUP BY
T1.id
More efficient...
SELECT
T1.id, SUM(T2.id) + T1.id AS CumSum
FROM
#TMP T1
JOIN #TMP T2 ON T2.id < T1.id
GROUP BY
T1.id
Try this
select
t.id,
t.SomeNumt,
sum(t.SomeNumt) Over (Order by t.id asc Rows Between Unbounded Preceding and Current Row) as cum
from
#t t
group by
t.id,
t.SomeNumt
order by
t.id asc;
Try this:
CREATE TABLE #t(
[name] varchar NULL,
[val] [int] NULL,
[ID] [int] NULL
) ON [PRIMARY]
insert into #t (id,name,val) values
(1,'A',10), (2,'B',20), (3,'C',30)
select t1.id, t1.val, SUM(t2.val) as cumSum
from #t t1 inner join #t t2 on t1.id >= t2.id
group by t1.id, t1.val order by t1.id
Without using any type of JOIN cumulative salary for a person fetch by using follow query:
SELECT * , (
SELECT SUM( salary )
FROM `abc` AS table1
WHERE table1.ID <= `abc`.ID
AND table1.name = `abc`.Name
) AS cum
FROM `abc`
ORDER BY Name

Select only unique values

Tabls is
ID Count
1 30
2 30
3 10
4 15
5 10
6 25
I want query which will give me
4 15
6 25
in result
You can use NOT EXISTS:
SELECT ID, Count
FROM dbo.TableName t1
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.TableName t2
WHERE t1.ID <> t2.ID AND t1.Count = t2.Count
)
Demo
The following should select what you want:
SELECT t.ID, t.[Count]
FROM Table t
WHERE
(SELECT COUNT(*) FROM Table t1 WHERE t1.[Count] = t.[Count]) = 1
Please note that you should really have an index on Table.[Count].
you could also do it with a grouping statement
SELECT MIN(ID), Count
FROM Table
GROUP BY Count
HAVING COUNT(*) = 1
Use HAVING together with COUNT DISTINCT, to limit the result:
SELECT [Id], [Count]
FROM MyTable
WHERE [Count] IN (
SELECT [Count]
FROM MyTable
GROUP BY [Count]
HAVING COUNT(DISTINCT [Count]) = 1
)

Select rows with column with min value

I need to select the rows with the minimum distance by grouping on the OrganisationID. Here is my data in a single table:
ID OrganisationID Distance
0 10 100
1 10 200
3 10 50
4 20 80
5 20 300
This is the result I want:
ID OrganisationID Distance
3 10 50
4 20 80
This will accomplish that:
SELECT t1.*
FROM yourTable t1
LEFT JOIN yourTable t2
ON (t1.OrganisationID = t2.OrganisationID AND t1.Distance > t2.Distance)
WHERE t2.OrganisationID IS NULL;
sqlfiddle demo
Note that if there are multiple rows with the lowest distance duplicate, this returns them both
EDIT:
If, as you say in the comments, only want one column and the MIN distance you can do it easily with MIN and GROUP BY:
SELECT city, MIN(distance)
FROM table2
GROUP BY city;
sqlfiddle demo
p.s. i saw your previous question that you deleted, and was answering it with a different thing than this (was going to tell you that since you had the organisationID in the WHERE clause, you could just do: SELECT TOP 1 ... order by Distance DESC), but if you need more it for more than one organisationID, this is something that can get you there)
This is the solution:
SELECT ID ,D.*
FROM <TABLE> INNER JOIN( SELECT OrganisationID 'OR',MIN(Distance) DI
FROM <TABLE>
GROUP BY OrganisationID) D
ON D.DI=<TABLE>.Distance
Test :
CREATE TABLE #T
(
ID INT,
OrganisationID INT,
Distance INT
)
INSERT INTO #T
SELECT 0,10,100
UNION ALL
SELECT 1,10,200
UNION ALL
SELECT 3,10,50
UNION ALL
SELECT 4,20,80
UNION ALL
SELECT 5,20,300
SELECT ID ,D.*
FROM #T INNER JOIN( SELECT OrganisationID 'OR',MIN(Distance) DI
FROM #T
GROUP BY OrganisationID) D
ON D.DI=#T.Distance
DROP TABLE #T

Resources