WeTi
asked on
SQL query with Multi Select column and multi result.
Dear expert
I got a sql query question.
Here is some exemple query:
Select (
Select Count (*) From ... Where... ) As something,
(
Select Count (*) From...Where...) As something.
Now here is my question, I need to do a new select like this:
select (
select name from... where...) As Name
This isnt working.. Error:
Subquery returned more than 1 value?
Anyone know how to do this?
Thanks
I got a sql query question.
Here is some exemple query:
Select (
Select Count (*) From ... Where... ) As something,
(
Select Count (*) From...Where...) As something.
Now here is my question, I need to do a new select like this:
select (
select name from... where...) As Name
This isnt working.. Error:
Subquery returned more than 1 value?
Anyone know how to do this?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put it like
select * FROM (
select name from... where...) As Name
select * FROM (
select name from... where...) As Name
ASKER
Its not possible to show: count1 (*) result in one column, count2 (*) in one column and Name multi rows here in one column?
Conceptually is not possible in sql server to have different number of rows in the same table for different columns.
But it you still want to show details.
You table will have total rows returned multiple row query and first row will have data for count1 and count2, rest of them will be null.
But it you still want to show details.
You table will have total rows returned multiple row query and first row will have data for count1 and count2, rest of them will be null.
ASKER
Its fine if it shows null on count1 and count2 on row 2+ its not a problem, how do i do this?
Exactly Arifhusen Ansari
When you do an inline subquery it is making a new column and must have only one value
When you do an inline subquery it is making a new column and must have only one value
Select (select count(*) from sys.triggers where 1=1) as Something
-- you are selecting a column named [Something]
-- it must be a single value - in this case it is a count(*)
But if I were to select (?) as something but there were multiple possibilities for (?) then I am not returning a single value, but multiple values so it becomes a record set. If I am selecting from a recordset, then it must be a real subquery as a set of records
select * from (select name from sys.triggers where 1=1) as something
-- here, Something is a derived table
You can do it using below query as well.
But this will reflect the same count for all the rows.
select *,(SELECT COUNT(*) FROM Table1) AS Count1, (SELECT COUNT(*) FROM Table2) AS Count2 FROM (
select name from... where...) As Name
But this will reflect the same count for all the rows.
Yes, it is possible, but add the inline subquery to your multirow query e.g.
select something.*, (select count(*) from sys.triggers where 1=1) as something_else, (select count(*) from sys.triggers where 1=1) as Another_Thing
from (select name from sys.triggers where 1=1) as something
ASKER
I want this way:
Count1 Count2 TIme Name
2134 2133 2018-9 Eric
null null null Eric2
null null null Eric3
Like that?
Count1 Count2 TIme Name
2134 2133 2018-9 Eric
null null null Eric2
null null null Eric3
Like that?
ASKER
Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
.... god... any other way than this?
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
.... god... any other way than this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By the way, I have been using sys.triggers where 1=1 because I was drafting an Article at the time. You would change that to represent your table and where clause. Hope that replacement was obvious to you...
Does that make sense ?
If not, if you provide the table names (etc), I will substitute the correct columns / tables / where clause for you.
Does that make sense ?
If not, if you provide the table names (etc), I will substitute the correct columns / tables / where clause for you.
ASKER
Yes, I works as you said Mark. Showing Null now to result, thanks and also thanks for help Arifhusen.
ASKER