2017-01-15 28 views
0

次のSQL ServerクエリをMySQLに変換する方法はありますか? MySQLのでMySQLの代わりにCOUNT(*)OVER(PARTITION BY mct.MasterTableid)の代わりに

COUNTため
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 

オルタナティブ(*)OVER(mct.MasterTableid BY PARTITION)

答えて

1

あなたはcountMasterTableidMasterTable内のテーブルとjoinなります。

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 
+0

を使用することができます – Lynnw