只偏移表格中的一列,该表格有2列?

huangapple go评论64阅读模式
英文:

Offset just one column in table that has 2 columns?

问题

我有一个包含两列的表,我将在查询中将其与另一个表连接起来。我不会粘贴/解释与该连接相关的所有内容,因为它正在进行中,有点混乱。

第一列中包含了1-12月份,在R78中,有相应的月份对应的值。根据月份进行连接可以帮助我将销售额乘以R78相应的月份行,从而得到一个值。

例如,如果销售额为4000美元,并且销售发生在3月份,而员工是一名老员工,那么金额将为4000美元 x 6。我遇到的问题是,如果员工是在4月份开始工作的,他们4月份的销售额可能是2000美元,我需要将其乘以第一个月,即1。

我需要一种方法来做到这一点,而不需要硬编码任何日期,以便将来不必更新此查询。我已经解决了日期部分,但我需要弄清如何创建偏移量。

基本上,我要找的是,如果我想从第4个月开始,我希望R78从值1重新开始。

我尝试过各种各样的order by、offset、fetch查询,但我无法达到我想要的效果。数据类型都是smallint。

尝试过的示例:

SELECT *
FROM dbo.RT_R78
ORDER BY month
OFFSET (select count(*) from dbo.RT_R78) - 11 rows
SELECT *
FROM dbo.RT_R78
ORDER BY month
OFFSET 1 ROWS
FETCH NEXT 11 ROWS ONLY
英文:

I have a table with 2 columns that I will be joining to another in a query. I won't paste/explain everything going on with that join as it is a mess in progress.
In the first column is months 1-12, and in R78, there is corresponding values by month.
The join on month helps me take sales number * R78 corresponding row of month to give me a value.
Example would be if sales number is $4000 and sale came through in March for a long time employee, then it would be $4000 x 6. Issue I am having is that if employee started in April and their sales April was say $2000, I need to times that by the first month, which is 1.
I need a way to do this without hard coding any dates so that this query wont have to be updated in the future. I have the date part figured out but I need to figure out how to create that offset.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

MONTH	R78
1	    1
2	    3
3	    6
4	   10
5	   15
6	   21
7	   28
8	   36
9	   45
10	   55
11	   66
12	   78

Basically what I am looking for is if I want month to start at 4, I want R78 to start over at value 1.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

MONTH	R78
4	    1
5	    3
6	    6
7	   10
8	   15
9	   21
10	   28
11	   36
12	   45

I tried various order by, offset, fetch queries but I can't get to where I would like. Datatypes are both smallint.
Examples tried:

SELECT *
FROM dbo.RT_R78
ORDER BY month
OFFSET (select count(*) from dbo.RT_R78) - 11 rows
SELECT *
FROM dbo.RT_R78
ORDER BY month
OFFSET 1 ROWS
FETCH NEXT 11 ROWS ONLY

答案1

得分: 1

这是一种使用窗口函数 sum() 来帮助从所需月份开始重新构建数据集的方法:

with cte as (
  select *, sum(case when month >= 4 then 1 else 0 end) over(order by month) as rn
  from RT_R78
)
select c.month, r.R78
from cte c
inner join RT_R78 r on r.month = c.rn

结果:

month R78
4 1
5 3
6 6
7 10
8 15
9 21
10 28
11 36
12 45

演示链接

英文:

This is a method that uses the window function sum() to help reconstruct the dataset starting from the desired month :

with cte as (
  select *, sum(case when month &gt;= 4 then 1 else 0 end) over(order by month) as rn
  from RT_R78
)
select c.month, r.R78
from cte c
inner join RT_R78 r on r.month = c.rn

Result :

month R78
4 1
5 3
6 6
7 10
8 15
9 21
10 28
11 36
12 45

Demo here

答案2

得分: 0

以下是翻译好的部分:

如果员工在其他月份开始工作会怎样?
以下是我理解问题后的解决方案:

WITH RT_R78 AS ( -- 创建一个包含12个月份的R78值的表
SELECT * 
FROM (VALUES (1, 1), (2, 3), (3, 6), (4, 10), (5, 15), (6, 21)
           , (7, 28), (8, 36), (9, 45), (10, 55), (11, 66), (12, 78) ) AS t (MonthNumber, R78)
)
, Employees AS ( -- 创建一个包含3名员工和其入职月份的表
SELECT * 
FROM (VALUES ('Employee1', 3), ('Employee2', 4), ('Employee3', 9) ) AS t (EmployeeName, StartMonthNumber)
)
SELECT Employees.EmployeeName, Employees.StartMonthNumber, RT_R78.MonthNumber, RT_R78.R78
FROM Employees
    INNER JOIN RT_R78 ON RT_R78.MonthNumber<=13 - Employees.StartMonthNumber
ORDER BY Employees.EmployeeName, RT_R78.MonthNumber

链接至示例

英文:

What if the employee starts in other months?
Here is my solution (as I understood the question):

WITH RT_R78 AS ( -- give me a table with the R78-Value for the 12 Month
SELECT * 
FROM (VALUES (1, 1), (2, 3), (3, 6), (4, 10), (5, 15), (6, 21)
           , (7, 28), (8, 36), (9, 45), (10, 55), (11, 66), (12, 78) ) AS t (MonthNumber, R78)
)
, Employees AS ( -- give me a table with 3 Employee and their StartMonthNumber
SELECT * 
FROM (VALUES (&#39;Employee1&#39;, 3), (&#39;Employee2&#39;, 4), (&#39;Employee3&#39;, 9) ) AS t (EmployeeName, StartMonthNumber)
)
SELECT Employees.EmployeeName, Employees.StartMonthNumber, RT_R78.MonthNumber, RT_R78.R78
FROM Employees
    INNER JOIN RT_R78 ON RT_R78.MonthNumber&lt;=13 - Employees.StartMonthNumber
ORDER BY Employees.EmployeeName, RT_R78.MonthNumber

https://dbfiddle.uk/EaxfpWyi

huangapple
  • 本文由 发表于 2023年6月16日 06:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定