查询结果与表和检索特定字段的比较

时间:2018-05-25 09:59:14

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

我的查询

site-packages/sqlalchemy/dialects/oracle/base.px

enter image description here

我想再次比较查询结果列

  • ./base.py:360:RESERVED_WORDS
  • SELECT stoMast.sStockistCode, stoMast.sStateName, stoMast.sDivision, stateMap.sRMCode FROM tblRSM_State_Mapping stateMap INNER JOIN tblStockistMaster stoMast ON stateMap.sStateName = stoMast.sStateName WHERE stateMap.sRMCode = 'MCNE04001' and stoMast.sDivision = 'CIDIS' except select sStockistCode, sStateName, sDivision, sRMCode From tblEntry
  • sStockistCode

sStateName具有相同的字段

  • sDivision
  • tblStockistMaster
  • sStockistCode 并检索STOCKIST NAME。

不知道如何将上述查询结果与表格进行比较。

2 个答案:

答案 0 :(得分:1)

也许您可以使用以下与CTE表达式

一起使用的SQL代码
;with cte as (
    SELECT
        stoMast.sStockistCode,
        stoMast.sStateName,
        stoMast.sDivision,
        stateMap.sRMCode
    FROM
        tblRSM_State_Mapping stateMap
    INNER JOIN
        tblStockistMaster stoMast ON
        stateMap.sStateName = stoMast.sStateName
    WHERE 
        stateMap.sRMCode = 'MCNE04001' 
        and 
        stoMast.sDivision = 'CIDIS'

    except

    select 
        sStockistCode,
        sStateName,
        sDivision,
        sRMCode
    From
        tblEntry
)
select
cte.*,
sm.sStockistName
from cte
left join tblStockistMaster as sm
    on sm.sStockistCode  = cte.sStockistCode  and
       sm.sStateName  = cte.sStateName and
       sm.sDivision = cte.sDivision 

答案 1 :(得分:0)

-- I retrieve the stockist Name
SELECT
    stoMast.sStockistName,
FROM
    tblRSM_State_Mapping stateMap
INNER JOIN
    tblStockistMaster stoMast
        ON stateMap.sStateName = stoMast.sStateName
-- I'm joining table entry If I can match
LEFT JOIN
    tblEntry tbl
        on tbl.sStockistCode = stoMast.sStockistName
        AND tbl.sStateName = stoMast.sStateName
        AND tbl.sDivision = stoMast.sDivision
        AND tbl.sRMCode = stateMap.sRMCode
WHERE 
    stateMap.sRMCode = 'MCNE04001' 
    and stoMast.sDivision = 'CIDIS'
-- And The the exept thing, I don't want that got a match with  the tableentry
    AND COALESCE(tbl.sStockistCode, tbl.sStateName, tbl.sDivision, tbl.sRMCode) is null

我希望这是你想要的。另一方面,请帮助我们了解您的来源(表格和数据的概念)以及您想要的结果。将更容易创建查询。