我希望有人可以帮我解决一个相当直接的问题,但让我感到难过。
我写了以下查询
SELECT a.post_id,
a.date_from,
a.date_to,
b.description,
b.parttime_pct AS budget_hours,
Sum(a.parttime_pct) AS contract_hours,
b.parttime_pct - Sum(a.parttime_pct) AS remainder
FROM aprresourcepost a
LEFT OUTER JOIN aprpost b
ON(( a.client = b.client
AND a.post_id = b.post_id ))
INNER JOIN ahsresources c
ON(( c.client = a.client
AND c.resource_id = a.resource_id ))
LEFT OUTER JOIN agladdress d
ON(( 'C0' = d.attribute_id
AND c.client = d.client
AND c.resource_id = d.dim_value
AND d.address_type = '1' ))
LEFT OUTER JOIN agldimvalue e
ON(( e.client = b.client
AND e.dim_value = b.dim_value ))
WHERE a.client = 'CI'
AND a.post_id = '14013'
GROUP BY a.post_id,
b.description,
b.parttime_pct,
a.parttime_pct,
a.date_from,
a.date_to
返回以下内容
post_id date_from date_to descr budget_hours contract_hours remainder
14013 2014-10-01 2099-12-31 Reg 140.00 105.00 35.00
14013 2014-12-20 2099-12-31 Reg 140.00 140.00 105.00
我想要展示的内容(如果可能)是
post_id date_from date_to descr budget_hours contract_hours remainder
14013 2014-10-01 2099-12-31 Reg 140.00 105.00 35.00
14013 2014-12-20 2099-12-31 Reg 140.00 140.00 0.00
这基本上表明,截至12月20日之前的任何日期,此角色的建议小时数(员工)仍有35小时,但截至12月20日,该职位已100%填补,但是我想要包括日期和日期数据是根据位置和日期和日期相加,我想我有点需要滚动总数
我知道如果我从select和group中删除日期,它将显示140.00 140.00但是我需要一种方法来分隔日期。
非常感谢任何帮助。
谢谢你们
答案 0 :(得分:0)
没有数据我发现这很难测试。但这是我最好的一击。使用CTE和ROW_NUMBER提供虚拟行id(需要2005年以后),我们自己加入前一行来累积余数:
WITH qry AS
(
SELECT ROW_NUMBER() OVER (ORDER BY date_from) AS row,
a.post_id,
a.date_from,
a.date_to,
b.description,
b.parttime_pct AS budget_hours,
Sum(a.parttime_pct) AS contract_hours,
b.parttime_pct - Sum(a.parttime_pct) AS remainder
FROM aprresourcepost a
LEFT OUTER JOIN aprpost b
ON(( a.client = b.client
AND a.post_id = b.post_id ))
INNER JOIN ahsresources c
ON(( c.client = a.client
AND c.resource_id = a.resource_id ))
LEFT OUTER JOIN agladdress d
ON(( 'C0' = d.attribute_id
AND c.client = d.client
AND c.resource_id = d.dim_value
AND d.address_type = '1' ))
LEFT OUTER JOIN agldimvalue e
ON(( e.client = b.client
AND e.dim_value = b.dim_value ))
WHERE a.client = 'CI'
AND a.post_id = '14013'
GROUP BY a.post_id,
b.description,
b.parttime_pct,
a.parttime_pct,
a.date_from,
a.date_to
)
SELECT
qry2.post_id,
qry2.date_from,
qry2.date_to,
qry2.description,
qry2.budget_hours,
qry2.contract_hours,
qry2.budget_hours - (qry2.remainder + ISNULL(qry1.remainder, 0)) AS Cumulative_Remainder
FROM
qry qry2
LEFT JOIN qry qry1 ON qry1.row = qry2.row - 1
答案 1 :(得分:0)
您需要做的就是将查询作为子查询并在外部查询中查找remainder
select *,
budget_hours- contract_hours AS remainder
from(
SELECT a.post_id,
a.date_from,
a.date_to,
b.description,
b.parttime_pct AS budget_hours,
Sum(a.parttime_pct) AS contract_hours
FROM aprresourcepost a
LEFT OUTER JOIN aprpost b
ON(( a.client = b.client
AND a.post_id = b.post_id ))
INNER JOIN ahsresources c
ON(( c.client = a.client
AND c.resource_id = a.resource_id ))
LEFT OUTER JOIN agladdress d
ON(( 'C0' = d.attribute_id
AND c.client = d.client
AND c.resource_id = d.dim_value
AND d.address_type = '1' ))
LEFT OUTER JOIN agldimvalue e
ON(( e.client = b.client
AND e.dim_value = b.dim_value ))
WHERE a.client = 'CI'
AND a.post_id = '14013'
GROUP BY a.post_id,
b.description,
b.parttime_pct,
a.parttime_pct,
a.date_from,
a.date_to ) A
答案 2 :(得分:0)
你是否有理由通过a.parttime_pct进行分组? 如果从group by子句中删除它会发生什么? 它不会在select语句中作为列返回,因此您不需要按它进行分组。
SELECT a.post_id,
a.date_from,
a.date_to,
b.description,
b.parttime_pct AS budget_hours,
Sum(a.parttime_pct) AS contract_hours,
b.parttime_pct - Sum(a.parttime_pct) AS remainder
FROM aprresourcepost a
LEFT OUTER JOIN aprpost b
ON(( a.client = b.client
AND a.post_id = b.post_id ))
INNER JOIN ahsresources c
ON(( c.client = a.client
AND c.resource_id = a.resource_id ))
LEFT OUTER JOIN agladdress d
ON(( 'C0' = d.attribute_id
AND c.client = d.client
AND c.resource_id = d.dim_value
AND d.address_type = '1' ))
LEFT OUTER JOIN agldimvalue e
ON(( e.client = b.client
AND e.dim_value = b.dim_value ))
WHERE a.client = 'CI'
AND a.post_id = '14013'
GROUP BY a.post_id,
b.description,
b.parttime_pct,
a.date_from,
a.date_to