AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 215518
Accepted
heye
heye
Asked: 2018-08-22 11:05:46 +0800 CST2018-08-22 11:05:46 +0800 CST 2018-08-22 11:05:46 +0800 CST

过滤掉同一列中具有不同值的相同id的记录

  • 772

谁能帮我解决以下问题。

create table [order1] (order_id int, CartTransaction_Id int, Status_Id int);
insert into [order1] values
(357488,87214,1),
(357489,87214,8),
(357490,87214,1),
(357491,87214,1),
(343980,87216,1),
(357483,87216,1);

create table [Status1] (Type VARCHAR(100), Status_Id int);
insert into [Status1] values
('New','1'),
('Awaiting Approval','8'),
('New','1'),
('New','1'),
('New','1'),
('New','1');

    select
A.Order_Id, A.CartTransaction_Id, A.Status_Id, B.Type, B.Status_Id
FROM order1 A
JOIN dbo.Status1 B ON dbo.A.Status_Id = dbo.B.Status_Id
--AND B.Type = 'New' 
order by A.CartTransaction_Id

下面是我从上面的查询中得到的输出。

在此处输入图像描述

以下是预期的结果

在此处输入图像描述

仅当 CartTransaction_Id 组中所有 order_id 的状态类型为“新”时,我才需要填充数据

从捐赠中,我需要过滤掉 CartTransaction_Id (87214),因为 order_id status_type 之一是在等待批准

sql-server partitioning
  • 4 4 个回答
  • 2693 Views

4 个回答

  • Voted
  1. Best Answer
    James Z
    2018-08-22T11:19:06+08:002018-08-22T11:19:06+08:00

    例如,您可以这样做:

    select
    A.Order_Id, A.CartTransaction_Id, A.Status_Id, B.Type, B.Status_Id
    FROM order1 A
    JOIN dbo.Status1 B ON dbo.A.Status_Id = dbo.B.Status_Id
    where
    not exists (select 1 from order1 o1 where o1.CartTransaction_Id = A.CartTransaction_Id and o1.Status_Id != 1)
    order by A.CartTransaction_Id
    

    如果您有很多数据,那么您可能需要检查其他替代方案,例如 max + over

    • 1
  2. mathewb
    2018-08-22T19:14:11+08:002018-08-22T19:14:11+08:00

    尝试这个:

    SELECT  A.Order_Id, A.CartTransaction_Id, A.Status_Id, B.Type, B.Status_Id
    FROM    order1 A
    JOIN    Status1 B
        ON A.Status_Id = B.Status_Id
    OUTER APPLY (SELECT TOP 1 C.CartTransaction_Id FROM order1 C WHERE A.CartTransaction_Id = C.CartTransaction_Id AND C.Status_Id <> 1) C
    WHERE   C.CartTransaction_Id IS NULL
    ORDER BY A.CartTransaction_Id
    
    • 1
  3. Unkush
    2018-08-22T11:20:28+08:002018-08-22T11:20:28+08:00

    您的 Status1 表是否需要有多个“NEW”条目...如果不是下面的查询应该可以工作

    select
    A.Order_Id, A.CartTransaction_Id, A.Status_Id, B.Type, B.Status_Id
    FROM order1 A
    JOIN dbo.Status1 B ON A.Status_Id = B.Status_Id
    WHERE NOT EXISTS (SELECT 1 FROM order1 c WHERE c.CartTransaction_Id = 
    a.CartTransaction_Id AND c.Status_Id <> 1)
    order by A.CartTransaction_Id
    
    • 0
  4. Lee Walters
    2018-08-22T21:25:29+08:002018-08-22T21:25:29+08:00

    首先,您不需要向 [Status1] 表插入那么多

    create table [Status1] (Type VARCHAR(100), Status_Id int);
    insert into [Status1] values
    ('New','1'),
    ('Awaiting Approval','8');
    

    对你的加入来说已经足够了。

    这是我对使用 ROW_NUMBER 得到你想要的答案的看法。

    ;with Orders as (
        select A.Order_Id [OrderID]
        , A.CartTransaction_Id [CartTransactionID]
        , A.Status_Id [OrderStatus]
        , B.Type [StatusType]
        , B.Status_Id [StatusID]
        , ROW_NUMBER() OVER(PARTITION BY A.CartTransaction_Id, A.Status_Id ORDER BY A.CartTransaction_Id, A.Status_Id) RowNo
    from order1 a join status1 b on a.status_id = b.status_id
    )
    select o.OrderID
        , o.CartTransactionID
        , o.OrderStatus
        , o.StatusType
        , o.StatusID 
    from orders o
    Where 1=1
    and o.RowNo = 1
    and o.StatusID = 1 --o.StatusType='new'
    
    • 0

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve