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
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
Hello,

Where you write something in sub query. You can write this in two ways.

1) SELECT ( SELECT COUNT(*) FROM table1 ) AS Main

  This means you only expect only one value from Table1 and this query will work because count(*) will return only one column and column name is  "Main" with count. It's as a scalar  value means only One value.

2) SELECT * FROM ( SELECT name FROM Table1) AS Main

In this case we are expecting whole table as out put and we wrote like "SELECT * FROM " and Main will be you table. not a column

And so when we select multiple value in first query like "SELECT ( SELECT name FROM table1 )" it will through error.

Hope it will clear your doubt .
WeTiAuthor Commented:
Ok but I want to show 2 results, one with count, one with data with multi result.
Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
Put it like

select * FROM  (
select name from... where...) As Name
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

WeTiAuthor Commented:
Its not possible to show: count1 (*) result in one column, count2 (*) in one column and Name multi rows here in one column?
Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
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.
WeTiAuthor Commented:
Its fine if it shows null on count1 and count2 on row 2+ its not a problem, how do i do this?
Mark WillsTopic AdvisorCommented:
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

Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
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.
Mark WillsTopic AdvisorCommented:
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

WeTiAuthor Commented:
I want this way:

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

Like that?
WeTiAuthor Commented:
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?
Mark WillsTopic AdvisorCommented:
Yes, it is possible....

select something.*, case when rn=1 then (select count(*) from sys.triggers where 1=1) else NULL end as something_else, 
                   case when rn=1 then (select count(*) from sys.triggers where 1=1) else NULL end as Another_Thing
 
from (select row_number() over (order by [name]) as rn, [name] from sys.triggers where 1=1) as something

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
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.
WeTiAuthor Commented:
Yes, I works as you said Mark. Showing Null now to result, thanks and also thanks for help Arifhusen.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.