如何在SQL Server 2014中的子查询中选择多个字段? [英] How to select multiple fields in a subquery in SQL server 2014 ?

查看:541
本文介绍了如何在SQL Server 2014中的子查询中选择多个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想在Sql Server 2014的子查询中选择多个列。



谁能知道怎么做以及怎么做?



谢谢!



问候:

Asim Mughal



我的尝试:



我尝试过如下...



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 ...

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.

推荐答案

目前还不清楚你要用列做什么

It's not clear what you are trying to do with the "Column"
(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

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



但这确实是一种糟糕的方式。

看起来那些总数应该是由tbl_Stock .RecordItem在这种情况下它应该只是


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),



或者你可以有一个子-query as another join。



如果没有然后帮助使用改进问题链接,从每个表中添加一些样本数据和预期结果(对于样本数据)。如果您回复此解决方案,我将收到有关更改的通知,我将再看看


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


这篇关于如何在SQL Server 2014中的子查询中选择多个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆