Sql 在复杂条件下联接3个表
我需要连接三个结构相同的表,如下所示 我需要根据store和date字段值连接这些表,如下所示: 这里复杂的部分是,即使一个表中的行与其他表不匹配,它也应该创建一个其他值为零的新行。例如,如果某一特定日期的门店没有发货和分配数量,但当天有取消数量,则应创建新行,将发货$allocated值设置为0,将cancelled field值设置为正确的值。有人能推荐最好的方法吗?这应该行得通Sql 在复杂条件下联接3个表,sql,Sql,我需要连接三个结构相同的表,如下所示 我需要根据store和date字段值连接这些表,如下所示: 这里复杂的部分是,即使一个表中的行与其他表不匹配,它也应该创建一个其他值为零的新行。例如,如果某一特定日期的门店没有发货和分配数量,但当天有取消数量,则应创建新行,将发货$allocated值设置为0,将cancelled field值设置为正确的值。有人能推荐最好的方法吗?这应该行得通 select alldates.procdate, alldates.stor
select alldates.procdate,
alldates.store,
st.quantity as ship_quantity,
ct.quantity as cancel_quantity,
at.quantity as allocation_quantity
from (select procdate, store from shp_table union
select procdate, store from cnl_table union
select procdate, store from aloc_table) alldates
left join shp_table st on alldates.procdate = st.procdate and alldates.store = st.store
left join cnl_table ct on alldates.procdate = ct.procdate and alldates.store = ct.store
left join aloc_table at on alldates.procdate = at.procdate and alldates.store = at.store
请提供所有三个表的示例数据和所需结果。适当的数据库标记会有所帮助。。请不要将样本数据作为图像发布。分配数量和取消数量来自何处?与上面发布的表格图像类似,还有两个表格用于分配和取消。