SQL:寻找舍入的重复项

时间:2018-06-16 14:42:02

标签: sql sql-server duplicates rounding

我有一张表似乎有可能在被撤销之前四舍五入到最接近的美元的交易

考虑到这样的事情:

  func Cart(w   http.ResponseWriter,    r   *http.Request)  {   

data := CartData{ 
    Name: "Cart",
    Equipment: model.GetEquipment(model.Db),
    Pages: []Page{
        {
            Title: "Meine Geräte", 
            Active: false,
            Link: "/my-equipment",
        },
        {
            Title: "Equipment", 
            Active: false,
            Link: "/equipment",
        },
        {
            Title: "Logout",
            Active: false,
            Link: "/logout",
        },
    },

}

    equipment,_ := model.GetEquipmentByID(r.FormValue("id"))
    session, _ := store.Get(r, "cookie-name")

    Strings := strconv.Itoa(equipment.ID)
    fmt.Println(Strings)
    StringsWithComma := Strings + ","
    session.Values["EquipmentIDs"] = session.Values["EquipmentIDs"] + StringsWithComma // THIS CODE LINE DOES NOT WORK, I want to expand "EquipmentIDs" with the new ID

tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
tmpl.ExecuteTemplate(w, "base", data)
}
}

我希望看到有多少帐户拥有这种价值模式,以及一个圆形的反对价部分。因此,我想从上表中提取AccountIDs 1和3,例如。

我对AccountID 4不感兴趣,因为这是一个精确的(绝对值)匹配,或者AccountID 5,它有一个"舍入的副本"但不是反对的。

任何人都知道如何在SQL中实现这一目标吗?

3 个答案:

答案 0 :(得分:2)

只需与舍入值进行比较。

SELECT
    T.*
FROM
    Transactions AS T
WHERE
    EXISTS (
        SELECT
            'inverted rounded operation detected'
        FROM
            Transactions AS N
        WHERE
            T.AccountID = N.AccountID AND
            ROUND(T.[Transaction], 0) = -1 * ROUND(N.[Transaction], 0))

答案 1 :(得分:2)

您可能有多个匹配的交易。因此,您应该使用row_number()进行匹配:

with t as (
      select t.*,
             row_number() over (partition by accountid, floor(transaction) order by transaction) as seqnum
      from transactions t
     )
select t.*
from t
where exists (select 1
              from t t2
              where t2.accountid = t.accountid and
                    floor(t2.transaction) = floor(t.transaction) and
                    t2.seqnum = t.seqnum
             );

答案 2 :(得分:0)

舍入所有正交易值并查找具有相等负值的所有帐户。

select acct.accountid
from
    acct
    inner join (
    select
        accountid,
        round(transaction, 0) as whole_trans
    from
        acct
    where
        transaction > 0
        and transaction != round(transaction, 0)
    ) as unround on unround.accountid = acct.accountid
where
    unround.whole_trans = -1 * acct.transaction;