donglu3087 2015-09-18 04:13
浏览 79
已采纳

SQL Server中的交叉计算

I have a complicated request. I have to make a report in PHP like the picture below. Part summarized yield must be filled with across calculation. Like I explain in formula column, value in part summarized yield got from calculation between location yield and part summarized yield. (See this formula)

Example excel report

How to do that calculation in a PHP report? I already tried using a cursor, but it still did not work.

Here is my cursor calculation:

--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

Here table structure : enter image description here

I need to fill part_summary field using formula that i show in excel. In formula show, calculation using cross field.

  • 写回答

1条回答 默认 最新

  • doulan1866 2015-09-18 14:19
    关注

    Here is how you can do it (without nasty cursors). Just use row number over your ordering/aggregation criteria to find the next row, left join each row with its next (not forgetting the aggregation criteria) and it's done.

    Let's use an example (and here is how you properlu post a table structure):

    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
    

    and to select using row number and use the result in a self-join

    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
    

    You can see the results in the 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据