SQL - 拉取客户端(结果集中每行 1 个客户端)以及这些客户端的最近订单日期

发布于 12-10 18:12 字数 1468 浏览 0 评论 0原文

下面是一些用于提取客户端列表的 SQL。正如你所看到的,我添加了用“--ADDED”注释的行——一个新字段、一个连接和一个排序依据。我试图在结果集中每行显示一个客户端,但对于每个客户端,我想显示最新的订单。最好的方法是什么?我应该在 JOIN 中放置一个 where 还是使用 TOP 1?我想有几种方法,但我想让它尽可能高效。

SELECT 
    CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
    CONVERT(VARCHAR,client.name) As [Client Name], 
    client.clientid As [Id],
    CONVERT(CHAR(10),[ecom_order].[order_date],101)  -- ADDED
FROM  
    client 
    JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
    JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
    -- keep left outer join since some clients won't have any orders, thus no last order
    LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid -- ADDED
WHERE  
    client.name LIKE '%_%' 
    AND 
    client.name <= (
        SELECT MAX(maxsubquery.ordercolumn) FROM (
            SELECT 
                TOP 10 client.name AS ordercolumn 
            FROM  
                client 
                JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
            WHERE  
                client.name LIKE '%_%' 
            ORDER BY client.name
        ) 
        AS maxsubquery)
 ORDER BY 
    client.name,
    [ecom_order].order_date DESC -- ADDED

测试数据结果集示例:

在此处输入图像描述

Below is some SQL to pull a list of clients. As you can see I added the lines annotated by "-- ADDED"--a new field, a join and an order by. I'm trying to display one client per row in my result set, but for each client, I want to display the most recent order. What's the best way to do this? Should I put a where in my JOIN or use the TOP 1? I suppose there are a few ways, but I want to make it as efficient as possible.

SELECT 
    CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
    CONVERT(VARCHAR,client.name) As [Client Name], 
    client.clientid As [Id],
    CONVERT(CHAR(10),[ecom_order].[order_date],101)  -- ADDED
FROM  
    client 
    JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
    JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
    -- keep left outer join since some clients won't have any orders, thus no last order
    LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid -- ADDED
WHERE  
    client.name LIKE '%_%' 
    AND 
    client.name <= (
        SELECT MAX(maxsubquery.ordercolumn) FROM (
            SELECT 
                TOP 10 client.name AS ordercolumn 
            FROM  
                client 
                JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
            WHERE  
                client.name LIKE '%_%' 
            ORDER BY client.name
        ) 
        AS maxsubquery)
 ORDER BY 
    client.name,
    [ecom_order].order_date DESC -- ADDED

Test Data Result Set Example:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气2024-12-17 18:12:54

试试这个

   SELECT 
        CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
        CONVERT(VARCHAR,client.name) As [Client Name], 
        client.clientid As [Id],
      -- Change to get latest
        CONVERT(CHAR(10),Max([ecom_order].[order_date]),101)       
     FROM  client 
        JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
        JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
        -- keep left outer join since some clients won't have any orders, 
     -- thus no last order
        LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid 
    WHERE  
        client.name LIKE '%_%' 
        AND 
        client.name <= (
            SELECT MAX(maxsubquery.ordercolumn) FROM (
                SELECT 
                    TOP 10 client.name AS ordercolumn 
                FROM  
                    client 
                    JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                    JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
                WHERE  
                    client.name LIKE '%_%' 
                ORDER BY client.name
            ) 
            AS maxsubquery)
     GROUP BY client.identifyingnumber,client.name,client.client_id
     ORDER BY 
        client.name

Try this

   SELECT 
        CONVERT(NVARCHAR(255),client.identifyingnumber) As [Client ID], 
        CONVERT(VARCHAR,client.name) As [Client Name], 
        client.clientid As [Id],
      -- Change to get latest
        CONVERT(CHAR(10),Max([ecom_order].[order_date]),101)       
     FROM  client 
        JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
        JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
        -- keep left outer join since some clients won't have any orders, 
     -- thus no last order
        LEFT OUTER JOIN [ecom_order] ON client.clientid = [ecom_order].clientid 
    WHERE  
        client.name LIKE '%_%' 
        AND 
        client.name <= (
            SELECT MAX(maxsubquery.ordercolumn) FROM (
                SELECT 
                    TOP 10 client.name AS ordercolumn 
                FROM  
                    client 
                    JOIN clientstatus ON client.clientstatusid = clientstatus.clientstatusid 
                    JOIN clienttype ON clienttype.clienttypeid = client.clienttypeid 
                WHERE  
                    client.name LIKE '%_%' 
                ORDER BY client.name
            ) 
            AS maxsubquery)
     GROUP BY client.identifyingnumber,client.name,client.client_id
     ORDER BY 
        client.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文