I have a SQL query where I have in my where clause a list into a IN condition.
Example of what I mean:
WHERE Id IN (11111,11112)
And what I try to do is the following: I have a linked server query where I have an IN clause.
And I have a subquery where I use a JOIN table to cross data but I need that in the IN condition use the same Id like the main query.
I hope you guys understand what I try to do:
Here's my code:
SELECT
Name,
Street,
Number,
(SELECT loginUser FROM [LinkedServer].[Database].[dbo].[Users] T1
INNER JOIN [LinkedServer].[Database].[dbo].[General] T2
ON T2.IdUser = T1.Id
WHERE T2.Id IN (11111,11112,11113,11114,11115)
)
FROM [LinkedServer].[Database].[dbo].[General]
WHERE Id IN (11111,11112,11113,11114,11115)
i get this error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
To reuse same set of ids you could use Table Variable.
DECLARE #T TABLE(ID INT);
INSERT INTO T
SELECT 11111 UNION ALL SELECT 11112 ....
then
SELECT * FROM
(SELECT Varchar1,
Varchar2,
Varchar3, loginUser FROM [LinkedServer].[Database].[dbo].[Table] T1
INNER JOIN [LinkedServer].[Database].[dbo].[Table] T2
ON T2.INT1 = T1.INT1
WHERE T2.Id IN (select id from #T)
) AS X WHERE
X.Id IN (select id from #T)
Consider using a join instead of a correlated-subquery:
SELECT General.Name
, General.Street
, General.Number
, Users.loginUser
FROM [LinkedServer].[Database].[dbo].[General]
LEFT
JOIN [LinkedServer].[Database].[dbo].[Users]
ON Users.Id = General.IdUser
WHERE General.Id IN (11111, 11112, 11113, 11114, 11115)
This will return the loginUser where possible.
i have solved it finally.
The Problem was that the subquery havenĀ“t a where condition to extract a specific Id.
Without a WHERE Condition the query return more than 1 value.
What i have done:
Thanks for the advice and his solution Author: "LIUFA".
With a combination of his code and some correction of my code i get the solution.
I have add one more column Id and give it an alias. In the subquery i have done the select from the Temp table
using a where condition. And its Working. Perfect.
Hope that this solution can help somebody to solved a related issue.
Finally Code:
DECLARE #T TABLE(ID INT);
INSERT INTO #T
Select ReferenceId From MyBigData
SELECT,
A.Id,
Name,
Street,
Number,
(SELECT loginUser FROM [LinkedServer].[Database].[dbo].[Users] T1
INNER JOIN [LinkedServer].[Database].[dbo].[General] T2
ON T2.IdUser = T1.Id
WHERE T2.Id IN (SELECT Id FROM #T WHERE Id = A.Id)
)
FROM [LinkedServer].[Database].[dbo].[General]
WHERE Id IN (SELECT Id FROM #T)
Related
I am looking to left join another table because there are two columns in that table that I need to add to my query..how can I left join onto my existing query? For example the query I am using is similar to the one below..
select subject, sum(cnt_daily) as cnt,
min(cnt_daily) as min_cnt_daily, max(cnt_daily) as max_cnt_daily
from (
select study_date, subject, count(*) as cnt_daily
from mytable
where study_date >= '2022-01-01'
group by study_date, subject
) t
group by subject
I tried
select *
from mytable
left join table2
on mytable.id= table1.id
order by table1.id;
But i know this isnt right
You may use CTE:
WITH t AS (
select study_date, subject, count(*) as cnt_daily
from mytable
where study_date >= '2022-01-01'
group by study_date, subject
)
select subject, sum(cnt_daily) as cnt,
min(cnt_daily) as min_cnt_daily, max(cnt_daily) as max_cnt_daily
from t
group by subject
This could encourage reuse of the query inside CTE.
SELECT DISTINCT(t1.Ticker),t2.SecurityID,t2.ClosePrice,t2.QuoteDateTime FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
WHERE t2.QuoteDateTime IN (SELECT max(QuoteDateTime) FROM [Hub].[SecurityMaster].[SecurityPrices]) AND t1.SecurityTypeName = 'REIT'
I get an output with no data. The subquery doesn't run along with the other filter in the WHERE clause. I am not sure what I am doing wrong. Can somebody please help!
If you are trying to get the lastest row from SecurityPrices for each Ticker, one option is to use cross apply():
select --distinct /* distinct not needed if `Ticker` is unique on `smd`
smd.Ticker
, sp.SecurityID
, sp.ClosePrice
, sp.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityMasterDetails] as smd
cross apply (
select top 1
i.SecurityID
, i.ClosePrice
, i.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityPrices] i
where i.SecurityID = smd.SecurityID
order by i.QuoteDateTime desc
) as sp
where SecurityTypeName = 'REIT' /* which table does this column belong to? */
I think your query would be
SELECT DISTINCT TOP 1 WITH TIES
t1.Ticker,
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2 ON t2.SecurityID =t1.SecurityID
WHERE SecurityTypeName = 'REIT'
ORDER BY t2.QuoteDateTime DESC
You aren't getting results because the max(QuoteDateTime) record doesn't have SecurityTypeName = 'REIT'. I think you want the max(QuoteDateTime) for this SecurityTypeName, so this can be done with an INNER JOIN.
SELECT DISTINCT
(t1.Ticker),
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
INNER JOIN
(SELECT max(QuoteDateTime) DT FROM [Hub].[SecurityMaster].[SecurityPrices]) P on P.DT = t2.QuoteDateTime
WHERE SecurityTypeName = 'REIT'
EDIT
Your data doesn't have what you think it does, I suspect. Here is how you can check...
--Find the SecurityID that matches the max date
SELECT
SecurityID ,
max(QuoteDateTime) DT
FROM [Hub].[SecurityMaster].[SecurityPrices]
GROUP BY SecurityID
--I'm betting this ID isn't in your SecurityMasterDetails where the Type is REIT
SELECT DISTINCT
SecurityID
FROM SecurityMasterDetails
WHERE SecurityTypeName = 'REIT'
Since the SecurityID returned in the first query isn't in the second query result set, you are going to get NULL results.
I would like to add the count result of a 2nd query to the result set which queries the 1st table and connect this subquery with the ID of the 1st Table result. This simplified code explains it:
Select
ID_Field_Table1,
(Select Count(x)
From Table2
Where ID_Field_Table2 = ID_Field_Table1) As mycount
From Table1
What is the correct syntax?
Thanks
This code is simplified but the construction is the same. In my DB the ID_Field_Table2 and ID_Field_Table1 have the same name (let's say ID_Field). If I use this same Name, the SubQuery goes like "...Where ID_Field = ID_Field.." and this always Returns a Reslut, because it is not connecting the queries ("Select a Where x = x" returns any record in the table 2).
Select
ID_Field AS ID1,
(Select Count(x)
From Table2
Where ID_Field = ID1) As mycount
From Table1
It just returns an error, saying that ID1 does not exist.
When I use the original name of the columns (which are the same), the result is wrong
When I us an alias, an error occurs
So I think the query is wrong at all.
Well, you can't use an alias defined in the select clause, but you can use the aliases defined in the from clause:
Select
ID_Field AS ID1,
(Select Count(x)
From Table2
Where ID_Field = t1.ID_Field) As mycount
From Table1 t1
You can also do it without an alias, since the sub query is for a different table:
Select
ID_Field AS ID1,
(Select Count(x)
From Table2
Where ID_Field = Table1.ID_Field) As mycount
From Table1
However, I suspect a query using left join to a derived table as suggested in esta's answer would have better performance.
Something like this?
Select
Table1.ID_Field_Table1,
T2.MYCOUNT
From Table1
LEFT JOIN (SELECT ID_Field_Table2, COUNT(x) AS MYCOUNT FROM TABLE2 GROUP BY ID_FIELD_TABLE2) T2 ON T2.ID_Field_Table2=Table1.ID_Field_Table1
Try This query once it may help you.
Select ID_Field_Table1,COUNT_NBR
cross apply
(
Select Count(x) as COUNT_NBR
From Table2
Where ID_Field_Table2 = ID_Field_Table1
group by ID_Field_Table1
) As mycount
From Table1
I am writing a simple select subquery statement and it is not giving any result. Neither it is throwing any error.
My Sql query is like this -
select * from Table1
where id in (select ID from Table2 where user = 'xyz')
I tried with exists also, but not showing any result.
Appreciate your help.
It could be that the below subquery returns a empty list because the filter condition where user = 'xyz' doesn't matches any record
select ID from Table2 where user = 'xyz'
The outer query as below doesn't matches the condition where id in ()
select * from Table1 where id in ()
Thus returning an empty result set.
You can convert your posted query to be a INNER JOIN query like
select t1.*
from Table1 t1 join Table2 t2
on t1.id = t2.id
where t2.user = 'xyz';
As a test, try using LIKE operator instead of equality comparison
select * from Table1
where id in (select ID from Table2 where user like '%xyz%')
I have a table like so:
Id, Comment, LastUpdatedDate
I'm tyring to select the latest comment for that id. The table can have many comments on that id with different dates but I'm trying to get the latest date out of there. I've tried the following with no success:
SELECT tt.*
FROM tagtestresultcomment tt
INNER JOIN
(
SELECT tag_id, MAX(last_update) AS MaxDateTime
FROM tagtestresultcomment
GROUP BY tag_id
) groupedtt ON tt.tag_id = groupedtt.tag_id AND tt.last_update = groupedtt.MaxDateTime
order by tag_id
Does anyone have any ideas of how to achieve this?
Thanks!
It sounds like you want only the latest comment for each tag_id? In which case, here is one approach you can use from SQL 2005 and on:
;WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY tag_id ORDER BY last_update DESC) AS RowNo
FROM TagTestResultComment
)
SELECT * FROM CTE WHERE RowNo = 1
try this
Select * from tagtestresultcomment where last_update in
(select max(last_update) from tagtestresultcomment group by tag_id)
your query code is too redundant. first
tt.tag_id = groupedtt.tag_id AND tt.last_update = groupedtt.MaxDateTime
it's enough just
tt.tag_id = groupedtt.tag_id
and second, it's enough just
SELECT [desired field list extcept last_update and ],
tag_id,
MAX(last_update) AS MaxDateTime
FROM
tagtestresultcomment
group by
tag_id, [desired field list extcept last_update and tag_id]
at all to achieve your objective
I have tried something like this:
declare #tagtestresultcomment table
(
id int
, comment varchar(50)
,LastUpdatedDate datetime
)
--==== Populate table
insert into #tagtestresultcomment(id,comment,LastUpdatedDate)
select 1,'My name is Arthur','2011-06-09 00:00:00' union all
select 2,'My name is DW','2011-06-19 00:00:00' union all
select 2,'Arthur is my brother','2011-06-21 00:00:00' union all
select 1,'I have a sister named DW','2011-06-21 00:00:00' union all
select 3,'I am Muffy','2011-06-14 00:00:00' union all
select 3,'I like sports','2011-06-14 00:00:00'
-- SELECT stmt
select * from #tagtestresultcomment t
join
(
select id, MAX(lastupdateddate) as LastUpdatedDate from #tagtestresultcomment group by id
) m
on t.id = m.id
and t.LastUpdatedDate = m.LastUpdatedDate
The "MAX" group function wasn't working for me, so I used a sub-query. I had trouble wrapping my head around your single table example, so I'm using a common parent-child 1-to-many relationship with a blog and comment tables as an example.
SELECT
b.id,
b.content,
c.id,
c.blog_id,
c.content,
c.last_update
FROM blog b
INNER JOIN blog_comment c
ON b.id = c.blog_id AND c.id = (
SELECT TOP 1 id FROM blog_comment WHERE blog_id = b.id ORDER BY last_update DESC
)
The query takes a hit on my sub-query, as it will call that "SELECT TOP 1" query for each record in the blog table. I'd like to hear of a faster, more efficient example if possible.