Недавно я написал длинный SQL запрос с таким количеством строк и переменных, чтобы получить точное среднее время ответа разных людей. Итак, мой вопрос, как я могу показать их в Excel, используя PowerPivot? Потому что, когда я копирую весь запрос и вставляю его в раздел оператора PowerPivot SQL, он выдает ошибку, тогда как он точно выполняется в SQL.
DECLARE @Leave TABLE
(
[TimeU] datetime,
[TimeM] datetime,
[TimeH] datetime,
[TimeSG] datetime,
[TimeS] datetime,
[TimeHR] datetime,
[TimeHH] datetime
)
DECLARE @LeaveD TABLE
(
SName nvarchar(50),
kind nvarchar(50),
number int
)
DECLARE @LeaveAvg TABLE
(
[DayA] int,
[HourA] int,
[MinuteA] int,
[Title] nvarchar(60)
)
DEclare @LeaveExactAVG Table
(
totalMin nvarchar(8),
Title nvarchar(60)
)
INSERT INTO @Leave ([TimeU], [TimeM], [TimeH], [TimeSG], [TimeS], [TimeHR], [TimeHH])
SELECT
--nvarchar5,--nvarchar8,--nvarchar9,--nvarchar10,--nvarchar11,--nvarchar12,
tp_Created as [TimeU],
datetime3 as [TimeM] ,
datetime4 as [TimeH] ,
datetime8 as [TimeSG] ,
datetime5 as [TimeS] ,
datetime6 as [TimeHR] ,
datetime7 as [TimeHH]
FROM [ORG].[dbo].[AllUserData]
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and tp_DeleteTransactionId=0x
and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23' --nvarchar14=N'انجام شد'
INSERT INTO @LeaveD (SName, kind, number)
SELECT
N'Leave' as SNmae, N'manager' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'manager'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'officer' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'officer'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'security' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'security'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'especialist' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'especialist'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'HRB' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'HRB'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'HRM' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'HRM'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
union
SELECT
N'Leave' as SNmae, N'finished' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'finished'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
and nvarchar8<>N'No' and nvarchar9<>N'No' and nvarchar10<>N'No' and nvarchar11<>N'No' and nvarchar12<>N'No' and nvarchar5<>N'No'
union
SELECT
N'Leave' as SNmae, N'رد شده' as Kind ,count(*) as number
FROM [ORG].[dbo].[AllUserData] as kind
where tp_ListId='BC168B2F-0E03-451A-8902-2379E8C5B483' and nvarchar14=N'finished'
And tp_DeleteTransactionId=0x and tp_Created>'2019-09-22' --and tp_Created<'2019-07-23'
and (nvarchar8=N'No' or nvarchar9=N'No' or nvarchar10=N'No' or nvarchar11=N'No' or nvarchar12=N'No' or nvarchar5=N'No')
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeU]),max([TimeM]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeU]),max([TimeM]))/60)))-(((DATEDIFF(minute, min([TimeU]),max([TimeM]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeU]),max([TimeM]))-((DATEDIFF(minute, min([TimeU]),max([TimeM]))/60)*60) as [MinuteA]
,N'M speed' as [Title]
from @Leave
where TimeM is not null
group by [TimeU] , [TimeM]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeM]),max([TimeH]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeM]),max([TimeH]))/60)))-(((DATEDIFF(minute, min([TimeM]),max([TimeH]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeM]),max([TimeH]))-((DATEDIFF(minute, min([TimeM]),max([TimeH]))/60)*60) as [MinuteA]
,N'O speed' as [Title]
from @Leave
where TimeH is not null and TimeM is not null
group by [TimeM] , [TimeH]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeH]),max([TimeSG]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeH]),max([TimeSG]))/60)))-(((DATEDIFF(minute, min([TimeH]),max([TimeSG]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeH]),max([TimeSG]))-((DATEDIFF(minute, min([TimeH]),max([TimeSG]))/60)*60) as [MinuteA]
,N'S speed' as [Title]
from @Leave
where TimeSG is not null AND TimeH is not null
group by [TimeH] , [TimeSG]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeH]),max([TimeS]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeH]),max([TimeS]))/60)))-(((DATEDIFF(minute, min([TimeH]),max([TimeS]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeH]),max([TimeS]))-((DATEDIFF(minute, min([TimeH]),max([TimeS]))/60)*60) as [MinuteA]
,N'E speed' as [Title]
from @Leave
where TimeS is not null AND TimeH is not null and TimeSG is null
group by [TimeH] , [TimeS]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeSG]),max([TimeS]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeSG]),max([TimeS]))/60)))-(((DATEDIFF(minute, min([TimeSG]),max([TimeS]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeSG]),max([TimeS]))-((DATEDIFF(minute, min([TimeSG]),max([TimeS]))/60)*60) as [MinuteA]
,N'E speed' as [Title]
from @Leave
where TimeS is not null AND TimeSG is not null
group by [TimeSG] , [TimeS]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeS]),max([TimeHR]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeS]),max([TimeHR]))/60)))-(((DATEDIFF(minute, min([TimeS]),max([TimeHR]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeS]),max([TimeHR]))-((DATEDIFF(minute, min([TimeS]),max([TimeHR]))/60)*60) as [MinuteA]
,N'HRB speed' as [Title]
from @Leave
where TimeS is not null and TimeHR is not null
group by [TimeU] , [TimeM]
INSERT INTO @LeaveAvg ([DayA],[HourA],[MinuteA],[Title])
select
(DATEDIFF(minute, min([TimeHR]),max([TimeHH]))/60)/24 as [DayA]
,(((DATEDIFF(minute, min([TimeHR]),max([TimeHH]))/60)))-(((DATEDIFF(minute, min([TimeHR]),max([TimeHH]))/60)/24)*24) as [HourA]
,DATEDIFF(minute, min([TimeHR]),max([TimeHH]))-((DATEDIFF(minute, min([TimeHR]),max([TimeHH]))/60)*60) as [MinuteA]
,N'HRM speed' as [Title]
from @Leave
where TimeHR is not null and TimeHH is not null
group by [TimeU] , [TimeM]
INSERT INTO @LeaveExactAVG(totalMin,Title)
select AVG(DayA*24*60+HourA*60+MinuteA) as totalMin
, Title
from @LeaveAvg
group by Title
select N'AVG' as TypeOfAVG,Title as ResponseType, CASE
WHEN cast(totalMin as int) < 60 THEN cast(cast(totalMin as int) as varchar) +N' min '
WHEN cast(totalMin as int) < 1440 THEN cast((cast(totalMin as int)/60) as varchar) +N' hour '+ cast((cast(totalMin as int)%60) as varchar) +N' min '
ELSE cast(cast(totalMin as int)/1440 as varchar) +N' day '+cast(((cast(totalMin as int)%1440)/60) as varchar)+N' hour '+cast(((cast(totalMin as int)%1440)%60) as varchar)+N' min '
END as ResponseTime
from @LeaveExactAVG
`