Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL: Returning multiple columns from an in-line subquery
    primarykey
    data
    text
    <p>I'm creating an SQL statement that will return a month by month summary on sales.</p> <p>The summary will list some simple columns for the date, total number of sales and the total value of sales.</p> <p>However, in addition to these columns, i'd like to include 3 more that will list the months best customer by amount spent. For these columns, I need some kind of inline subquery that can return their ID, Name and the Amount they spent.</p> <p>My current effort uses an inline <code>SELECT</code> statement, however, from my knowledge on how to implement these, you can only return one column and row per in-line statement. </p> <p>To get around this with my scenario, I can of course create 3 separate in-line statements, however, besides this seeming impractical, it increases the query time more that necessary.</p> <pre><code>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 </code></pre> <p>How can i better structure this query?</p> <hr> <h2>FULL ANSWER</h2> <p>After some tweaking of Dave Barkers solution, I have a final version for anyone in the future looking for help.</p> <p>The solution by Dave Barker worked perfectly with the customer details, however, it made the simpler Total Sales and Total Sale Amount columns get some crazy figures.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload