Sql 如何获取/插入有效数据之间的虚拟零周和成本
这个问题很难回答,让我抓狂。 我想做的基本上是在临时表中的有效周和成本之间插入零周和成本。见下表Sql 如何获取/插入有效数据之间的虚拟零周和成本,sql,insert,Sql,Insert,这个问题很难回答,让我抓狂。 我想做的基本上是在临时表中的有效周和成本之间插入零周和成本。见下表 Practice ID Practice Name Cost Week 1 1 - Practice 1 56.00 18 1 1 - Practice 1 80.00 18 1 1 - Practice 1 122.00 18 1 1 - Practic
Practice ID Practice Name Cost Week
1 1 - Practice 1 56.00 18
1 1 - Practice 1 80.00 18
1 1 - Practice 1 122.00 18
1 1 - Practice 1 -80.00 19
1 1 - Practice 1 80.00 19
1 1 - Practice 1 80.00 21
3 3 - Practice 3 80.00 24
3 3 - Practice 3 18.00 28
3 3 - Practice 3 50.00 29
3 3 - Practice 3 18.00 30
3 3 - Practice 3 18.00 34
3 3 - Practice 3 18.00 35
4 4 - Practice 4 36.00 29
4 4 - Practice 4 299.81 31
4 4 - Practice 4 54.00 32
4 4 - Practice 4 132.00 34
4 4 - Practice 4 314.00 35
4 4 - Practice 4 18.00 35
4 4 - Practice 4 501.00 36
4 4 - Practice 4 342.00 36
7 7 - Practice 7 28.00 24
7 7 - Practice 7 56.00 27
7 7 - Practice 7 40.00 27
我想做的是,对于我需要以某种方式插入零周数和成本的每个实践,在1到36之间缺少周数
Practice ID Practice Name Cost Week
1 1 - Practice 1 0.00 12
1 1 - Practice 1 0.00 13
1 1 - Practice 1 0.00 14
1 1 - Practice 1 0.00 15
1 1 - Practice 1 0.00 16
1 1 - Practice 1 0.00 17
1 1 - Practice 1 56.00 18
1 1 - Practice 1 80.00 18
1 1 - Practice 1 122.00 18
1 1 - Practice 1 -80.00 19
1 1 - Practice 1 80.00 19
1 1 - Practice 1 80.00 21
正如你所看到的,这可能看起来很简单,但我从一系列连接中获取上述付款。实际的一周是从一个时间表中获取的,其中付款的日期时间与周数的日期时间相连接。然后,如果有匹配的付款,则返回带有付款日期的付款
我在下面加入了一个非个性化的查询版本
如果付款表中没有付款,则无法返回周数,因为没有可加入的项目。(很难解释)我曾尝试创建一个临时表,其中只包含财政周数,然后更新有付款的临时表,但由于每个机构每周没有固定的付款数量,因此我无法确定需要多少周条目
如果有人能帮上忙,他们就是救命恩人,因为这让我头痛了一天左右
SELECT R.[Practice id]
,W.[Practice Short Name]
,convert(VARCHAR(10), R.[Practice id]) + ' - ' + W.[Practice Short Name] AS PracticeNumberName
,SUM(R.[Cost]) AS 'Cost'
,I.FiscalWeek
,I.WeekEndingDate
,R.[Owner]
,R.[Description]
FROM dbo.Payments R
INNER JOIN dbo.Time I ON convert(VARCHAR(10), R.[AllocationDate], 121) = convert(VARCHAR(10), I.CalendarDate, 121)
INNER JOIN dbo.OtherDetails W ON R.[Practice id] = W.[Practice Id]
WHERE [paymenttype] NOT IN (5,14,15)
AND I.[FiscalYear] in (2013,2014)
AND I.[FiscalWeek] between 1 and 36
AND R.[cost] <> 0
AND R.[Practice id] IN (1,2,3,4,5,6,7,8,9,10,11,12)
GROUP BY R.[Practice id]
,W.[Practice Short Name]
,convert(VARCHAR(10), R.[Practice id]) + ' - ' + W.[Practice Short Name]
,I.FiscalWeek
,I.WeekEndingDate
,R.[Owner]
,R.[Description]
HAVING sum(R.[cost]) <> 0
选择R[机构id]
,W.[机构简称]
,将(VARCHAR(10),R[机构id])+'-'+W[机构简称]转换为PracticeNumber Name
,总和(R[成本])为“成本”
,I.FiscalWeek
,I.周末日期
,R.[所有者]
,R.[说明]
来自dbo.R
转换时的内部联接dbo.Time I(VARCHAR(10),R[AllocationDate],121)=转换(VARCHAR(10),I.CalendarDate,121)
R.[Practice id]=W.[Practice id]上的内部联接dbo.OtherDetails W
其中[paymenttype]不在(5,14,15)中
我[财政年度]在(20132014)
而我[财政周]在1到36岁之间
和R[成本]0
和(1,2,3,4,5,6,7,8,9,10,11,12)中的R[实践id]
按R分组[机构id]
,W.[机构简称]
,convert(VARCHAR(10),R[机构id])+'-'+W[机构简称]
,I.FiscalWeek
,I.周末日期
,R.[所有者]
,R.[说明]
具有总和(R[成本])0
像这样的事情会让你开始。你可以关注细节
select i.FiscalWeek, isnull(somefield, 0) cost
, etc
from time i left join payments r on etc
left join other tables
这将为您提供所有会计周,无论其他表中是否有匹配的记录