我需要创建一个查询,该查询查看1年期的发票价值,并确定在同一星期内和任意给定的一周内,在同一市场中2个或更多商店中有多少员工拥有发票。编辑:本质上,我需要找到在同一周内同时在两家商店工作的员工。
我正在接近,但无法完全使查询正常工作。我尝试将日期左侧的发票数介于右侧+一周之间的日期表连接到发票表。并不是我想要的。
在示例代码中,只有前两名雇员在一周内在多个市场中拥有发票。
编辑:如果一周是基于日历的,我意识到以下示例可以工作。我需要它是一个不基于日历的滚动7天窗口。因此发票日期+ 7天,这就是为什么我尝试进行自我加入。
示例代码:
DROP TABLE [tmp].[store]
CREATE TABLE [tmp].[store](
[storepk] int NOT null IDENTITY(1,1),
[storename] varchar(20) NOT NULL,
[storemarket] varchar(20) NOT NULL,
[store_id] int null
) ON [PRIMARY]
GO
DROP TABLE [tmp].[storeinvoices]
CREATE TABLE [tmp].[storeinvoices](
[invoicepk] int NOT null IDENTITY(1,1),
[storeemployee_id] int null,
[store_id] int null,
[invoicedate] datetime null
) ON [PRIMARY]
GO
DROP TABLE [tmp].[storeemployee]
CREATE TABLE [tmp].[storeemployee](
[storeemployeepk] int NOT null IDENTITY(1,1),
[storeemployee_id] int null,
[store_id] int null,
[fname] VARCHAR(20) null,
[lname] VARCHAR(20) NULL
) ON [PRIMARY]
GO
DROP TABLE [tmp].[storeemployeeglobal]
CREATE TABLE [tmp].[storeemployeeglobal](
[storeemployeeglobalpk] int NOT null IDENTITY(1,1),
[masterstoreemployee_id] int not null,
[storeemployee_id] int null,
[store_id] int null
) ON [PRIMARY]
GO
INSERT INTO [tmp].[storeemployee]
SELECT 1,1,'steve','johnson' UNION
SELECT 2,2,'steve','johnson' UNION
SELECT 3,3,'steve','johnson' UNION
SELECT 4,1,'adam','thomas' UNION
SELECT 5,2,'adam','thomas' UNION
SELECT 6,1,'susan','smith' UNION
SELECT 7,3,'susan','smith'
INSERT INTO [tmp].[storeemployeeglobal]
SELECT 1,1,1 UNION
SELECT 1,2,2 UNION
SELECT 1,3,3 UNION
SELECT 2,4,1 UNION
SELECT 2,5,2 UNION
SELECT 3,6,1 UNION
SELECT 3,7,3
INSERT INTO [tmp].[store]
SELECT 'Down Town Store', 'South', 1 UNION
SELECT 'East Side Store','East', 2 UNION
SELECT 'South City Store','South', 3
INSERT INTO [tmp].[storeinvoices]
SELECT 1,1,'2018/1/1' UNION
SELECT 1,1,'2018/1/5' UNION
SELECT 1,1,'2018/2/1' UNION
SELECT 1,1,'2018/2/12' UNION
SELECT 1,1,'2018/3/15' UNION
SELECT 2,2,'2018/2/15' UNION
SELECT 3,3,'2018/3/25' UNION
SELECT 4,1,'2018/1/5' UNION
SELECT 4,1,'2018/1/25' UNION
SELECT 4,1,'2018/2/1' UNION
SELECT 4,1,'2018/2/15' UNION
SELECT 5,2,'2018/1/27' UNION
SELECT 6,1,'2018/1/11' UNION
SELECT 6,1,'2018/3/15' UNION
SELECT 7,3,'2018/5/15'
SELECT *
FROM [tmp].[storeinvoices] AS i
INNER JOIN [tmp].[storeemployeeglobal] AS eg
ON i.[storeemployee_id] = eg.[storeemployee_id]
AND i.[store_id] = eg.[store_id]
INNER JOIN [tmp].[store] AS s
ON i.[store_id] = s.[store_id]
ORDER BY eg.[masterstoreemployee_id], i.[invoicedate]
答案 0 :(得分:2)
您可以使用HAVING
:
SELECT i.storeemployee_id, DATEPART(week, invoicedate)
FROM [storeinvoices] AS i
INNER JOIN [storeemployeeglobal] AS eg
ON i.[storeemployee_id] = eg.[storeemployee_id]
AND i.[store_id] = eg.[store_id]
INNER JOIN [store] AS s
ON i.[store_id] = s.[store_id]
GROUP BY i.storeemployee_id,DATEPART(week, invoicedate)
HAVING COUNT(DISTINCT column_that_indicate_market)>=2
答案 1 :(得分:1)
我在SQL Fiddle中看不到任何满足您要求的数据。此外,您对“周”的含义也不清楚。这应该使您对如何做有一个认识:
SELECT i.storeemployee_id, datepart(iso_week, i.invoicedate) as week, count(*)
FROM storeinvoices i INNER JOIN
store s
ON i.store_id = s.store_id
GROUP BY i.storeemployee_id, datepart(iso_week, i.invoicedate)
HAVING MIN(s.storemarket) <> MAX(s.storemarket);
这使用iso_week
,所以在每年的末尾/开始时,星期的定义不受部分星期的影响。