SQL Server 将列添加到SQL表

v09wglhw  于 2022-12-26  发布在  其他
关注(0)|答案(3)|浏览(155)

我尝试在SQL中向此查询添加一列,如下所示-

nlejzf6q

nlejzf6q1#

可以使用xml将查询的所有结果存储到列中

CUSIP=(Select gs.holdingsymbol 
           from dbo.holdingsymbol_tbl gs where gs.holdingsymboltypeid=2 for xml path(''))
ni65a41a

ni65a41a2#

您不需要子查询,只需在JOIN上添加另一个条件,如下所示:

SELECT DISTINCT
    h.tradingitemid
    ,h.securityid
    ,h.currencyId
    ,CUSIP = hs.holdingsymbol
FROM cpr..holding_tbl h
INNER JOIN cpr..HoldingSymbol_tbl hs ON h.holdingid = hs.holdingId AND hs.holdingsymboltypeid = 2
WHERE h.userCompanyId = 10;
xpcnnkqh

xpcnnkqh3#

您可以尝试这样做:

CUSIP=(Select TOP 1 gs.holdingsymbol 
           from dbo.holdingsymbol_tbl gs where gs.holdingsymboltypeid=2)

相关问题