MySQL:从内联子查询中返回多个列
我正在创build一个SQL语句,它将返回一个月的销售总结。
摘要将列出date的一些简单列,销售总数和销售总额。
不过,除了这些专栏之外,我还想再列出3个,以按用量计算,列出几个月的最佳客户。 对于这些列,我需要一些内联子查询,可以返回他们的ID,名称和金额。
我目前的工作是使用一个内联的SELECT
语句,但是,从我所知道的如何实现这些语句中,只能通过内联语句返回一列和一行。
为了解决这个问题,我当然可以创build3个独立的在线语句,但是,除了这个看似不切实际的操作外,它还会增加查询时间。
SELECT DATE_FORMAT(OrderDate,'%M %Y') AS OrderMonth, COUNT(OrderID) AS TotalOrders, SUM(OrderTotal) AS TotalAmount, (SELECT SUM(OrderTotal) FROM Orders WHERE DATE_FORMAT(OrderDate,'%M %Y') = OrderMonth GROUP BY OrderCustomerFK ORDER BY SUM(OrderTotal) DESC LIMIT 1) AS TotalCustomerAmount, (SELECT OrderCustomerFK FROM Orders WHERE DATE_FORMAT(OrderDate,'%M %Y') = OrderMonth GROUP BY OrderCustomerFK ORDER BY SUM(OrderTotal) DESC LIMIT 1) AS CustomerID, (SELECT CustomerName FROM Orders INNER JOIN Customers ON OrderCustomerFK = CustomerID WHERE DATE_FORMAT(OrderDate,'%M %Y') = OrderMonth GROUP BY OrderCustomerFK ORDER BY SUM(OrderTotal) DESC LIMIT 1) AS CustomerName FROM Orders GROUP BY DATE_FORMAT(OrderDate,'%m%y') ORDER BY DATE_FORMAT(OrderDate,'%y%m') DESC
我怎样才能更好地构build这个查询?
完整的答案
经过对Dave Barkers解决scheme的一些调整之后,我有一个最终版本供任何人在未来寻求帮助。
戴夫·巴克(Dave Barker)的解决scheme与客户的细节完美配合,但是它使“总销售额”和“总销售额”列变得更为简单。
SELECT Y.OrderMonth, Y.TotalOrders, Y.TotalAmount, Z.OrdCustFK, Z.CustCompany, Z.CustOrdTotal, Z.CustSalesTotal FROM (SELECT OrdDate, DATE_FORMAT(OrdDate,'%M %Y') AS OrderMonth, COUNT(OrderID) AS TotalOrders, SUM(OrdGrandTotal) AS TotalAmount FROM Orders WHERE OrdConfirmed = 1 GROUP BY DATE_FORMAT(OrdDate,'%m%y') ORDER BY DATE_FORMAT(OrdDate,'%Y%m') DESC) Y INNER JOIN (SELECT DATE_FORMAT(OrdDate,'%M %Y') AS CustMonth, OrdCustFK, CustCompany, COUNT(OrderID) AS CustOrdTotal, SUM(OrdGrandTotal) AS CustSalesTotal FROM Orders INNER JOIN CustomerDetails ON OrdCustFK = CustomerID WHERE OrdConfirmed = 1 GROUP BY DATE_FORMAT(OrdDate,'%m%y'), OrdCustFK ORDER BY SUM(OrdGrandTotal) DESC) Z ON Z.CustMonth = Y.OrderMonth GROUP BY DATE_FORMAT(OrdDate,'%Y%m') ORDER BY DATE_FORMAT(OrdDate,'%Y%m') DESC
将内联SQL移动为内连接查询。 所以你会喜欢…
SELECT DATE_FORMAT(OrderDate,'%M %Y') AS OrderMonth, COUNT(OrderID) AS TotalOrders, SUM(OrderTotal) AS TotalAmount, Z.OrderCustomerFK, Z.CustomerName, z.OrderTotal as CustomerTotal FROM Orders INNER JOIN (SELECT DATE_FORMAT(OrderDate,'%M %Y') as Mon, OrderCustomerFK, CustomerName, SUM(OrderTotal) as OrderTotal FROM Orders GROUP BY DATE_FORMAT(OrderDate,'%M %Y'), OrderCustomerFK, CustomerName ORDER BY SUM(OrderTotal) DESC LIMIT 1) Z ON Z.Mon = DATE_FORMAT(OrderDate,'%M %Y') GROUP BY DATE_FORMAT(OrderDate,'%m%y'), Z.OrderCustomerFK, Z.CustomerName ORDER BY DATE_FORMAT(OrderDate,'%y%m') DESC
你也可以做这样的事情:
SELECT a.`y`, ( SELECT @c:=NULL ) AS `temp`, ( SELECT @d:=NULL ) AS `temp`, ( SELECT CONCAT(@c:=b.`c`, @d:=b.`d`) FROM `b` ORDER BY b.`uid` LIMIT 1 ) AS `temp`, @c as c, @d as d FROM `a`
给这个镜头:
SELECT CONCAT(o.order_month, ' ', o.order_year), o.total_orders, o.total_amount, x.sum_order_total, x.ordercustomerfk, x.customername FROM (SELECT MONTH(t.orderdate) AS order_month, YEAR(t.orderdate) AS order_year COUNT(t.orderid) AS total_orders, SUM(t.ordertotal) AS total_amount FROM ORDERS t GROUP BY MONTH(t.orderdate), YEAR(t.orderdate)) o JOIN (SELECT MONTH(t.orderdate) AS ordermonth, YEAR(t.orderdate) AS orderyear SUM(t.ordertotal) 'sum_order_total', t.ordercustomerfk, c.customername FROM ORDERS t JOIN CUSTOMERS c ON c.customerid = o.ordercustomerfk GROUP BY t.ordercustomerfk, MONTH(t.orderdate), YEAR(t.orderdate)) x ON x.order_month = o.order_month AND x.order_year = o.order_year ORDER BY o.order_year DESC, o.order_month DESC