4

I have a database table like below. enter image description here

Here I am using group by on parent_sku column. Here is my query and its result. enter image description here

SELECT
  domain,
  parent,
  parent_name,
  parent_sku,
  SUM(qty)        AS sales,
  SUM(gross)      AS revenue,
  SUM(net)        AS net_revenue,
  SUM(refund_qty) AS returns,
  created_at
FROM report_byproducts_name
GROUP BY parent_sku
ORDER BY sales DESC
LIMIT 50

But I want to give priority for the domain name. Like if I give priority to themusthaves domain then it should give the result as domain = themusthaves and parent = 450212 with the same group by. Currently, I am always getting domain=tmhde and parent=325227

Expected Result enter image description here

Note: Please ignore created_at column

Any suggestions will be appreciated

Thanks

Here I am adding SQL Fiddle

21
  • If you want to group by multiple columns, then add multiple columns to your group by, separated by commas. Commented Apr 2, 2018 at 13:20
  • @smcjones: I don't want to group by multiple columns. I just want to set the priority of themusthaves domain at the top over other domain column Commented Apr 2, 2018 at 13:23
  • You want to give priority by sales column? Commented Apr 2, 2018 at 13:24
  • 1
    Your query is invalid because you are selecting non aggregate columns which grouping only by the parent_sku. Please fix this problem first before proceeding. Commented Apr 2, 2018 at 13:25
  • @num8er: No I want to give priority to domain column so I can get domain=themusthaves and parent=450212 as a group by result. Commented Apr 2, 2018 at 13:26

3 Answers 3

1

I think you need to aggregate a few more columns then join back to your table to get the relevant non aggregate columns something like

   select domain,
         parent,
         parent_name,parent_sku,
         sumsales,sumrevenue,sumnet_revenue,sumreturns
from report_byproducts_name
Join
(
SELECT
  max(parent) maxparent,
  parent_sku aggsparent_sku,
  SUM(sales)        AS sumsales,
  SUM(revenue)      AS sumrevenue,
  SUM(net_revenue)  AS sumnet_revenue,
  SUM(returns) AS sumreturns
FROM report_byproducts_name
GROUP BY parent_sku
) aggs on aggs.maxparent = parent and aggs.aggsparent_sku = parent_sku;

+--------------+--------+--------------------------------+------------+----------+-------------------+-------------------+------------+
| domain       | parent | parent_name                    | parent_sku | sumsales | sumrevenue        | sumnet_revenue    | sumreturns |
+--------------+--------+--------------------------------+------------+----------+-------------------+-------------------+------------+
| themusthaves | 450212 | Pailletten Damaged Jeans Grijs | 311        |      350 | 13692.89013671875 | 11360.31005859375 |          1 |
+--------------+--------+--------------------------------+------------+----------+-------------------+-------------------+------------+
1 row in set (0.00 sec)

Can I suggest you change float fields to decimal(10,2).

Sign up to request clarification or add additional context in comments.

Comments

0

You can achieve this by creating temp table. Here is what you can do:

WITH temp AS
(
SELECT
  parent_sku,
  SUM(qty)        AS sales,
  SUM(gross)      AS revenue,
  SUM(net)        AS net_revenue,
  SUM(refund_qty) AS returns,
  created_at
FROM report_byproducts_name
GROUP BY parent_sku
)
SELECT * FROM report_byproducts_name
WHERE parent_sku IN (SELECT parent_sku FROM temp) AND domain = 'themusthaves'

You can give priority in Where clause.

1 Comment

Will only work in MySQL 8.0+ because that's the only version that supports CTE.. And it's unliky the topicstarter is using MySQL 8.0 because it's not production ready jet.
0

Thanks for the support. I can get the expected results using the subquery like below. I don't know the way is correct or not but I am posting my answer here through which I can get expected results. Through I will wait for the better suggestions to get expected results.

SELECT 
  (SELECT domain FROM report_byproducts_name WHERE domain='themusthaves' AND parent_sku=rbn.parent_sku) as domain,
  (SELECT parent FROM report_byproducts_name WHERE domain='themusthaves' AND parent_sku=rbn.parent_sku) as parent,
  parent_name,
  parent_sku, 
  SUM(sales) as sales, 
  SUM(revenue) as revenue, 
  SUM(net_revenue) as net_revenue, 
  SUM(returns) as returns,
  created_at 
FROM report_byproducts_name rbn 
GROUP by parent_sku 
ORDER BY sales DESC 
LIMIT 50

Here is SQL Fiddle for the expected results

4 Comments

I would not trust the group by- If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. - dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
@P.Salmon: Thanks for the information. This query might be invalid with ONLY_FULL_GROUP_BY enabled because of the nonaggregated domain, parent etc columns. But how can I get expected result for this type of table structure? Any other way?
Did you look at my answer?
@P.Salmon: Yes I have checked answer where you have used max(parent) but I can't use it as max(parent) because parent column is the relational column which is dependent on the domain. I want data based on the current domain. so if current domain is tmhde then I want parent based on tmhde, if current domain is themusthaves then I want parent based on themustahves domain and so on. In my answer I can pass current domain as parameter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.