Sql 我的查询正在返回重复项

Sql 我的查询正在返回重复项,sql,sql-server,database,duplicates,Sql,Sql Server,Database,Duplicates,我编写了一个SQL查询来过滤许多条件,并使用distinct只查找唯一的记录 具体来说,我只需要将AccountID字段设置为唯一,每个AccountID都有多个addressclientid 查询可以工作,但会产生一些重复项 进一步的警告是: 每个AccountID 对于AccountID 我只想返回accountid,它们的状态与指定的状态不同,因此我没有在中使用,因为我不需要这两种状态 我只想找到AccountID列的唯一值 如果有人能帮助改进下面的查询,我们将不胜感激 SELECT AF

我编写了一个SQL查询来过滤许多条件,并使用distinct只查找唯一的记录

具体来说,我只需要将
AccountID
字段设置为唯一,每个
AccountID
都有多个
addressclientid

查询可以工作,但会产生一些重复项

进一步的警告是:

  • 每个
    AccountID
  • 对于
    AccountID
  • 我只想返回
    accountid
    ,它们的状态与指定的状态不同,因此我没有在中使用,因为我不需要这两种状态

    我只想找到
    AccountID
    列的唯一值

    如果有人能帮助改进下面的查询,我们将不胜感激

    SELECT AFS_Account.AddressClientID
        ,afs_transunit.AccountID
        ,SUM(afs_transunit.Units)
    FROM AFS_TransUnit
        ,AFS_Account
    WHERE afs_transunit.AccountID IN (
            -- Gets accounts which only have non post statuses
            SELECT DISTINCT accountid
            FROM afs_trans
            WHERE accountid NOT IN (
                    SELECT accountid
                    FROM afs_trans
                    WHERE STATUS IN (
                            'POSTPEND'
                            ,'POSTWAIT'
                            )
                    )
                -- This gets the unique accountIDs which only have transactions with Y status,
                -- and removes any which have both Y and N.
                AND AccountID IN (
                    SELECT DISTINCT accountid
                    FROM afs_trans
                    WHERE IsAllocated = 'Y'
                        AND accountid NOT IN (
                            SELECT DISTINCT AccountID
                            FROM afs_trans
                            WHERE IsAllocated = 'N'
                            )
                    )
            )
        AND AFS_TransUnit.AccountID = AFS_Account.AccountID
    GROUP BY afs_transunit.AccountID
        ,AFS_Account.AddressClientID
    HAVING SUM(afs_transunit.Units) > 100
    
    谢谢

    SELECT AFS_Account.AddressClientID
        ,afs_transunit.AccountID
        ,SUM(afs_transunit.Units)
    FROM AFS_TransUnit
    INNER JOIN AFS_Account ON AFS_TransUnit.AccountID = AFS_Account.AccountID
    INNER JOIN afs_trans ON afs_trans.acccountid = afs_transunit.accountid
    WHERE afs_trans.STATUS NOT IN ('POSTPEND','POSTWAIT')
        -- AND afs_trans.isallocated = 'Y'
    GROUP BY afs_transunit.AccountID
        ,AFS_Account.AddressClientID
    HAVING SUM(afs_transunit.Units) > 100
    and max(afs_trans.isallocated) = 'Y' 
    and min(afs_trans.isallocated) = 'Y'
    
    使用ANSI SQL联接语法修改查询。在加入表时,只需指定条件,而无需使用已有的子查询


    使用ANSI SQL联接语法修改查询。在加入表时,只需指定条件,而无需使用现有的子查询。

    既然您确认在
    AccountID
    列的两个表之间存在一对多关系,您可以使用
    AccountID
    Max
    值来获得不同的值:

    SELECT afa.AddressClientID
        ,MAX(aft.AccountID)
        ,SUM(aft.Units)
    FROM AFS_TransUnit aft
    INNER JOIN AFS_Account afa ON aft.AccountID = afa.AccountID
    GROUP BY afa.AddressClientID
    HAVING SUM(aft.Units) > 100
        AND MAX(aft.AccountID) IN (
            -- Gets accounts which only have non post statuses
            -- This gets the unique accountIDs which only have transactions with Y status,
             -- and removes any which have both Y and N.
            SELECT DISTINCT accountid
            FROM afs_trans a
            WHERE [STATUS] NOT IN ('POSTPEND','POSTWAIT')
                AND a.accountid IN (
                    SELECT t.accountid
                    FROM (
                        SELECT accountid
                            ,max(isallocated) AS maxvalue
                            ,min(isallocated) AS minvalue
                        FROM afs_trans
                        GROUP BY accountid
                        ) t
                    WHERE t.maxvalue = 'Y'
                        AND t.minvalue = 'Y'
                    )
            )
    

    由于您确认在
    AccountID
    列的两个表之间存在一对多关系,因此可以使用
    AccountID
    Max
    值来获得不同的值:

    SELECT afa.AddressClientID
        ,MAX(aft.AccountID)
        ,SUM(aft.Units)
    FROM AFS_TransUnit aft
    INNER JOIN AFS_Account afa ON aft.AccountID = afa.AccountID
    GROUP BY afa.AddressClientID
    HAVING SUM(aft.Units) > 100
        AND MAX(aft.AccountID) IN (
            -- Gets accounts which only have non post statuses
            -- This gets the unique accountIDs which only have transactions with Y status,
             -- and removes any which have both Y and N.
            SELECT DISTINCT accountid
            FROM afs_trans a
            WHERE [STATUS] NOT IN ('POSTPEND','POSTWAIT')
                AND a.accountid IN (
                    SELECT t.accountid
                    FROM (
                        SELECT accountid
                            ,max(isallocated) AS maxvalue
                            ,min(isallocated) AS minvalue
                        FROM afs_trans
                        GROUP BY accountid
                        ) t
                    WHERE t.maxvalue = 'Y'
                        AND t.minvalue = 'Y'
                    )
            )
    


    设置SQLFIDLE将有助于调试您的问题。另外:您正在使用哪些数据库管理系统?博士后?Oracle?此联接上的两个表之间是否存在一对一关系:
    AFS\u TransUnit.AccountID=AFS\u Account.AccountID
    ?@jpw无需担心!我猜这两个表之间存在一对多的关系。所以,我只是想和OP确认一下这是否属实。@a_horse_,没有名字-我正在使用MS SQL Server。设置sqlfiddle将有助于调试您的问题。另外:您正在使用哪些数据库管理系统?博士后?Oracle?此联接上的两个表之间是否存在一对一关系:
    AFS\u TransUnit.AccountID=AFS\u Account.AccountID
    ?@jpw无需担心!我猜这两个表之间存在一对多的关系。因此,我只想与OP确认这是否属实。@a_horse_,没有名称-我正在使用MS SQL Server。此查询在功能上与原始查询不同,不会返回相同的结果。谢谢,但是我只需要返回AccountID的结果,其中only IsAllocated设置为Y。AccountID可以有多个事务,它们可以是Y和N。因此,如果accountid有“Y”和“N”,那么它不应该出现在结果中。对吗?@vkp对。例如,trans表中的AccountID可能有10条记录,9条可能是Y,但1条可能是N。我不希望修改这些AccountID以包括
    having
    子句中的条件。看看它是否有效。此查询在功能上与原始查询不同,不会返回相同的结果。谢谢,但是我只需要返回AccountID的结果,其中only IsAllocated设置为Y。AccountID可以有多个事务,它们可以是Y和N。因此,如果AccountID有“Y”和“N”,则,它不应该出现在你的结果中……对吗?@vkp没错。例如,trans表中的AccountID可能有10条记录,9条可能是Y,但1条可能是N。我不希望修改这些AccountID以包括
    having
    子句中的条件。看看它是否有效。@SolilquyOfChaos我相信您可能想调整一些非in查询。我将很快发表我的建议。@SolilquyOfChaos我相信您可能希望调整一些非in查询。我将很快发布我的建议。