SQL Server Find and set only first duplicated value remaining duplicates set to 0

mqxuamgl  于 2023-03-28  发布在  其他
关注(0)|答案(1)|浏览(114)

Could someone help me to make a query that sets the value to field but only once per duplicated record? I do have some duplicated rows and I need a column that stores the value of my transaction only once. I did tried but without good effect, right now I'm stuck with a query like this.

SELECT         JOURNALNUMBER, SUBLEDGERVOUCHER, ACCOUNTINGCURRENCYAMOUNT, MAINACCOUNTVALUE, 
                    ITEMID,  QTY, INVENTTRANSID, 
                         (SELECT        count(journalnumber)
                           FROM            TestGeneralTransY AS tab
                           WHERE        (TestGeneralTransY.JOURNALNUMBER = JOURNALNUMBER) AND (TestGeneralTransY.ITEMID = ITEMID)
                           AND (TestGeneralTransY.mainaccountvalue = mainaccountvalue) AND (TestGeneralTransY.ACCOUNTINGCURRENCYAMOUNT = ACCOUNTINGCURRENCYAMOUNT)
                           ) AS MInAmount
FROM            TestGeneralTransY

I want that the MinAmount would show only one valu per transaction like in PAF-004644308 I would need one row with value -79,5 and the second with 0. I do need this because I need to do sum calculations and with duplicated values its not possible for me.

JOURNALNUMBERSUBLEDGERVOUCHERACCOUNTINGCURRENCYAMOUNTMAINACCOUNTVALUEITEMIDQTY INVENTTRANSIDMInAmount
PAF-004644305WZ-0187132-39.7500003110001 MPL-WST-0044-186.000000 PT020719031 1
PAF-004644308WZ-0187135-79.5000003110001 MPL-WST-0036-260.000000 PT021519260 2
PAF-004644308WZ-0187135-79.5000003110001 MPL-WST-0036-260.000000 PT021519260 2
PAF-004644324WZ-0187146-397.490000 3110001 MPA-PAL-0010-10.000000PT022252398 1
PAF-004647078WZ-0187322-10573.9900003110001 MPL-FOR-0227-4500.000000PT022149061 1
PAF-004654294FSL22000020 397.4900003110001 MPA-PAL-0010-10.000000PT022252398 1
PAF-004654296FSL22000021 79.5000003110001 MPL-WST-0036-260.000000 PT021519260 2
PAF-004654296FSL22000021 79.5000003110001 MPL-WST-0036-260.000000 PT021519260 2
PAF-004654297FSL22000022 39.7500003110001 MPL-WST-0044-186.000000 PT020719031 1
PAF-004660123FSL22000026 10573.9900003110001 MPL-FOR-0227-4500.000000PT022149061 1
PAF-004667259WZ-0188549-357.740000 3110001 MPA-PAL-0010-9.000000PT022252398 1
PAF-004667260WZ-0188562-119.250000 3110001 MPL-WST-0044-260.000000 PT020719031 3
PAF-004667260WZ-0188562-119.250000 3110001 MPL-WST-0044-260.000000 PT020719031 3
PAF-004667260WZ-0188562-119.250000 3110001 MPL-WST-0044-260.000000 PT020719031 3
PAF-004673442WZ-0189010-73.7700003110001 MPA-PAL-0010-1.000000PT022252398 1
PAF-004673524WZ-0189011-39.7500003110001 MPL-WST-0036-260.000000 PT021519260 1
PAF-004686484FSL22000103 39.7500003110001 MPL-WST-0036-260.000000 PT021519260 1
PAF-004686612FSL22000104 431.5100003110001 MPA-PAL-0010-9.000000PT022252398 2
PAF-004686612FSL22000104 431.5100003110001 MPA-PAL-0010-1.000000PT022252398 2
PAF-004686824FSL22000106 119.2500003110001 MPL-WST-0044-260.000000 PT020719031 3
PAF-004686824FSL22000106 119.2500003110001 MPL-WST-0044-260.000000 PT020719031 3
PAF-004686824FSL22000106 119.2500003110001 MPL-WST-0044-260.000000 PT020719031 3
bnlyeluc

bnlyeluc1#

You can use ROW_NUMBER() to numerate your items, and then CASE to return ACCOUNTINGCURRENCYAMOUNT only for first one.

select
    JOURNALNUMBER,
    SUBLEDGERVOUCHER,
    case
        when rn = 1 then ACCOUNTINGCURRENCYAMOUNT
        else 0
    end ACCOUNTINGCURRENCYAMOUNT,
    MAINACCOUNTVALUE,
    ITEMID,
    QTY,
    INVENTTRANSID
from
    (
        select
            JOURNALNUMBER,
            SUBLEDGERVOUCHER,
            ACCOUNTINGCURRENCYAMOUNT,
            MAINACCOUNTVALUE,
            ITEMID,
            QTY,
            INVENTTRANSID,
            ROW_NUMBER() OVER (
                PARTITION BY JOURNALNUMBER
                ORDER BY (SELECT NULL)
            ) rn
        from
            TestGeneralTransY
    )

Please check if OVER clause of ROW_NUMBER() makes sense for your data.

相关问题