添加到sql查询以将数据从另一个表连接到当前表

时间:2017-11-30 15:54:16

标签: sql database excel

我在微软查询中有两个sqls,它们分别是两个单独的excel文件,我想在一个excel文件中创建一个主要的sql,它结合了两个sql代码 - 这样第二个sql(3列)数据[z.ANALYSTNAME,z.MASTERCITYCODE,z.MEXID])基于每个s.code中的第一列连接到第一个sql表,并且z.code是相同的。请参阅下面的2平方米,返回两个表:

 `select
    distinct
    s.code as SEDOL, s.ISIN, s.Shortname as StockShortname, s.Fullname as 
StockFullname, s.Status as StockStatus,
    s.PriceKey as ExShareKey, s.IssueCcy as PriceCcy,
    s.Catagory as Catagory,
    decode(Catagory,'E','Equity','U','Unit Trust','G','Fixed Interest', 
catagory) as CatagoryFull,
    s.DealCCY as DealingCCY, s.IncomeCcy, s.EarningsCCY, s.noOfHolders, 
 s.Class as AssetClass, s.Depot as StockDepot,
    s.StampScale as StampScale, s.REIT as REITFlag, s.LastBid,s.LastOffer, 
S.PriceDate as LastPriceDate,
    s.SecGroup as AssetGroup, ts.Fullname as AssetGroupDesc, ts.ShortName as 
AssetGroupShort,
    s.SecTypes as AssetType, tt.Fullname as AssetTypeDesc, tt.ShortName as 
AssetTypeShort,
    s.Indust as AssetSector, ti.Fullname as AssetSectorDesc,
    s.Secmicropal as AssetSubSector, tm.Fullname as AseetSubSectorDesc,
    s.SecCustom as AssetCustom, tsc.Fullname as AssetCustomDesc,
    s.dealstyle as assetDealStyle,
    ex.PriceFeed_code as exsharepricecode, ex.Country_Of_Incorporation as 
exsharecoi
    , round(nvl(h.Quantity,0),2) as TotalHoldingUnits
    , round(nvl(h.Quantity,0)*((s.LastBid+s.LastOffer)/2 + 
nvl(s.Accrued/100,0))/cs.LastBid,2) as HoldingsValueGBP
, sysdate as ValuesAsAtDate
    , nvl(s.risk_rating,nvl(tm.type,ti.Type)) as RiskNumber,
    case
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=1 then 0
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=2 then 0
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=3 then 0
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=4 then 0.5
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=5 then 0.5
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=6 then 0.5
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=7 then 1
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=8 then 1
        when nvl(s.risk_rating,nvl(tm.type,ti.Type))=9 then 1
        else null
    end as EqRiskWeight,
    s.Recommendation as StockRecommendation, s.recommenddate as 
RecommendationDate,
    s.ShareBenefits1 as Benefits1AlternateShareClass, s.ShareBenefits2 as 
ResearchStyle,
    s.ShareBenefits3 as PriceTarget, s.ShareBenefits4 as HouseModel,
FROM    cpam.Security s,
    cpam.TableSecGroup ts, cpam.TableSecTypes tt, cpam.TableIndust ti, 
cpam.TableSecmpal tm, cpam.TableSecCustom tsc,
    cpam.ExShare ex, 
    (SELECT     h.Asset, sum(h.Quantity) as Quantity
        FROM    cpam.Holdings h
        WHERE   h.status='A' and h.Quantity<>0
        GROUP BY h.Asset) h,
    cpam.Currencies cs
WHERE   s.Class = 'S' and s.code is not null 
    and S.SecGroup = ts.code(+)
    and S.SecTypes = tt.code(+)
    and S.Indust = ti.code(+)
    and S.Secmicropal = tm.code(+)
    and S.PriceKey = ex.code (+)
    and s.SecCustom = tsc.code(+)
    and s.code = h.asset(+)
    and s.IssueCcy = cs.Code(+) 
ORDER BY S.Shortname`

第二张表:

'SELECT z.code, z.ANALYSTNAME, z.MASTERCITYCODE, z.MEXID
FROM CPAM_QRY.VIEW_CAM_STOCK_UNIV_STKDATA z'

0 个答案:

没有答案