1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] To sum column values in SQL Server

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 31, 2024 às 11:43.

  1. Stack

    Stack Membro Participativo

    In the query shown here, I want to sum the columns of freightCharge and LoadingandUnloadingcharges and add the result as new column totalcost.

    I have tried this code and I'm unable to get the desired result:

    SELECT DISTINCT
    GRN.GoodsReceivedNoteNo,
    CASE
    WHEN COUNT(GRND.GoodsReceivedNoteID) OVER (PARTITION BY GRND.GoodsReceivedNoteID) > 1
    THEN ISNULL((GRND.FreightCharges), 0.00)
    ELSE ISNULL((OEC.FreightCharges), 0.00)
    END FreightCharge,
    CASE
    WHEN COUNT(GRND.GoodsReceivedNoteID) OVER (PARTITION BY GRND.GoodsReceivedNoteID) > 1
    THEN ISNULL((GRND.LoadingCost + GRND.UnloadingCost), 0.00)
    ELSE ISNULL((OEC.LoadingCost + OEC.UnloadingCost), 0.00)
    END LoadingandUnloadingcharges
    FROM
    GoodsReceivedNoteDetail GRND
    LEFT OUTER JOIN
    GoodsReceivedNote GRN ON GRN.GoodsReceivedNoteID = GRND.GoodsReceivedNoteID
    LEFT OUTER JOIN
    OtherExpenseCost OEC ON OEC.GoodsReceivedNoteID = GRN.GoodsReceivedNoteID

    Continue reading...

Compartilhe esta Página