我必须明智地显示数据部件名称
Part Name Time Incoming Outgoing Transfer
--------- ------------ -------- -------- ---------
Sales 9:00 - 9:30 1 4 6
Sales 9:30 - 10:00 4 5 7
Sales 10:30 - 11:00 8 7 2
Michel 9:00 - 9:30 2 40 16
Michel 9:30 - 10:00 5 15 17
我必须明智地和时间地显示数据方
我在复杂的sql开发方面不是很好,但得到了一个人的帮助。他的SQL看起来像
DECLARE @StartTime datetime = '2015-06-09 09:00:00',
@EndTime datetime = '2015-06-09 18:00:00',
@Interval int = 30 -- this can be changed.
DECLARE @PartyIntervals TABLE (
Party1Name VARCHAR(500),
StartRange DATETIME,
EndRange DATETIME)
DECLARE @CSVIntervals TABLE (
Party1Name VARCHAR(500),
Incoming INT,
StartRange DATETIME)
;WITH cSequence AS
(
SELECT
@StartTime AS StartRange,
DATEADD(MINUTE, @Interval, @StartTime) AS EndRange
UNION ALL
SELECT
EndRange,
DATEADD(MINUTE, @Interval, EndRange)
FROM cSequence
WHERE DATEADD(MINUTE, @Interval, EndRange) < @EndTime
)
INSERT INTO @PartyIntervals
SELECT Party1Name,
StartRange,
EndRange
FROM (SELECT DISTINCT Party1Name FROM tridip_Interval) A, cSequence ORDER BY Party1Name;
--SELECT * FROM @PartyIntervals ORDER BY Party1Name
INSERT INTO @CSVIntervals
SELECT Party1Name,
count(*) as incoming,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='I'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name
SELECT P.Party1Name,
P.StartRange,
P.EndRange,
IsNull(Incoming , 0) AS Incoming
FROM @PartyIntervals P LEFT OUTER JOIN
@CSVIntervals A ON A.Party1Name = P.Party1Name AND P.StartRange = A.StartRange
ORDER BY P.Party1Name,P.StartRange
这个计算不明确
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
why 48 is used ?
INSERT INTO @CSVIntervals
SELECT Party1Name,
count(*) as incoming,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='I'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name
这里只有传入的相关数据被插入到CSVIntervals
表中,但我还需要为同一个sql中的所有部分插入传出和传输数据。这是不是很清楚如何添加更多的SQL来插入传出和传输相关的数据?
如果有人能理解我的要求那么请帮助我形成sql,结果我会得到我想要的结果。
感谢
我尝试写这个sql
INSERT INTO @CSVIntervals
SELECT Party1Name,
count(*) as incoming,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='I'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name,
SELECT Party1Name,
count(*) as OutGoing,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='O'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name,
SELECT Party1Name,
count(*) as Transfer,
CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0) AS StartRange
FROM tridip_Interval
WHERE direction='T'
GROUP BY CONVERT(smalldatetime, ROUND(CAST(Convert(datetime, [Call Start]) AS float) * 48.0,0,1)/48.0), Party1Name
这不起作用,但我想插入传输和传输的数据,就像传入数据一样。
答案 0 :(得分:0)
如果在三个部分之间使用UNION ALL,那么编辑中的SQL似乎可以选择所有数据。
所以试试:
Insert Into @CSVIntervals
Query 1
union all
Query 2
union all
Query 3