简体   繁体   中英

How can I replicate the following excel calculations in SQL statements ?

I start with columns Week , Prod and Qty :

初始数据集

I'd like my output to be the following :

所需的输出

I have used the following excel formulas to get this output:

In Cell D2 : =IF(B2=B1, IF(C2=C1, "N", "Y"), "N") In Cell E2 : =IF(D2="N", "NA", C2-C1)

I'd like to replicate the same in SQL statements. I'm not sure how I can get started.

I understand that you are trying to compare the quantity of each given product over consecutive weeks. In SQL, you can achieve this by joining the table with itself, using a LEFT JOIN clause (same product and next week). When there is no match (no next week for the given product), the join clause will yield NULL values. Based on this, you can then implement the comparison logic.

You tagged your question with both oracle and mysql , I don't know if it's really on purpose, but this solution should work for both :

select
    t1.week,
    t1.prod,
    t1.qty,
    case 
        when t2.week is null or t1.qty = t2.qty then 'N'
        else 'Y' 
    end week_over_week_change,
    case 
        when t2.week is null or t1.qty = t2.qty then 'NA'
        else t1.qty - t2.qty 
    end week_over_week_change_qty
from table t1
left join table t2 on t2.prod = t2.prod and t2.week = t1.week + 1

Querying Excel directly:

Sub TestQuery()

    Dim oConn As New ADODB.Connection
    Dim oRS As ADODB.Recordset
    Dim sPath, t
    Dim sSQL As String, s As String

    sSQL = " select t1.*, t2.qty, IIf(t1.Qty=t2.Qty,'N','Y') as changed,  " & _
           " IIf(t1.Qty=t2.Qty,'NA',t2.qty-t1.qty) as change " & _
           " from [Sheet4$] t1, [Sheet4$] t2 " & _
           " where t1.Prod=t2.Prod and t2.Week = t1.week+1 "

    sPath = ThisWorkbook.Path & "\" & ThisWorkbook.Name

    oConn.Open "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
               "DBQ=" & sPath & ";"

    Set oRS = oConn.Execute(sSQL)

    If Not oRS.EOF And Not oRS.BOF Then
        ToSheet Sheet5.Range("A1"), oRS
    Else
        MsgBox "No records found"
    End If

End Sub

Sub ToSheet(rng, rs)
    Dim f, i
    i = 0
    rng.Resize(1000, 200).ClearContents
    For Each f In rs.Fields
        rng.Offset(0, i).Value = f.Name
        i = i + 1
    Next f
    rng.Offset(1, 0).CopyFromRecordset rs
End Sub

Input/output:

在此处输入图片说明

This is in Oracle, but using analytic functions which are standard. The syntax may be slightly different in other SQL products, but the syntax of the functions themselves should be the same. I also use a case expression, which is also standard.

select week, prod,
       case when qty != lag(qty) over (partition by prod order by week)
            then 'Y' else 'N' end as week_over_week_change,
       qty - lag(qty) over (partition by prod order by week) as weekly_qty_change
from   [your table, view, or whatever]
........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM