在感恩节假期里,我一直在绞尽脑汁,做了以下事情没有运气。
我运行以下SQL脚本以从我们的销售数据库获取最近12个月的数据。虽然这已经按照我想要的另一份报告返回了每次销售,但有人要求我现在搜索结果并查看产品是否已经订购过,如果没有将结果返回到另一份报告。
这是我到目前为止所得到的:
SELECT
cicmpy.cmp_name AS "Company Name",
humres.fullname AS "Salesperson",
oehdrhst_sql.ord_dt AS "Order Date",
oelinhst_sql.item_no AS "Item Number",
oelinhst_sql.item_desc_1 AS "Description",
oelinhst_sql.qty_ordered AS "Qty Ordered",
oelinhst_sql.item_desc_2 AS "UPC CODE",
oehdrhst_sql.oe_po_no AS "Customer PO"
FROM
(humres humres
INNER JOIN
(cicmpy cicmpy
INNER JOIN
oehdrhst_sql oehdrhst_sql ON cicmpy.cmp_code=oehdrhst_sql.cus_no)
ON humres.res_id=oehdrhst_sql.slspsn_no)
INNER JOIN
oelinhst_sql oelinhst_sql ON oehdrhst_sql.ord_no = oelinhst_sql.ord_no
WHERE
oehdrhst_sql.ord_dt > DATEADD(year, -1, GETDATE())
ORDER BY
cicmpy.cmp_name, oelinhst_sql.item_no
以上以下列格式返回我的数据
Company Name Salesperson Order Date Item Number Description Qty Ordered UPC CODE Customer PO
Company 1 John Doe 12/29/2014 1200052 Product 1 90 0000000001 292072
Company 1 John Doe 4/7/2015 1200053 Product 2 90 0000000002 348615
Company 1 John Doe 6/2/2015 1200052 Product 1 90 0000000001 382184
Company 2 Jane Doe 12/29/2014 1200052 Product 1 90 0000000001 292072
Company 2 Jane Doe 4/7/2015 1200053 Product 2 90 0000000002 348615
Company 2 Jane Doe 6/2/2015 1200052 Product 1 90 0000000001 382184
Company 3 Jane Doe 12/29/2014 1200052 Product 1 90 0000000001 292072
Company 3 Jane Doe 4/7/2015 1200053 Product 2 90 0000000002 348615
Company 3 John Smith 6/2/2015 1200053 Product 2 90 0000000002 382184
Company 3 John Smith 12/29/2014 1200052 Product 1 90 0000000001 292072
Company 3 John Smith 4/7/2015 1200053 Product 2 90 0000000002 348615
Company 3 John Smith 6/2/2015 1200052 Product 1 90 0000000001 382184
我曾经想过使用COUNT但是我无法想象如何通过客户名称来限制它我也玩弄了使用Distinct,但我也想不通。任何帮助都会受到赞赏,因为我无法继续向Excel发送20,000多行销售额来过滤这些单一报告。
有人要求我在结果中搜索(如上例所示)并查看产品是否已经订购过,如果没有,则将结果返回到另一份报告。有效地对这些结果进行查询,即。当公司1下订单时,新产品的商品编号为1200053,不在之前的购买中,因此需要返回以下商品
Company Name Salesperson Order Date Item Number Description Qty Ordered UPC CODE Customer PO
Company 1 John Doe 12/29/2015 1200053 Product 3 90 0000000001 292999
答案 0 :(得分:0)
要仅包含在DATE之前未购买产品的客户,只需在where子句中添加NOT EXISTS (<Some Query>)
条件即可。
编辑:进一步说明,NOT EXISTS
将检查客户是否历来订购该商品,如果没有,请在结果中退回该客户。
见下文。
DECLARE @GT DATETIME
SET @GT = GETDATE();
SELECT
cicmpy.cmp_name AS [Company Name],
humres.fullname AS [Salesperson],
oehdrhst_sql.ord_dt AS [Order Date],
oelinhst_sql.item_no AS [Item Number],
oelinhst_sql.item_desc_1 AS [Description],
oelinhst_sql.qty_ordered AS [Qty Ordered],
oelinhst_sql.item_desc_2 AS [UPC CODE],
oehdrhst_sql.oe_po_no AS [Customer PO]
FROM cicmpy cicmpy
INNER JOIN oehdrhst_sql oehdrhst_sql
ON cicmpy.cmp_code = oehdrhst_sql.cus_no
INNER JOIN humres humres
ON humres.res_id = oehdrhst_sql.slspsn_no
INNER JOIN oelinhst_sql oelinhst_sql
ON oehdrhst_sql.ord_no = oelinhst_sql.ord_no
WHERE oehdrhst_sql.ord_dt > DATEADD(year, -1, @GT)
AND NOT EXISTS --Exclude those customers identified in the sub query below.
(
--Identify customers where the item was purchased previously.
select *
from cicmpy c
INNER JOIN oehdrhst_sql oh
ON c.cmp_code = oh.cus_no
INNER JOIN oelinhst_sql ol
ON oh.ord_no = ol.ord_no
where c.cmp_code = oehdrhst_sql.cmp_code
and ol.item_no = oelinhst_sql.item_no
and ol.ord_dt <= DATEADD(year, -1, @GT)
)
ORDER BY
cicmpy.cmp_name, oelinhst_sql.item_no;
希望这有帮助!