所以我有这个查询驱动的ssrs报告,我已经被要求在报告的一部分列出工程项目最多的工程师列出它们从大多数到最少...最大到最小。这是一个巨大的查询。
declare @startDate date
declare @endDate date
set @startDate = GETDATE() -30
set @endDate = GETDATE()
SELECT
PN_B,
PN_A,
Project_Practice,
Date_Closed_UTC,
Project_ID,
Company_Name,
Invoiced_Service_Amount,
AGR_Amt,
Total_Service_Billing,
Project_TCW,
Hrs_Budgeted,
Est_Hours,
Billing_Method,
Project_Total_Hrs,
Project_Hrs_B,
Project_Hrs_NB,
Project_Hrs_NC,
Project_Hrs_Agr,
Project_Status,
Location,
OPP_RECID,
Opportunity_RecID,
AM,
DA,
PM,
PeopleNRole,
PM_Billing_Method_ID
FROM
(
SELECT
PPRS2.PN_B,
PPRS2.PN_A,
PPRS2.Project_Practice,
PPRS2.Date_Closed_UTC,
PPRS2.Project_ID,
PPRS2.Company_Name,
PPRS2.Invoiced_Service_Amount,
PPRS2.AGR_Amt,
PPRS2.Total_Service_Billing,
PPRS2.Project_TCW,
PPRS2.Hrs_Budgeted,
PPRS2.Est_Hours,
PPRS2.Billing_Method,
PPRS2.Project_Total_Hrs,
PPRS2.Project_Hrs_B,
PPRS2.Project_Hrs_NB,
PPRS2.Project_Hrs_NC,
PPRS2.Project_Hrs_Agr,
PPRS2.Project_Status,
PPRS2.Location,
PPRS2.OPP_RECID,
AMDA2.Opportunity_RecID,
AMDA2.AM,
AMDA2.DA,
AMDA2.PM,
PeopleNRole,
PPRS2.PM_BILLING_METHOD_ID
FROM
(
SELECT
PN_B,
PN_A,
Project_Practice,
Date_Closed_UTC,
Project_ID,
Company_Name,
Invoiced_Service_Amount,
AGR_Amt,
Total_Service_Billing,
Project_TCW,
Hrs_Budgeted,
Est_Hours,
Billing_Method,
Project_Total_Hrs,
Project_Hrs_B,
Project_Hrs_NB,
Project_Hrs_NC,
Project_Hrs_Agr,
Project_Status,
Location,
OPP_RECID,
pl2.PeopleNRole,
PM_Billing_Method_ID
FROM
(
SELECT
Table_B.Project_Number AS PN_B,
Table_A.Project_Number AS PN_A,
Table_A.Project_Practice,
Table_A.Date_Closed_UTC,
Table_A.Project_ID,
Table_A.Company_Name,
Table_A.Invoiced_Service_Amount,
Table_A.AGR_Amt,
Table_A.Total_Service_Billing,
Table_B.Project_TCW,
Table_A.Hrs_Budgeted,
Table_A.Est_Hours,
Table_A.Billing_Method,
Table_B.Project_Total_Hrs,
Table_B.Project_Hrs_B,
Table_B.Project_Hrs_NB,
Table_B.Project_Hrs_NC,
Table_B.Project_Hrs_Agr,
Table_A.Project_Status,
Table_A.Location,
Table_A.OPP_RECID,
Table_A.PM_BILLING_METHOD_ID
FROM
(
SELECT
Total_Service_Billing,
AGR_Amt,
Invoiced_Service_Amount,
Sales_Tax_Amount_Total,
Project_Number,
Project_ID,
Date_Closed_UTC,
Est_Hours,
Billing_Method,
Company_Name,
Project_Status,
Project_Practice,
Hrs_Budgeted,
Location,
OPP_RECID,
PM_Billing_Method_ID
FROM
(
SELECT
Project_Service_Totals.Service_Total AS Total_Service_Billing,
Project_Service_Totals.AGR_Amt,
Project_Service_Totals.Service_Invoice_Amt_Total AS Invoiced_Service_Amount,
Project_Service_Totals.Sales_Tax_Amount_Total,
Project_Service_Totals.Project_RecID AS Project_Number,
PM_Project.Project_ID,
PM_Project.Date_Closed_UTC,
PM_Project.Est_Hours,
PM_Project.PM_Billing_Method_ID AS Billing_Method, /* A = Actual Rates F = Fixed Fee for the Inital Project Type */
Company.Company_Name,
PM_Status.Description AS Project_Status,
PM_Type.Description AS Project_Practice,
SUM(v_rpt_Project.hours_budget) AS Hrs_Budgeted,
Owner_Level.Description AS Location,
PM_Project.Opportunity_RecID AS OPP_RECID,
PM_Project.PM_Billing_Method_ID
FROM
(
SELECT
SUM(Service_Invoice_Amt - Sales_Tax) + SUM(BL_AGR_AMT) AS Service_Total,
SUM(BL_AGR_AMT) AS AGR_Amt,
SUM(Service_Invoice_Amt) AS Service_Invoice_Amt_Total,
SUM(Sales_Tax) AS Sales_Tax_Amount_Total,
Project_RecID
FROM
(
SELECT
Billing_Log_RecID,
Invoice_Number,
Invoice_Amount AS Service_Invoice_Amt,
PM_Project_RecID AS Project_RecID,
Sales_Tax_Amount AS Sales_Tax,
AGR_Amount AS BL_AGR_AMT
FROM
Billing_Log AS Billing_Log_1
)
AS Service_Invoice_Total
GROUP BY Project_RecID
)
AS Project_Service_Totals
INNER JOIN
PM_Project
ON
Project_Service_Totals.Project_RecID = PM_Project.PM_Project_RecID
INNER JOIN
Company
ON
PM_Project.Company_RecID = Company.Company_RecID
INNER JOIN
PM_Status
ON
PM_Project.PM_Status_RecID = PM_Status.PM_Status_RecID
INNER JOIN
PM_Type
ON
PM_Project.PM_Type_RecID = PM_Type.PM_Type_RecID
INNER JOIN
v_rpt_Project
ON
PM_Project.PM_Project_RecID = v_rpt_Project.ProjectNbr
INNER JOIN
Owner_Level
ON
PM_Project.Owner_Level_RecID = Owner_Level.Owner_Level_RecID
GROUP BY
Project_Service_Totals.Service_Total,
Project_Service_Totals.AGR_Amt,
Project_Service_Totals.Service_Invoice_Amt_Total,
Project_Service_Totals.Sales_Tax_Amount_Total,
Project_Service_Totals.Project_RecID,
PM_Project.Project_ID,
PM_Project.Date_Closed_UTC,
PM_Project.Est_Hours,
PM_Project.PM_Billing_Method_ID,
Company.Company_Name,
PM_Status.Description,
PM_Type.Description,
Owner_Level.Description,
PM_Project.Opportunity_RecID
)
AS T
)
AS Table_A
INNER JOIN
(
SELECT
Project_Hrs_B,
Project_Hrs_NB,
Project_Hrs_NC,
Project_Total_Hrs,
Project_Hrs_Agr,
Project_TCW,
Project_Number
FROM
(
SELECT
SUM(Hrs_Billable) AS Project_Hrs_B,
SUM(Hrs_NonBillable) AS Project_Hrs_NB,
SUM(Hrs_NoCharge) AS Project_Hrs_NC,
SUM(Hrs_Billable + Hrs_NonBillable + Hrs_NoCharge) AS Project_Total_Hrs,
SUM(Hrs_Agr) AS Project_Hrs_Agr,
SUM(Project_TC) AS Project_TCW,
Project_Number
FROM
(
SELECT
SUM(Hrs_B) AS Hrs_Billable,
SUM(Hrs_NB) AS Hrs_NonBillable,
SUM(Hrs_NC) AS Hrs_NoCharge,
SUM(Agr_Hrs) AS Hrs_Agr,
SUM(TE_TotalCost) AS Project_TC,
Project_Number,
REC_ID,
Full_Name
FROM
(
SELECT
Rec_ID,
Full_Name,
Project_Number,
SUM(VT_Hrs_Agr) AS Agr_Hrs,
SUM(Line_TC) AS TE_TotalCost,
SUM(CASE WHEN Billing_Type = 'B' THEN (TE_Hrs_Inv) ELSE 0 END) AS Hrs_B,
SUM(CASE WHEN Billing_Type = 'NB' THEN (TE_Hrs_Inv) ELSE 0 END) AS Hrs_NB,
SUM(CASE WHEN Billing_Type = 'NC' THEN (TE_Hrs_Inv) ELSE 0 END) AS Hrs_NC
FROM
(
SELECT
VT_Hrs_Cost,
TE_Hrs_Inv,
Line_TC,
VT_Hrs_B,
VT_Hrs_NB,
VT_Hrs_Agr,
Project_Number,
Rec_ID,
Full_Name,
Billing_Type,
Work_Role
FROM
(
SELECT
v_rpt_Time.Hourly_Cost AS VT_Hrs_Cost,
Time_Entry.Hours_Invoiced AS TE_Hrs_Inv,
v_rpt_Time.Hourly_Cost * Time_Entry.Hours_Invoiced AS Line_TC,
v_rpt_Time.Billable_Hrs AS VT_Hrs_B,
v_rpt_Time.NonBillable_Hrs AS VT_Hrs_NB,
v_rpt_Time.AgrHrsCovered AS VT_Hrs_Agr,
Time_Entry.PM_Project_RecID AS Project_Number,
v_rpt_Time.member_recid AS Rec_ID,
v_rpt_Time.first_name + ' ' + v_rpt_Time.last_name AS Full_Name,
v_rpt_Time.option_id AS Billing_Type,
v_rpt_Time.work_role AS Work_Role
FROM
Time_Entry
INNER JOIN
v_rpt_Time
ON
Time_Entry.Time_RecID = v_rpt_Time.Time_RecID
)
AS Project_Hrs
)
AS Project_Hrs_Sorted
GROUP BY
Project_Number,
Billing_Type,
TE_Hrs_Inv,
VT_Hrs_Agr,
Rec_ID,
Full_Name
)
AS Project_Cost
GROUP BY
Project_Number,
Rec_ID,
Full_Name
)
AS Project_Totals
GROUP BY
Project_Number
)
AS B
)
AS Table_B
ON
Table_A.Project_Number = Table_B.Project_Number
) AS PPRS,
/* Section that creates the Column to Row using a CROSS APPLY and STUFF Command with XML */
(
SELECT
Project_Number,
PeopleNRole
FROM
(
SELECT DISTINCT
Base.PeopleNrole,
p1.Project_Number
FROM
(
SELECT DISTINCT
Time_Entry10.PM_Project_RecID AS Project_Number,
(v_rpt_Time10.first_name + ' ' + v_rpt_Time10.last_name /* + ' ' + member10.Role_id Removes the SE from the Name*/) AS PeopleNrole,
Member10.Role_ID AS RoleID
FROM
Time_Entry AS Time_Entry10
INNER JOIN
v_rpt_Time AS v_rpt_Time10
ON
Time_Entry10.Time_RecID = v_rpt_Time10.Time_RecID
INNER JOIN
Member AS Member10
ON
v_rpt_Time10.member_recid = Member10.Member_RecID AND Member10.Role_ID = 'SE'
--order by Time_Entry10.Hours_Bill desc
)
AS P1
CROSS APPLY
(
SELECT
STUFF
(
(
SELECT ', ' + PeopleNrole
FROM
(
SELECT DISTINCT
Time_Entry11.PM_Project_RecID AS Project_Number,
(v_rpt_Time11.first_name + ' ' + v_rpt_Time11.last_name/* + ' ' + member11.Role_id Removes the SE from the Name*/ ) AS PeopleNrole,
Member11.Role_ID
AS
RoleID
FROM
Time_Entry
AS
Time_Entry11
INNER JOIN
v_rpt_Time AS v_rpt_Time11
ON
Time_Entry11.Time_RecID = v_rpt_Time11.Time_RecID
INNER JOIN
Member AS Member11
ON
v_rpt_Time11.member_recid = Member11.Member_RecID AND Member11.Role_ID = 'SE'
--order by Time_Entry11.Hours_Bill desc
)
AS
P2
WHERE
p2.Project_Number = P1.Project_Number
ORDER BY
Time_Entry.Hours_Bill
FOR
XML
PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '')) Base(PeopleNRole))
AS PeopleLink
)
AS pl2
WHERE
PPRS.PN_A = PL2.Project_Number
)
AS PPRS2
INNER JOIN
(
SELECT
Opportunity_RecID,
AM,
DA,
PM
FROM
(
SELECT
N.Opportunity_RecID,
Member_1.First_Name + ' ' + Member_1.Last_Name AS AM,
Member_2.First_Name + ' ' + Member_2.Last_Name AS DA,
Member_3.First_Name + ' ' + Member_3.Last_Name AS PM
FROM
Member AS Member_1
INNER JOIN
(
SELECT DISTINCT
v_rpt_Opportunity3.Opportunity_RecID,
v_rpt_Opportunity3.Sales_Rep1,
v_rpt_Opportunity3.Sales_Rep2,
PM_Project3.Member_ID
FROM
v_rpt_Opportunity AS v_rpt_Opportunity3
INNER JOIN
PM_Project AS PM_Project3
ON
v_rpt_Opportunity3.Opportunity_RecID = PM_Project3.Opportunity_RecID
) AS N
ON
Member_1.Member_ID = N.Sales_Rep1
INNER JOIN
Member AS Member_2
ON
N.Sales_Rep2 = Member_2.Member_ID
INNER JOIN
Member AS Member_3
ON
N.Member_ID = Member_3.Member_ID
)
AS AMDA
)
AS AMDA2
ON
PPRS2.OPP_RECID = AMDA2.Opportunity_RecID
)
AS PPRS_pt1
WHERE
Date_Closed_UTC BETWEEN @Startdate AND @EndDate
AND
Location <> 'AOSCloud'
/* (Location IN(@Division))
AND Per Leslie 8/14/2013 want all divisions listed
(Project_Practice = IN (@Practice)*/
ORDER BY PN_B
我已经提出了我认为需要编辑查询timeentry10和timeentry11的地方,但我不能这样做,因为order by子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非顶部,偏移,或者xml也被指定...所以我做的是尝试做time_entry.hours_bill desc,但它告诉我,我没有访问该位置的time_entry。他们以前拥有的是在for xml之前的roleid命令。这真的让我失去了因为我觉得它过于复杂。对此的任何帮助都会很棒。我也不会反对把peoplenrole添加另一个专栏,只是让那个时间最多的人把它添加到报告中,说SE的业务时间最长。
获取peoplenrole的部分是
* Section that creates the Column to Row using a CROSS APPLY and STUFF Command with XML */
(
SELECT
Project_Number,
PeopleNRole
FROM
(
SELECT DISTINCT
Base.PeopleNrole,
p1.Project_Number
FROM
(
SELECT DISTINCT
Time_Entry10.PM_Project_RecID AS Project_Number,
(v_rpt_Time10.first_name + ' ' + v_rpt_Time10.last_name /* + ' ' + member10.Role_id Removes the SE from the Name*/) AS PeopleNrole,
Member10.Role_ID AS RoleID
FROM
Time_Entry AS Time_Entry10
INNER JOIN
v_rpt_Time AS v_rpt_Time10
ON
Time_Entry10.Time_RecID = v_rpt_Time10.Time_RecID
INNER JOIN
Member AS Member10
ON
v_rpt_Time10.member_recid = Member10.Member_RecID AND Member10.Role_ID = 'SE'
--order by Time_Entry10.Hours_Bill desc
)
AS P1
CROSS APPLY
(
SELECT
STUFF
(
(
SELECT ', ' + PeopleNrole
FROM
(
SELECT DISTINCT
Time_Entry11.PM_Project_RecID AS Project_Number,
(v_rpt_Time11.first_name + ' ' + v_rpt_Time11.last_name/* + ' ' + member11.Role_id Removes the SE from the Name*/ ) AS PeopleNrole,
Member11.Role_ID
AS
RoleID
FROM
Time_Entry
AS
Time_Entry11
INNER JOIN
v_rpt_Time AS v_rpt_Time11
ON
Time_Entry11.Time_RecID = v_rpt_Time11.Time_RecID
INNER JOIN
Member AS Member11
ON
v_rpt_Time11.member_recid = Member11.Member_RecID AND Member11.Role_ID = 'SE'
--order by Time_Entry11.Hours_Bill desc
)
AS
P2
WHERE
p2.Project_Number = P1.Project_Number
ORDER BY
RoleID
FOR
XML
PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '')) Base(PeopleNRole))
AS PeopleLink
)
AS pl2
WHERE
PPRS.PN_A = PL2.Project_Number
)
的图片
答案 0 :(得分:0)
如此迅速和肮脏我发现了一份SSRS报告,显然这个人就像这样计算了这些时间
SELECT F_Name, L_Name, Total_TE_Hours_Actual, project_id
FROM (SELECT SUM(TE_Part2.TE_Hours_Actual) AS Total_TE_Hours_Actual, Member.First_Name AS F_Name, Member.Last_Name AS L_Name, TE_Part2.PM_Project_RecID as project_id
FROM
(SELECT TE_Hours_Actual, PM_Project_RecID, Rec_ID
FROM
(SELECT v_rpt_Time.Hourly_Cost AS TE_Hourly_Cost, v_rpt_Time.hours_actual AS TE_Hours_Actual, Time_Entry.PM_Project_RecID, v_rpt_Time.NonBillable_Hrs AS TE_Hours_Non_Billable, v_rpt_Time.member_recid AS Rec_ID
FROM Time_Entry
INNER JOIN v_rpt_Time ON Time_Entry.Time_RecID = v_rpt_Time.Time_RecID
) AS TE_Part1) AS TE_Part2
INNER JOIN Member ON TE_Part2.Rec_ID = Member.Member_RecID
GROUP BY TE_Part2.Rec_ID, Member.First_Name, Member.Last_Name, TE_Part2.PM_Project_RecID) AS TE_Part3
所以我把它放到像这样的CTE
with time_CTE(F_Name, L_Name, Total_TE_Hours_Actual, project_id) as (
SELECT F_Name, L_Name, Total_TE_Hours_Actual, project_id
FROM (SELECT SUM(TE_Part2.TE_Hours_Actual) AS Total_TE_Hours_Actual, Member.First_Name AS F_Name, Member.Last_Name AS L_Name, TE_Part2.PM_Project_RecID as project_id
FROM
(SELECT TE_Hours_Actual, PM_Project_RecID, Rec_ID
FROM
(SELECT v_rpt_Time.Hourly_Cost AS TE_Hourly_Cost, v_rpt_Time.hours_actual AS TE_Hours_Actual, Time_Entry.PM_Project_RecID, v_rpt_Time.NonBillable_Hrs AS TE_Hours_Non_Billable, v_rpt_Time.member_recid AS Rec_ID
FROM Time_Entry
INNER JOIN v_rpt_Time ON Time_Entry.Time_RecID = v_rpt_Time.Time_RecID
) AS TE_Part1) AS TE_Part2
INNER JOIN Member ON TE_Part2.Rec_ID = Member.Member_RecID
GROUP BY TE_Part2.Rec_ID, Member.First_Name, Member.Last_Name, TE_Part2.PM_Project_RecID) AS TE_Part3)
select top 1 f_name + ' ' + l_name as Name, total_TE_Hours_Actual, project_id from time_CTE
where project_id is not null
order by Total_TE_Hours_Actual desc;
我在我的ssrs中创建了一个新专栏,然后说了顶级SE然后我做了这样的查找
=lookup (Fields!PN_A.value, Fields!project_id.value, Fields!Name.value, "SETime")
所以这对业务用户很满意,但是我正在接受我在这里的建议并将主要查询打破到CTE那件事是丑陋的方式