Sql 没有第一个表id的表联接?

Sql 没有第一个表id的表联接?,sql,join,Sql,Join,下面查询中的问题是,dm_fiscal_cal表的正确连接是如何工作的? 我知道您可以在没有pk fk关系的字段上加入,但这(在mom.approval\u wes=fc.week\u ending\u sunday上)与作业订单表无关时如何工作 SELECT jo.order_id ,cd.client_id ,st.staff_name ,c.company_name ,ma.market_code ,SUM(mom.gross_profit

下面查询中的问题是,dm_fiscal_cal表的正确连接是如何工作的? 我知道您可以在没有pk fk关系的字段上加入,但这(在mom.approval\u wes=fc.week\u ending\u sunday上)与作业订单表无关时如何工作

SELECT 
    jo.order_id
    ,cd.client_id
    ,st.staff_name
    ,c.company_name
    ,ma.market_code
    ,SUM(mom.gross_profit)
FROM 
---- main table for order information
-- anchoring the query in this table means only clients with orders will return
    job_order as jo
LEFT JOIN 
--- main mart table for "aqent staff" coordinator id is the agent
    dm_staffhier_d as st
        on jo.coordinator_id = st.person_id   
---- secondary table for clients and their markets each order is related to a "client / market" combination. 
--- You must join on both to identify the correct market for the client where the order exists
LEFT JOIN
    client_default as cd    
        on jo.client_id = cd.client_id and 
        jo.market_id=cd.market_id  
--- primary table for clients
--- each client id is represented once and only once in this tbl
LEFT JOIN 
client as c
    on cd.client_id=c.client_id

--- market lookup table. there are market_ids that relate to many objects (orders, clients, people, leads, ..etc)
--- always make sure you're anchoring the market_id (Fk) on the correct object for the query results. You can (and often do) need
--  to alias this table for multiple uses
LEFT JOIN
    market as ma
        on jo.market_id= ma.market_id   

---- main finanacial data mart tbl. each order fee for each week, for each client is represented.
--- you can join on order id for this query. You could use otherer relations ships for other queries
LEFT JOIN
    dm_mainordermetrics_f as mom
        on jo.order_id=mom.order_id

--- fiscal calendar tbl
--- we're joining this to the financial tbl because we're looking for GP$ in a certain period. That period is 
--- easily found by using the dm_fiscal_cal tbl (fiscal year 2017).
RIGHT JOIN
    dm_fiscal_cal as fc
        on mom.approval_wes = fc.week_ending_sunday
WHERE 
--- market requirement for LA, and BOS
    ma.short_description IN ('Los Angeles', 'Boston')
--- not a good solution for this - check having clause 
--    AND mom.gross_profit >0
    AND  fc.fiscal_year = 2017
GROUP BY 
     jo.order_id
    ,st.staff_name
    ,cd.client_id
    ,c.company_name
    ,ma.market_code

having sum(mom.gross_profit) > 0

order by 4,1
--- you should get 1509 rows

因此,您似乎对StackOverflow和SQL有了新的认识。首先,我试着回答你最初的问题

我的解释是,您在询问第二个
左连接(有时也称为
左外部连接
)是否是SQL中的有效语句,即使它既不包含
作业.order\u id
也不包含
财政.cal\u id

这个问题的答案是是该语句是有效的,它将运行。这并不意味着它一定会返回任何记录,也不意味着它正在做您希望它做的事情

如果这不是你想问的问题,我很抱歉,但这就引出了我的第二点

你应该编辑你的原始问题,以便更清楚地知道你想知道什么。如果你在询问之前自己做过任何研究,请提供你正在浏览的网站的链接。在查询的
FROM
子句中的表中添加相关列。最后,仔细想想你不明白的具体情况。也许可以突出显示问题中的代码行


所有这些都将帮助我们更好地回答您的问题

谢谢你的意见。我发现我的问题在另一个帖子中得到了回答。答案是“不需要主键。也不需要外键。只要数据类型匹配或转换为匹配,就可以在任意列上构造连接两个表的查询。不需要显式存在任何关系。”我很高兴您找到了解决方案。:)请记住我说过的关于将来提问的话。示例数据和期望的结果将非常有用。欢迎使用Stack Overflow。您误用了
分组依据
。请阅读。如果你的查询有效的话,它可能会产生不可预测的结果。在这里学习如何提问。