Link to home
Start Free TrialLog in
Avatar of WeTi
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
SOLUTION
Avatar of Arifhusen Ansari
Arifhusen Ansari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi
WeTi

ASKER

Ok but I want to show 2 results, one with count, one with data with multi result.
Put it like

select * FROM  (
select name from... where...) As Name
Avatar of WeTi

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.
Avatar of WeTi

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
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(*)

Open in new window

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

Open in new window

You can do it using below query as well.

select *,(SELECT COUNT(*) FROM Table1) AS Count1, (SELECT COUNT(*) FROM Table2) AS Count2 FROM  (
select name from... where...) As Name

Open in new window


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

Open in new window

Avatar of WeTi

ASKER

I want this way:

Count1            Count2          TIme          Name
2134                2133               2018-9        Eric
null                   null                 null             Eric2
null                   null                 null             Eric3

Like that?
Avatar of WeTi

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of WeTi

ASKER

Yes, I works as you said Mark. Showing Null now to result, thanks and also thanks for help Arifhusen.