DECLARE @a datetime
SET @a ='20150305' --DATE!起始日期
DECLARE @b datetime
set @b='20150305' --DATE!终止日期
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.amt),0) 今年销售金额,isnull (SUM(tp.qty),0) 今年销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
WHERE MP.ps_id='SA' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.cst_std),0) 今年样礼品金额,isnull (SUM(tp.qty),0) 今年样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND isnull(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.amt),0) 比期销售金额,isnull (SUM(tp.qty),0) 比期销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
WHERE MP.ps_id='SA' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.cst_std),0) 比期样礼品金额,isnull (SUM(tp.qty),0) 比期样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND isnull(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
如何拼才能显示如上的格式
------解决思路----------------------
DECLARE @a datetime
SET @a ='20150305' --DATE!起始日期
DECLARE @b datetime
set @b='20150305' --DATE!终止日期
;with s1 as(
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.amt),0) 今年销售金额,isnull (SUM(tp.qty),0) 今年销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
WHERE MP.ps_id='SA' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
),
s2 as(
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.cst_std),0) 今年样礼品金额,isnull (SUM(tp.qty),0) 今年样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND isnull(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
),
s3 as(
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.amt),0) 比期销售金额,isnull (SUM(tp.qty),0) 比期销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
WHERE MP.ps_id='SA' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
),
s4 as
(
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号, ct.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称,
isnull (SUM(tp.cst_std),0) 比期样礼品金额,isnull (SUM(tp.qty),0) 比期样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN cust c ON c.CUS_NO=mp.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=mp.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=tp.PRD_NO
left JOIN cust ct ON ct.cus_no=c.MAS_CUS
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND isnull(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY c.name,c.cus_no,ct.name,c.mas_cus,s.name,p.prd_no,p.name
)
select s1.店铺名称,s1.店铺代号,s1.归属系统名称,s1.归属系统代号,s1.业务员,s1.货品代号,s1.货品名称,
s1.今年销售数量,s1.今年销售金额,s2,今年样礼品数量,s2.今年样礼品金额,s3.比期销售数量,s3.比期销售金额,
s4.比期样礼品数量,比期样礼品金额
from s1
left join s2 on s1.店铺代号=s2.店铺代号 and s1.归属系统代号=s2.归属系统代号
and s1.业务员=s2.业务员 and s1.货品代号=s1.货品代号
left join s3 on s1.店铺代号=s3.店铺代号 and s1.归属系统代号=s3.归属系统代号
and s1.业务员=s3.业务员 and s1.货品代号=s1.货品代号
left join s4 on s1.店铺代号=s4.店铺代号 and s1.归属系统代号=s4.归属系统代号
and s1.业务员=s4.业务员 and s1.货品代号=s1.货品代号
------解决思路----------------------
--你的语句,很多多余的连接,那会影响效率
DECLARE @a DATETIME,@b DATETIME,@c DATETIME,@d DATETIME--你不是还要比期的日期吗
SET @a ='20150305' --DATE!起始日期
SET @b='20150305' --DATE!终止日期
SET @c=@a--如果不需要就设置它们和今年的日期一样
SET @d=@b
SELECT c.cus_no 店铺代号,c.name 店铺名称,c.mas_cus 归属系统代号,c.name 归属系统名称,s.name 业务员,p.prd_no 货品代号,p.name 货品名称
,T.amt1 今年销售金额
,T.qty1 今年销售数量
,T.cst_std2 今年样礼品金额
,T.qty2 今年样礼品数量
,T.amt3 比期销售金额
,T.qty3 比期销售数量
,T.cst_std4 比期样礼品金额
,T.qty4 比期样礼品数量
FROM(
SELECT COALESCE(T1.cus_no,T2.cus_no,T3.cus_no,T4.cus_no)cus_no
,COALESCE(T1.SAL_NO,T2.SAL_NO,T3.SAL_NO,T4.SAL_NO)SAL_NO
,COALESCE(T1.prd_no,T2.prd_no,T3.prd_no,T4.prd_no)prd_no
,T1.amt1,T1.qty1
,T2.cst_std2,T2.qty2
,T3.amt3,T3.qty3
,T4.cst_std4,T4.qty4
FROM(
SELECT mp.cus_no,mp.SAL_NO,tp.prd_no
,ISNULL(SUM(tp.amt),0)amt1,ISNULL(SUM(tp.qty),0)qty1--今年销售金额 及 今年销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO AND tp.PS_ID=mp.PS_ID
WHERE mp.ps_id='SA' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY mp.cus_no,mp.SAL_NO,tp.prd_no
)T1 FULL JOIN(
SELECT mp.cus_no,mp.SAL_NO,tp.prd_no
,ISNULL(SUM(tp.cst_std),0)cst_std2,ISNULL(SUM(tp.qty),0)qty2--今年样礼品金额 及 今年样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO=mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND ISNULL(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @a AND @b
GROUP BY mp.cus_no,mp.SAL_NO,tp.prd_no
)T2 ON T1.cus_no=T2.cus_no AND T1.SAL_NO=T2.SAL_NO AND T1.prd_no=T2.prd_no
FULL JOIN(
SELECT mp.cus_no,mp.SAL_NO,tp.prd_no
,ISNULL(SUM(tp.amt),0)amt3,ISNULL(SUM(tp.qty),0)qty3--比期销售金额 及 比期销售数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO = mp.PS_NO AND tp.PS_ID=mp.PS_ID
WHERE mp.ps_id='SA' AND tp.ps_dd BETWEEN @c AND @d
GROUP BY mp.cus_no,mp.SAL_NO,tp.prd_no
)T3 ON ISNULL(T1.cus_no,T2.cus_no)=T3.cus_no
AND ISNULL(T1.SAL_NO,T2.SAL_NO)=T3.SAL_NO
AND ISNULL(T1.prd_no,T2.prd_no)=T3.prd_no
FULL JOIN(
SELECT mp.cus_no,mp.SAL_NO,tp.prd_no
,ISNULL(SUM(tp.cst_std),0)cst_std4,ISNULL(SUM(tp.qty),0)qty4--比期样礼品金额 及 比期样礼品数量
FROM mf_pss mp
LEFT JOIN tf_pss tp ON tp.PS_NO=mp.PS_NO and tp.PS_ID=mp.PS_ID
LEFT JOIN tf_pss_z tz ON tz.PS_NO=mp.PS_NO AND tz.PS_ID=mp.PS_ID AND tz.ITM=tp.ITM
WHERE MP.ps_id='SA' AND ISNULL(tz.is_zp,'')='T' AND tp.ps_dd BETWEEN @c AND @d
GROUP BY mp.cus_no,mp.SAL_NO,tp.prd_no
)T4 ON COALESCE(T1.cus_no,T2.cus_no,T3.cus_no)=T4.cus_no
AND COALESCE(T1.SAL_NO,T2.SAL_NO,T3.SAL_NO)=T4.SAL_NO
AND COALESCE(T1.prd_no,T2.prd_no,T3.prd_no)=T4.prd_no
)T LEFT JOIN cust c ON c.CUS_NO=T.CUS_NO
LEFT JOIN salm s ON s.SAL_NO=T.SAL_NO
LEFT JOIN prdt p ON p.PRD_NO=T.PRD_NO