在MySQL中替代COUNT(*)OVER(由mct.MasterTableid分区)

时间:2017-01-15 01:36:05

标签: mysql sql sql-server

有没有更好的方法将以下SQL Server查询转换为MySQL?

SELECT mct.MasterTableid, 
       mct.MasterTablename, 
       mct.MasterTableorderid, 
       ct.CategoryTableid, 
       ct.CategoryTablename, 
       ct.CategoryTableorderid, 
       COUNT(*) OVER (PARTITION BY mct.MasterTableid) AS CoursesCount, 
       COUNT(scct.CategoryTabledetailid)          AS ChaptersCount 
FROM   MasterTable tm 
       INNER JOIN CategoryTable t 
               ON ct.MasterTableid = mct.MasterTableid 
       INNER JOIN SubCategoryTable td 
               ON ct.CategoryTableid = scct.CategoryTableid 
WHERE  ct.isdeleted = 0 
       AND mct.isdeleted = 0 
       AND scct.isdeleted = 0 
GROUP  BY mct.MasterTableid, 
          mct.MasterTablename, 
          mct.MasterTableorderid, 
          ct.CategoryTableid, 
          ct.CategoryTablename, 
          ct.CategoryTableorderid 
ORDER  BY mct.MasterTableorderid, 
          ct.CategoryTableorderid 

替代MySQL中的COUNT(*)OVER(由mct.MasterTableid分区)

1 个答案:

答案 0 :(得分:1)

您可count MasterTableidMasterTablejoin结果。

SELECT mct.MasterTableid,
       mct.MasterTablename,
       mct.MasterTableorderid,
       ct.CategoryTableid,
       ct.CategoryTablename,
       ct.CategoryTableorderid,
       mxt_cnt.CoursesCount,
       Count(scct.CategoryTabledetailid) AS ChaptersCount
FROM   MasterTable mct
       INNER JOIN CategoryTable ct
               ON ct.MasterTableid = mct.MasterTableid
       INNER JOIN SubCategoryTable scct
               ON ct.CategoryTableid = scct.CategoryTableid
       INNER JOIN (SELECT MasterTableid,
                          Count(1) AS CoursesCount
                   FROM   MasterTable
                   WHERE  isdeleted = 0
                   GROUP  BY MasterTableid) mxt_cnt
               ON mxt_cnt.MasterTableid = mct.MasterTableid
WHERE  ct.isdeleted = 0
       AND mct.isdeleted = 0
       AND scct.isdeleted = 0
GROUP  BY mct.MasterTableid,
          mxt_cnt.CoursesCount, -- Added in Group by 
          mct.MasterTablename,
          mct.MasterTableorderid,
          ct.CategoryTableid,
          ct.CategoryTablename,
          ct.CategoryTableorderid
ORDER  BY mct.MasterTableorderid,
          ct.CategoryTableorderid 

或者您可以使用Correlated sub-query

SELECT mct.MasterTableid,
       mct.MasterTablename,
       mct.MasterTableorderid,
       ct.CategoryTableid,
       ct.CategoryTablename,
       ct.CategoryTableorderid,
       mxt_cnt.CoursesCount,
       (SELECT Count(1) AS CoursesCount
        FROM   MasterTable mxt_cnt
        WHERE  mxt_cnt.MasterTableid = mct.MasterTableid
               AND mxt_cnt.isdeleted = 0) AS CoursesCount,
       Count(scct.CategoryTabledetailid)  AS ChaptersCount
FROM   MasterTable mct
       INNER JOIN CategoryTable ct
               ON ct.MasterTableid = mct.MasterTableid
       INNER JOIN SubCategoryTable scct
               ON ct.CategoryTableid = scct.CategoryTableid
WHERE  ct.isdeleted = 0
       AND mct.isdeleted = 0
       AND scct.isdeleted = 0
GROUP  BY mct.MasterTableid,
          mct.MasterTablename,
          mct.MasterTableorderid,
          ct.CategoryTableid,
          ct.CategoryTablename,
          ct.CategoryTableorderid
ORDER  BY mct.MasterTableorderid,
          ct.CategoryTableorderid