【问题标题】:Cross Calculation in SQL ServerSQL Server 中的交叉计算
【发布时间】:2015-09-18 04:13:34
【问题描述】:

我有一个复杂的请求。我必须用 PHP 做一个报告,如下图所示。 Part summarized yield 必须在计算中填写。 就像我在公式列中解释的那样,part summarized yield 中的值来自location yieldpart summarized yield 之间的计算。 (见这个公式)

如何在 PHP 报告中进行计算? 我已经尝试过使用游标,但它仍然不起作用。

这是我的光标计算:

--In PHP file, i make query to insert data first to table tyield_summary
--Cursor to input yield_summary
Declare @nourut varchar(2), @maxnourut varchar(2), @bpnum varchar(20), @pnum varchar(20), @curnourut varchar(2), @psum decimal(18,2), @ysum decimal(18,2)

DECLARE StockCursor CURSOR
FOR

select no_urut, part_number, Part_Summary
from tyield_summary 
where no_urut<>'99'
order by part_number desc, no_urut asc

set @bpnum=''

OPEN StockCursor
FETCH NEXT FROM StockCursor INTO @nourut, @pnum, @psum

WHILE @@FETCH_STATUS=0
BEGIN


  if @bpnum=@pnum
   begin

    select top 1 @curnourut=no_urut
    from tyield_summary 
    where part_number=@pnum 
    and no_urut<@nourut 
    order by no_urut desc

    set @bpnum=@pnum

    select @maxnourut = max(no_urut) from tyield_summary 
    where part_number=@pnum 


    update tyield_summary
    set yield_summary = case when Part_Summary=0 then @psum else (Part_Summary*@psum)/100 end
    where part_number=@pnum
    and no_urut=@curnourut



   end 
  else
   begin

    set @bpnum=@pnum

   end

  FETCH NEXT FROM StockCursor INTO @nourut, @pnum, @psum

END

CLOSE StockCursor
DEALLOCATE StockCursor

这里的表结构:

我需要使用我在 Excel 中显示的公式填写 part_summary 字段。 在公式显示中,使用交叉字段计算。

【问题讨论】:

  • 你被困在哪里了?请同时提供表结构。
  • 我已经更新了我的帖子并提供了表格结构。请帮忙
  • 将图像发布为“表格结构”是懒惰的人。无论如何,只需使用您的排序/聚合标准创建一个行号列并计算每行 * 行 +1,或者如果聚合中的实际行没有下一个值,则只获取行值。还要避免光标

标签: php sql sql-server tsql


【解决方案1】:

你可以这样做(没有讨厌的光标)。 只需在您的排序/聚合标准上使用行号即可找到下一行, left join 每一行和它的下一行(不要忘记聚合条件),它就完成了。

让我们举个例子(这里是你正确发布表结构的方法):

create table Lazydude
(
   Partno varchar(20) not null
  ,Seq int not null
  ,[Value] float not null
)
GO

insert into Lazydude (Partno, Seq, [Value])
values
 ('AAA', 1, 77.7)
,('BBB', 0, 2) 
,('BBB', 3, 3) 
,('BBB', 9, 5) 
,('CCC', 1, 33.3) 
,('CCC', 2, 33.3) 
GO

并使用行号进行选择并在自联接中使用结果

with Temp as(
  select Partno, Seq, [Value]
  ,ROW_NUMBER() OVER(ORDER BY Partno, Seq) AS [Row] 
  from Lazydude
 )
 select t0.Partno, t0.Seq, t0.[Value], t0.[Row]
 , t1.row as [Next Row], t1.[Value] as [Next Value]
 , case when t1.row is null
     then t0.[Value]
     else t0.Value * t1.Value
  end as [The Calculation]
from Temp t0
left join Temp t1 on t1.[Row] = t0.[Row] + 1 and t1.Partno = t0.Partno

你可以在SQL Fiddle看到结果

Partno  Seq Value   Row  Next Row   Next Value  The Calculation
AAA     1   77.7    1    (null)     (null)       77.7
BBB     0   2       2    3          3            6
BBB     3   3       3    4          5            15
BBB     9   5       4    (null)     (null)       5
CCC     1   33.3    5    6          33.3         1108.8899999999999
CCC     2   33.3    6    (null)     (null)       33.3

【讨论】:

  • 非常感谢吉恩。我尝试使用我的 sql 命令并完美运行。即使在最后一个序列中仍然有错误。我会尝试修复它。非常感谢您的帮助
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多