Click here to Skip to main content
15,180,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello to All,

I want to Select Multiple Columns in a Sub Query in Sql Server 2014.

Can anyone have some idea what and how to do that ??

Thanks!

Regards:
Asim Mughal

What I have tried:

I tried it as below ...

SQL
Select type_o.Type, shap_o.Shape, siz_o.Size,
CASE When itm_o.RecordNoShape<=3 Then 
type_o.Type + ' ' + siz_o.Size Else type_o.Type + ' ' + shap_o.Shape + ' ' + siz_o.Size End As 'ItemDescription',

(Select SUM(stk_i.ItemIn) As 'In_', SUM(stk_i.ItemOut) As 'Out_' From tbl_Stock stk_i),

SUM(stk_o.ItemIn)-SUM(stk_o.ItemOut) As 'NetStock'

From  
tbl_Stock stk_o, tbl_Items itm_o, tbl_i_Sizes siz_o, tbl_i_Shapes shap_o, tbl_i_Types type_o
Where  
stk_o.RecordNoItem=itm_o.RecordNoItem And itm_o.RecordNoSize=siz_o.RecordNoSize And itm_o.RecordNoType=type_o.RecordNoType And 
itm_o.RecordNoShape=shap_o.RecordNoShape And itm_o.RecordNoType=1
Group By  
itm_o.RecordNoSize, itm_o.RecordNoShape, siz_o.Size, shap_o.Shape, type_o.Type
Having SUM(stk_o.ItemIn)-SUM(stk_o.ItemOut)!=0


But it gives the following Error.

Msg 116, Level 16, State 1, Line 5
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Posted
Updated 5-Apr-16 7:24am
v2
Comments
pandu web dev 5-Apr-16 11:59am
   
I know mysql. I think you are messing up with logical operators like in or not in etc. One way to avoid this error is to provide only one expression, or one column, in the SELECT subquery statement. This is said in some documentation.
Suvendu Shekhar Giri 5-Apr-16 12:19pm
   
Never came across such requirement. Not sure if this kind of feature is available in SQL Server 2014, in fact any RDBMS.
I think, you need write separate subqueries for each of the column/expression.

1 solution

It's not clear what you are trying to do with the "Column"
JavaScript
(Select SUM(stk_i.ItemIn) As 'In_', SUM(stk_i.ItemOut) As 'Out_' From tbl_Stock stk_i),


If you are trying to return two values from that sub-query then you can't.

If you want both values as columns then you have to separate them out
JavaScript
(Select SUM(stk_i.ItemIn) As 'In_' From tbl_Stock), (Select SUM(stk_i.ItemOut) As 'Out_' From tbl_Stock stk_i),

But that really is an awful way to do it.
It looks like those totals are supposed to be by tbl_Stock.RecordItem in which case it should just be
SUM(stk_o.ItemIn), SUM(stk_o.ItemOut),

Or you could have a sub-query as another join.

If this doesn't help then use the Improve question link to add some sample data from each of the tables and your expected result (for the sample data). If you then reply to this solution I will be notified of the change and I will have another look
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900