SQL IN Operator with Select - sql

I want to use the IN Operator in SQL with a select statement but I get the following error "Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
The code that I am using looks as follows:
select * from Table where ID in (select Units from Table2)

You have use WHEN instead of WHERE
SELECT * FROM `Table` WHERE `ID` IN (SELECT `Units` FROM `Table2`)

You should use where in place of when in your query. The correct query will be:
select * from Table where ID in (select Units from Table2)

You should try to select specific ID columns in your second query, something like this:
Select*from Table where ID IN (Select ID from Table2)

Related

Is it possible to use a subquery more than once?

I was wondering if it was possible to use a subquery in SQL more than once.
For example:
with subQuery as (
select id from someTable1
)
update someTable2
set someValue = 1
where id in (select * from subQuery)
select * from someTable2
where id in (select * from subQuery)
...
As of right now, SQL throws an error on the (select * from subQuery) in the select * someTable2 clause saying "Invalid Object Name subQuery". So is there a way to use subQuery more than once without having to add a table or run the query multiple times changing out the first statement?
A CTE is in scope only for a single query, but a query can both UPDATE and OUTPUT data. eg
with subQuery as (
select id from someTable1
)
update someTable2
set someValue = 1
output inserted.*
where id in (select * from subQuery)

Group by using a function result SQL Server

How to group the entire SQL query using scalar valued function result.
SELECT Col1,Col2,udf_IsNumaric_Get(Param1,Param2)
FROM Table1
WHERE Col1 LIKE '%123%'
GROUP BY --Function result
ORDER BY --Function result
OR
SELECT Col1,Col2,(SELECT COUNT(*) FROM Table2) AS Count
FROM Table1
WHERE Col1 LIKE '%123%'
GROUP BY --Count
ORDER BY --Count
Is it possible to group the entire SQL query by using the output of the udf_IsNumaric_Get scalar valued function or using the SELECT statement in the 2nd example..
You can achieve it using a subquery:
SELECT t.Col1,t.Col2,t.Count FROM
(SELECT Col1,Col2,(SELECT COUNT(*) FROM Table2) AS Count
FROM Table1
WHERE Col1 LIKE '%123%') as t
GROUP BY Count
ORDER BY Count

Using a value from one query in second query sql

SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
This produces the result
AS COUNT
5 2
I then want to use the AS value in another query and only output the end result. Is this possible.i was thinking something like.
SELECT *
FROM
TABLE 2
Where AS =(
SELECT AS, COUNT(*)
FROM Table1
HAVING COUNT(AS)>1
group BY AS;
);
This is called a subquery. To be safe, you would use in instead of = (and as is a bad name for a column, because it is a SQL key word):
SELECT *
FROM TABLE2
WHERE col IN (SELECT col
FROM Table1
GROUP BY col
HAVING COUNT(col) > 1
);
Your first query is also incorrect, because the having clause goes after the group by.
You could use a subquery with the in operator:
SELECT *
FROM table2
WHERE AS IN (SELECT AS
FROM table1
GROUP BY AS
HAVING COUNT(*) > 1)

Select All Values From Table That Match All Values of Subquery

So I have a list of values that is returned from a subquery and would like to select all values from another table that match the values of that subquery. Is there a particular way that's best to go about this?
So far I've tried:
select * from table where tableid = select * from table1 where tableid like '%this%'
select * from table where tableid in(select tableid
from table1
where tableid like '%this%')
select * from table where tableid IN
(select tableid from table1 where tableid like '%this%')
A sub-query needs to return what you are asking for. Additionally, if there's more than 1 result, you need IN rather than =
This will work
select * from table where tableid in
(select tableid from table1 where tableid like '%this%')
= works when subquery returns 1 record only
in works when subquery returns 1 or more than one record only
I am reading a SQL Server book right now, and it highlights doing this using the EXISTS statement in the WHERE clause for speed and efficiency purposes. Here's a potential solution.
SELECT
*
FROM
tableName AS t
WHERE
EXISTS(
SELECT
1
FROM
table1 AS s --as in subtable or something like that
WHERE
t.tableid = s.tableid
AND
s.tableid like '%this%'
)

In MySql how do I get row duplication for the case where I pass duplicate IDs in a list?

For this MySQL SELECT statement:
SELECT * FROM MY_TABLE WHERE ID IN(x,y,y,z):
I want 4 rows back - ie I WANT row duplication for the case where I pass duplicate IDs in the list.
Is this possible?
using the IN() construct, that's not possible.
the only way i can think to do this is with a UNION:
SELECT * FROM my_table WHERE id = x
UNION ALL
SELECT * FROM my_table WHERE id = y
UNION ALL
SELECT * FROM my_table WHERE id = y
UNION ALL
SELECT * FROM my_table WHERE id = z
but in all honesty, i would just do the IN() like you have it and make your app code duplicate the rows as needed.
Put your IDs, including dups in a temp table and join your results on that table. The join will take care of filtering, but will keep duplicates if it's in the temp table twice
SELECT * FROM MY_TABLE WHERE ID IN(x,y,z)
union all
SELECT * FROM MY_TABLE WHERE ID IN(y)
To me, IN specify a set of values to search in (and duplication is a concept that conflict with the set one).
You should use other mean to reach your scope.