SELECT
	saai.ShopArticleAttributeItem_Name,
	SUM(soi.ShopOrderItem_Count) AS cnt,
	SUM(soi.ShopOrderItem_Count * soi.ShopOrderItem_PriceExcl) AS total
FROM ShopArticle AS sa
INNER JOIN ShopArticleItem AS sai ON (sai.ShopArticle_ID = sa.ShopArticle_ID)
INNER JOIN ShopOrderItem AS soi ON (soi.ShopArticleItem_ID = sai.ShopArticleItem_ID)
INNER JOIN ShopOrder AS so ON (so.ShopOrder_ID = soi.ShopOrder_ID)
INNER JOIN ShopArticleAttribute AS saa ON (saa.ShopArticle_ID = sa.ShopArticle_ID)
INNER JOIN ShopArticleAttributeItem AS saai ON (saai.ShopArticleAttributeItem_ID = saa.ShopArticleAttributeItem_ID)
WHERE
	saai.ShopArticleAttributeType_ID = 1
	AND soi.ShopOrderItemState_Code NOT IN ('new', 'canceled')
	AND soi.ShopOrderItemType_Code = 'article'
	AND so.ShopOrderType_Code = 'order'
	AND so.ShopOrderState_Code != 'new'
	AND so.ShopOrderPaymentState_Code = 'payed'
GROUP BY saai.ShopArticleAttributeItem_ID
ORDER BY total DESC, cnt DESC