Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
You can't return two (or multiple) columns in your subquery to do the comparison in the WHERE A_ID IN (subquery)
clause - which column is it supposed to compare A_ID
to? Your subquery must only return the one column needed for the comparison to the column on the other side of the IN
. So the query needs to be of the form:
SELECT * From ThisTable WHERE ThisColumn IN (SELECT ThatColumn FROM ThatTable)
You also want to add sorting so you can select just from the top rows, but you don't need to return the COUNT as a column in order to do your sort; sorting in the ORDER
clause is independent of the columns returned by the query.
Try something like this:
select count(distinct dNum)
from myDB.dbo.AQ
where A_ID in
(SELECT DISTINCT TOP (0.1) PERCENT A_ID
FROM myDB.dbo.AQ
WHERE M > 1 and B = 0
GROUP BY A_ID
ORDER BY COUNT(DISTINCT dNum) DESC)
ERROR SQL Server ONLY one expression can be specified in the select list when the subquery is not introduced with exists
Remove [STATUT_TITRE]
from sub-query as it will accept only one expression :
select c.CODE_ISIN
from cte c
where code_ISIN not in (select [CODE_ISIN] -- only one expression needed
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID = B.TITRE_ID
);
I would suggest to use NOT EXISTS
instead :
where not exists (select 1
from TT_TITRE A inner join
TT_STATUT_TITRE B
on A.TITRE_ID=B.TITRE_ID
where CODE_ISIN = c.CODE_ISIN
);
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS keeps failing
The issue is not in the where
clause, you are trying to put two values "CrdTID, CrdTCanBeShared" into your variable, which is wrong, the right and more clean way is:
DECLARE @IsCanBeSharedValid INT = ISNULL((
SELECT TOP 1 CrdTCanBeShared
FROM [dbo].[IH_CardTypes_Cat] (NOLOCK)
WHERE CrdTID = @TypeId AND CrdTCanBeShared = @CanBeShared
), -1)
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
You can't return two (or multiple) columns in your subquery to do the comparison in the WHERE A_ID IN (subquery)
clause - which column is it supposed to compare A_ID
to? Your subquery must only return the one column needed for the comparison to the column on the other side of the IN
. So the query needs to be of the form:
SELECT * From ThisTable WHERE ThisColumn IN (SELECT ThatColumn FROM ThatTable)
You also want to add sorting so you can select just from the top rows, but you don't need to return the COUNT as a column in order to do your sort; sorting in the ORDER
clause is independent of the columns returned by the query.
Try something like this:
select count(distinct dNum)
from myDB.dbo.AQ
where A_ID in
(SELECT DISTINCT TOP (0.1) PERCENT A_ID
FROM myDB.dbo.AQ
WHERE M > 1 and B = 0
GROUP BY A_ID
ORDER BY COUNT(DISTINCT dNum) DESC)
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. SQL
Use the APPLY
operator for the subquery:
Select
u.*,
sct.*
From
unikat u
OUTER APPLY
(
Select
COUNT(DISTINCT(s.DateSales)) as cDays,
SUM(s.Un) as Un,
SUM(s.VCS) as vcs
From
dbSales.dbo.Sales s
Where
s.IDX = u.IDX And
s.DateSales Between DATEADD(dd,-8, u.DateS) And DATEADD(dd,-1, u.DateS)
) AS sct
You can incorporate any amount of columns resulting from the APPLY
operator. Use CROSS APPLY
instead of OUTER APPLY
if you want to filter NULL
matching results.
Two Tables/Databases error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
You can't set the TotalSalesWeek1
to two columns (DISTINCT(tb_1.SubPart)
and tb_1.FinalItemSubPartQuantity * tb_2.SalesWeek1
).
I would suggest something like the following
SELECT
tb_1.SubPart,
SUM(tb_1.FinalItemSubPartQuantity) FinalItemSubPartQuantity,
SUM(tb_1.FinalItemSubPartQuantity * tb_2.SalesWeek1) TotalSalesWeek1
SUM(tb_1.FinalItemSubPartQuantity * tb_2.SalesWeek2) TotalSalesWeek2
FROM [009Reports].[dbo].[ANC Parts] tb_1
JOIN [555].[cache].[PurchasingSupplyChainNeeds] tb_2 ON tb_1.FinalPartNo = tb_2.ItemNo
GROUP BY tb_1.SubPart
The GROUP BY tb_1.SubPart
at the end says you want each unique SubPart on a row, the SUM
s in the SELECT
explain that you want those values summed for each group.
Related Topics
SQL - How to Store and Navigate Hierarchies
Possible to Perform Cross-Database Queries With Postgresql
Transpose Latest Rows Per User to Columns
Query With Left Join Not Returning Rows For Count of 0
How to Pass Variable as a Parameter in Execute SQL Task Ssis
How to Determine the Last Day of the Previous Month Using Postgresql
SQL Server:Transpose Rows to Columns
How Many Rows in a Database Are Too Many
Unknown Column in 'Field List' Error on MySQL Update Query
Strings as Primary Keys in SQL Database
MySQL: Invalid Use of Group Function
How to Generate a Range of Dates in SQL Server
How to Count Unique Items in Field in Access Query
How to Turn Identity_Insert on and Off Using SQL Server 2008