dongsuishou8039 2018-08-27 18:57
浏览 69
已采纳

内部联接的重复记录

I'm migrating a project that was pulling data from a MySQL database to a SQL database. The problem is that i'm running into duplicate records that I was able to quickly resolve when I was using a MySQL database. SQL on the other hand is giving me issues.

I have two tables: AllOrderData and CallSettings

Both tables have a column labeled OrderNumber. Sometimes there are multiple records on both tables with the same OrderNumber. I only need to display one record in these situations.

This is what i have now:

$dt = $_GET ["dt"];

$sql = "SELECT
  A.OrderNumber,
  A.DetailShipDate,
  A.ETC,
    C.OrderNumber,
    C.ETC
FROM AllOrderData A
INNER JOIN CallSettings C ON A.OrderNumber = C.OrderNumber
WHERE A.DetailShipDate = '$dt'";

I tried to add DISTINCT and also GROUP BY but I've read they're used for distinct rows and it's just not working.

When I was using MySQL, I had all the data in one table. Unfortunately, that's not an option anymore but the query was very simple:

$sql_string = "SELECT * FROM orders WHERE DetailShipDate='$dt' group by Order_Number

I tried the following:

 $sql = "SELECT x.OrderNumber,
                x.EnteredBy,
                x.Order_Number,
                x.Contact_Name
   FROM (SELECT A.OrderNumber,
                A.EnteredBy,
                C.Order_Number,
                C.Contact_Name
                row_number() OVER (PARTITION BY A.OrderNumber
                                   ORDER BY C.Order_Number) rn
                FROM AllOrderData A
                     INNER JOIN CallSettings C
                                ON A.OrderNumber = C.Order_Number
                WHERE A.DetailShipDate = '$dt') x
   WHERE x.rn = 1;";

And Got this error:

Error in query preparation/execution. Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '('. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '('. ) )

  • 写回答

1条回答 默认 最新

  • dongwenxiu5200 2018-08-27 20:38
    关注

    You can use row_number(). Partition and order by the order number.

    SELECT x.ordernumber,
           x.detailshipdate,
           x.etc,
           x.ordernumber,
           x.etc
           FROM (SELECT a.ordernumber,
                        a.detailshipdate,
                        a.etc,
                        c.ordernumber,
                        c.etc,
                        row_number() OVER (PARTITION BY c.ordernumber
                                           ORDER BY c.ordernumber) rn
                        FROM allorderdata a
                             INNER JOIN callsettings c
                                        ON a.ordernumber = c.ordernumber
                        WHERE a.detailshipdate = ?) x
           WHERE x.rn = 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)