原来一直以为join后面写个查询语句,sql server会自己生成一个worktable,然后再进行联合查询,但是今天的三观被刷新了一次。
book_month_deli是一张统计每月信息的表,book_cost是记录流水账的表。下面sql就简单地写:
语句1
SELECT *
FROM book_month_deli a WITH(NOLOCK)LEFT JOIN (SELECT
t1.shopid AS shopid
,SUM(t1.busisum) AS delisum
FROM book_cost t1 WITH(NOLOCK)
WHERE t1.billdate='2016-10-08'
GROUP BY t1.shopid
) b ON a.shopid = b.shopid
WHERE a.billmonth>='2016-05' AND a.shopid='00000040'
语句2
SELECT *
FROM book_month_deli a WITH(NOLOCK)
LEFT JOIN (SELECT
t1.shopid AS shopid
,SUM(t1.busisum) AS delisum
FROM book_cost t1 WITH(NOLOCK)
WHERE t1.billdate='2016-10-08'
GROUP BY t1.shopid
) b ON a.shopid =