将Excel公式转换为SQL语法计算

时间:2018-04-06 02:28:37

标签: sql sql-server excel

我需要帮助解决将Excel公式转换为SQL的问题。

以下是公式:

=IF(
  ([@[Net Available Stock Quantity]]+[@Subcon]+[@[In Progress WIP]]+
   IFERROR([@[Raw Material In Store]]/[@[Demand Material]],0)+
   IFERROR([@[Outstanding Raw Material]]/[@[Demand Material]],0)+
   [@[Outstanding WIP]])>[@[Maximun Stock]],
    ([@[Net Available Stock Quantity]]+[@Subcon]+[@[In Progress WIP]]+
    IFERROR([@[Raw Material In Store]]/[@[Demand Material]],0)+
    IFERROR([@[Outstanding Raw Material]]/[@[Demand Material]],0)+
    [@[Outstanding WIP]]-[@[Maximun Stock]]),
     IF(([@[Net Available Stock Quantity]]+[@Subcon]+[@[In Progress WIP]]+
     IFERROR([@[Raw Material In Store]]/[@[Demand Material]],0)+
     IFERROR([@[Outstanding Raw Material]]/[@[Demand Material]],0)+[@[Outstanding WIP]])<[@[Minimum Stock]],
      -[@[Minimum Stock]]+([@[Net Available Stock Quantity]]+[@Subcon]+[@[In Progress WIP]]+
      IFERROR([@[Raw Material In Store]]/[@[Demand Material]],0)+
      IFERROR([@[Outstanding Raw Material]]/[@[Demand Material]],0)+[@[Outstanding WIP]]),0)
)

这就是我所做的:

USE myDB

SELECT [Item No],
    CASE
        WHEN ([Net Available Stock Quantity]+Subcon+[In Progress WIP]+CASE ( 
            WHEN ([Raw Material In Store] AND [Demand Material] != 0 THEN [Raw Material In Store]/[Demand Material] ELSE 0) +
            WHEN ([Outstanding Raw Material] AND [Demand Material] != 0 THEN [Outstanding Raw Material]/[Demand Material] ELSE 0) +
            [Outstanding WIP]) > [Maximum Stock])
        THEN ([Net Available Stock]+Subcon+[Outstanding WIP]+CASE (
            WHEN ([Raw Material] AND [Demand Material] != 0 THEN [Raw Material]/[Demand Material] ELSE 0) +
            WHEN ([Outstanding Raw Material] AND [Demand Material] != 0 THEN [Outstanding Raw Material]/[Demand Material] ELSE 0) +
            [Outstanding WIP) - [Maximum Stock])
        ELSE (
            CASE
            WHEN([Net Available Stock Quantity]+Subcon+[Outstanding WIP]+CASE (
                WHEN ([Raw Material In Store AND [Demand Material] != 0 THEN [Raw Material In Store]/[Demand Material] ELSE 0) +
                WHEN ([Outstanding Raw Material] AND [Demand Material] != 0 THEN [Outstanding Raw Material]/[Demand Material] ELSE 0) +
            [Outstanding WIP] < [Minimum Stock])
            THEN ([Minimum Stock]+[Net Available Stock Quantity]+Subcon+[Outstanding WIP]+CASE (
                WHEN ([Raw Material In Store] AND [Demand Material] != 0 THEN [Raw Material In Store]/[Demand Material] ELSE 0) +
                WHEN ([Outstanding Raw Material] AND [Demand Material] != 0 THEN [Outstanding Raw material]/[Demand Material] ELSE 0) +
            [Outstanding WIP])
    END AS [Final Stock Over Max],
FROM myTable

那么,我粘贴到SQL Server查询时的问题,

  1. 开[项目编号]无效的列名称。 (在某些名称上有这样的大量错误)
  2. 第二个WHEN无效语法。
  3. 在ELSE内部的CASE语法无效。
  4. On AS无效语法。
  5. 我使用THIS LINK作为转换excel公式的指南。

    你们认为我做错了什么?我尝试转换了2天,很多例子我都试过,但仍然出现错误。

    更新:

    myTable结构:

    [Item No]         nchar(24) 
    Description       nchar(60) 
    UOM               nchar(10) 
    Stock             int   
    Sales             int   
    [Minimum Stock]   int   
    [Maximum Stock]   int   
    [Subcon Code]     char(24)  
    [Raw Material]    char(24)  
    [Description Material]  nchar(60)   
    [Demand Material]   char(10)    
    [In Progress WIP]   char(10)    
    Subcon            int   
    [Net Available Stock Quantity]  int 
    [Stock Over Maximum]            int 
    [Raw Material Outstanding]      int 
    [Raw Material In Store]         int
    [Outstanding WIP]               int
    

1 个答案:

答案 0 :(得分:1)

试试这段代码,随时重新排序......

    CASE
    WHEN ([Net Available Stock Quantity] + [Subcon] + [In Progress WIP] +
      COALESCE([Raw Material In Store] / NULLIF([Demand Material], 0), 0) +
      COALESCE([Outstanding Raw Material] / NULLIF([Demand Material], 0), 0) +
      [Outstanding WIP]) > [Maximun Stock] THEN CASE
        WHEN
          ([Net Available Stock Quantity] + [Subcon] + [In Progress WIP] +
          COALESCE([Raw Material In Store] / NULLIF([Demand Material], 0), 0) +
          COALESCE([Outstanding Raw Material] / NULLIF([Demand Material], 0), 0) + [Outstanding WIP]) < [Minimum Stock] THEN [Minimum Stock] + ([Net Available Stock Quantity] + [Subcon] + [In Progress WIP] +
          COALESCE([Raw Material In Store] / NULLIF([Demand Material], 0), 0) +
          COALESCE([Outstanding Raw Material] / NULLIF([Demand Material], 0), 0) + +[Outstanding WIP])
        ELSE ([Net Available Stock Quantity] + [Subcon] + [In Progress WIP] +
          COALESCE([Raw Material In Store] / NULLIF([Demand Material], 0), 0) +
          COALESCE([Outstanding Raw Material] / NULLIF([Demand Material], 0), 0) +
          [Outstanding WIP] - [Maximun Stock])

      END
    ELSE 0
  END