SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS - sql

Can any one please help me regarding that error in SQL server
" SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
select * from
(select row_number() over ( order by (select null) ) rn,
(select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1)
)
where rn between 0 and 100

Try this:
select * from
(select row_number() over ( order by (select null) ) rn,a.test1,a.test2,a.test3
from (select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1) a
) b
where b.rn between 0 and 100

There are multi errors in your query:-
First one:-
Incorrect syntax near the keyword 'where'
Fix: Type alias for derived table (I am gonna type myTable as an alias below)
second one:-
No column name was specified for column 2 of 'myTable'
Fix: Type alias for derived column (I am gonna type myCol as an alias below)
Third one:- (Thant you mentioned in your question)
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Reason: You can't return two (or multiple) columns in your subquery.
The resolved Query:-
select * from
(
select
row_number() over ( order by (select null) ) rn,
(
select distinct test1
from table1
where table1.test1= 1
EXCEPT
select distinct test1
from table2
where table2.test1= 1
) myCol
) myTable
where rn between 0 and 100
Maybe the result is not what you need, but unless this a working query and now you have the keys for handling yours as your needs.

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)

Ambiguous Error while column name is used in order by clause

I have encountered one issue below
SELECT ColumnName1,*
FROM TableName
WHERE ColumnName = 'XXXXXX'
ORDER BY ColumnName1
From above query it throws error as Ambiguous column name Column Name1.
Even though I have not used number tables then too it throws error like this. Can anybody tell what the reason behind this.
Because the SELECT has two columns called ColumnName1 -- one placed there explicitly and one from the *. Here are some choices:
SELECT t.ColumnName1, t.*
FROM TableName t
WHERE t.ColumnName = 'XXXXXX'
ORDER BY t.ColumnName1
or:
SELECT t.ColumnName1, t.*
FROM TableName t
WHERE t.ColumnName = 'XXXXXX'
ORDER BY 1
or:
SELECT t.ColumnName1 as second_columnname1, t.*
FROM TableName t
WHERE t.ColumnName = 'XXXXXX'
ORDER BY second_columnname1
To resolve an error we have 3 options:
-- 1. give alias to the column
SELECT A AS A_STANDS_ALONE, *
FROM YourTable
ORDER BY A
-- 2. order by 1
SELECT A, *
FROM YourTable
ORDER BY 1
-- 3. give alias to the table and used it in column call and order by
SELECT t.A, *
FROM YourTable t
ORDER BY t.A

Select Id+1 from subquery on same table

I need to select all id + 1 and id - 1 row information from MYTABLE with condition that id exist in existing query on same table .
I tried:
SELECT *
FROM MYTABLE
WHERE
id - 1 IN (SELECT * FROM MYTABLE WHERE EXISTINGQUERY) AND
id + 1 IN (SELECT * FROM MYTABLE WHERE EXISTINGQUERY)
But it doesn't work.
Any suggestions?
It seems to me you may write your query by using join like below
SELECT t1.* FROM MYTABLE t1
join (SELECT * FROM MYTABLE WHERE EXISTINGQUERY) t2
on (t1.id-1=t2.id or t1.id+1=t2.id)
The following SELECT statement may be used that includes the substitution variable :id
SELECT s.*
FROM MYTABLE s
WHERE id in (:id - 1,:id + 1)
AND EXISTS ( SELECT 1 FROM MYTABLE s WHERE id =:id AND EXISTINGQUERY);
where colon(:) notation may be replaced with # or & depending on the database.

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)

Count rows in more than one table with tSQL

I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt