6张表取得数据 //
dpdim_dp_shop //shopID
dpods_rotategroupshop //rotate_id
dpods_rotategroupuser //status
dpods_apolloshopextend type
dpods_territory //territoryid
dpods_territoryshop //territoryname
dpdim_sales_bu //dpods_rotategroupuser中的Userid 是 dpdim_sales_bu中的login_id
SELECT
distinct //去重a.shop_id,
a.shopgrp_id,
b.RotateGroupId,
a.shop_name,
a.city_name,
a.cat0_name, --主分类 shopType
a.cat1_name,
b.Status,
b.employee_name,
b.employee_id,
e.Type, -- 是否大客户
b.groupid, -- bu
g.TerritoryName
From
(
SELECT
shop_id,
shopgrp_id,
shop_name,
city_name,
cat0_name,
cat1_name
From dpdim_dp_shop
WHERE hp_valid_end_dt = "3000-12-31" and cat0_name like '爱车'
) a
LEFT OUTER JOIN
(
SELECT
b.ShopID,
c.UserId,
b.RotateGroupId,
c.Status,
d.employee_id,
d.employee_name,
d.groupid
From
(SELECT ShopID,RotateGroupId From dpods_rotategroupshop WHERE hp_statdate = '<@P:121>' ) b
LEFT OUTER JOIN //结果集
(
SELECT
UserId,
RotateGroupId,
status
From dpods_rotategroupuser
where hp_statdate = '<@P:121>' //公司的时间控件
) c on c.RotateGroupId = b.RotateGroupId
LEFT OUTER JOIN
(
SELECT
login_id,
employee_id,
employee_name,
groupid
From dpdim_sales_bu
where hp_statdate = '<@P:121>'
) d on d.login_id = c.UserId
)b on b.ShopID = a.shop_id
LEFT OUTER JOIN
(
SELECT
ShopID,
Type
From dpods_apolloshopextend
WHERE hp_statdate = '<@P:121>'
) e on e.ShopID = a.shop_id
LEFT OUTER JOIN
(
SELECT m.ShopId,m.TerritoryId,n.TerritoryName
From
(
SELECT
ShopId, // m.ShopId ,在下面的结果集中 就是g.ShopId
TerritoryId
From dpods_territoryshop
where hp_statdate = '<@P:121>'
) m
LEFT OUTER JOIN
(
SELECT
ID,
TerritoryName
From dpods_territory
where hp_statdate = '<@P:121>'
) n on n.ID = m.TerritoryId
) g on g.ShopId = a.shop_id