0

我在 MS Access 上遇到了 SQL 问题。

问题是,我有:一个人可以重复的周数是在两个不同的部门,或销售渠道,区域经理姓名,或两者都在同一周内。

我想要的是?我想在我有更多金额的同一周内合计总入站金额等(有 70 种不同)..

太好了,设法做到了,但是,当总入站和创建的 SR 在确定的一周内相等时,它会重复该周.. 我没有第三个值将它放在这里,也许使用第一个值,但我没有不知道怎么做..:/

任何人都知道如何管理它?

在 SQL Fiddle(www.sqlfiddle.com) 上创建了一个示例,以帮助更快地解决它。:)

CREATE TABLE Raw 
    (
     NTLogin varchar(20), 
     FiscalWeek int,
     FiscalMonthName varchar(20),
     FiscalQuarter varchar(20),
     SalesChannel varchar(20),
     Location varchar(20),
     Department varchar(20),
     Queue varchar(20),
     FunctionalName varchar(30),
     QueueStatus varchar(20),
     ManagerName varchar(20),
     SRCreated int,
     TotalInbounds int,
     Responses int,
     RDHits int
    );

INSERT INTO Raw
(NTLogin, FiscalWeek, SalesChannel, Department, Queue, FunctionalName, QueueStatus,
ManagerName, SRCreated, TotalInbounds, Responses, RDHits, FiscalMonthName,
FiscalQuarter, Location)
VALUES
('Name1', '201401', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName1', '100', '200', '1','0', 'Month1', 'Q1', 'L1'),
('Name1', '201402', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName1', '100', '200', '1','0', 'Month1', 'Q1', 'L1'),
('Name1', '201403', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName1', '100', '200', '1','0', 'Month1', 'Q1', 'L1'),
('Name1', '201404', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName1', '100', '200', '1','0', 'Month1', 'Q1', 'L1'),
('Name1', '201404', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName2', '101', '200', '1','0', 'Month1', 'Q1', 'L1'),
('Name1', '201405', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName2', '100', '200', '1','0', 'Month2', 'Q1', 'L1'),
('Name1', '201406', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName2', '100', '200', '1','0', 'Month2', 'Q1', 'L1'),
('Name1', '201407', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName2', '100', '200', '1','0', 'Month2', 'Q1', 'L1'),
('Name1', '201408', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName2', '100', '200', '1','0', 'Month2', 'Q1', 'L1'),
('Name1', '201408', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '100', '200', '1','0', 'Month2', 'Q1', 'L1'),
('Name1', '201409', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '100', '200', '1','0', 'Month3', 'Q1', 'L1'),
('Name1', '201410', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '100', '200', '1','0', 'Month3', 'Q1', 'L1'),
('Name1', '201411', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '100', '200', '1','0', 'Month3', 'Q1', 'L1'),
('Name1', '201412', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '0', '0', '1', '15', 'Month3', 'Q1', 'L1'),
('Name1', '201412', 'Sales1', 'Department1', 'Queue1', 'FunctionalName1',
'QueueStatus1', 'ManagerName3', '0', '0', '1', '10','Month3', 'Q1', 'L1');

这是我有问题的 SQL:

SELECT
Raw.[NTLogin],
Raw.[FiscalWeek],
MaxDuplicate.[ManagerName],
MaxDuplicate.[SalesChannel],
MaxDuplicate.Department,
MaxDuplicate.[FunctionalName],
MaxDuplicate.Queue,
MaxDuplicate.[QueueStatus],
Raw.FiscalMonthName,
Raw.FiscalQuarter,
Raw.Location,
Sum(Raw.[SRCreated]) AS [SRCreated],
Sum(Raw.[TotalInbounds]) AS [TotalInbounds],
Sum(Raw.RDHits)
FROM Raw
INNER JOIN
(SELECT Raw.[NTLogin],
Raw.[FiscalWeek],
Raw.[ManagerName],
Raw.[SalesChannel],
Raw.Department,
Raw.[FunctionalName],
Raw.Queue,
Raw.[QueueStatus]
FROM Raw
INNER JOIN
(SELECT
Raw.[NTLogin],
Raw.[FiscalWeek],
MAX(Raw.[TotalInbounds]) AS [TotalInbounds],
MAX(Raw.[SRCreated]) AS [SRCreated]
FROM Raw
GROUP BY Raw.[NTLogin], Raw.[FiscalWeek]) AS MAX
ON Raw.[NTLogin] = Max.[NTLogin]
AND Raw.[FiscalWeek] = Max.[FiscalWeek]
WHERE
Raw.[TotalInbounds] = Max.[TotalInbounds]
AND Raw.[SRCreated] = Max.[SRCreated]) AS MaxDuplicate
ON Raw.[NTLogin] = MaxDuplicate.[NTLogin]
AND Raw.[FiscalWeek] = MaxDuplicate.[FiscalWeek]
GROUP BY
Raw.[NTLogin],
Raw.[FiscalWeek],
Raw.FiscalMonthName,
Raw.FiscalQuarter,
Raw.Location,
MaxDuplicate.[ManagerName],
MaxDuplicate.[SalesChannel],
MaxDuplicate.Department,
MaxDuplicate.[FunctionalName],
MaxDuplicate.Queue,
MaxDuplicate.[QueueStatus]

希望可以有人帮帮我!谢谢!!!

4

0 回答 0