在表中获得重复百分比和计数的问题?

时间:2014-10-18 20:47:44

标签: mysql

我试图最终得到类似的东西,但逻辑却在逃避我

SELECT entity_id
     , customer_email
FROM sales_flat_order;

显示您可以想象的几个电子邮件地址,其中一些是一些独特的电子邮件地址。

我认为我可以做这样的事情来获得计数(虽然不成功)

SELECT (COUNT(DISTINCT customer_email)) AS 'All Orders'
     , (COUNT(DISTINCT customer_email) - COUNT(SELECT customer_email FROM sales_flat_order GROUP BY customer_email HAVING COUNT(*) = 1) AS 'Single Orders'
     , (COUNT(DISTINCT customer_email) - COUNT(SELECT customer_email FROM hubsales_flat_order GROUP BY customer_email HAVING COUNT(*) > 1) AS 'Repeat Orders'
FROM sales_flat_order;

即使在此工作之后,在尝试获得百分比之后也会相当复杂。我怎样才能最好地处理这个问题,并最好地修复查询?

我目前查询的错误是

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT customer_email FROM sales_flat_order GROUP BY customer_email HAVIN' at line 2

修改

我的sql查询得到了这么多,但最终出现了错误

SELECT (COUNT(DISTINCT customer_email)) AS 'All Orders'

     , ( COUNT(DISTINCT customer_email) - (
        SELECT COUNT(*) FROM(SELECT customer_email FROM hubsales_flat_order GROUP BY customer_email HAVING COUNT(*) = 1) AS 's')
       ) AS 'Single Orders'

     , ( COUNT(DISTINCT customer_email) - (
        SELECT COUNT(*) FROM(SELECT customer_email FROM hubsales_flat_order GROUP BY customer_email HAVING COUNT(*) > 1) AS 'r')
       ) AS 'Repeat Orders'

FROM hubsales_flat_order;

错误是

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''s')
   ) AS 'Single Orders'

1 个答案:

答案 0 :(得分:0)

这很纠结,但这里是

SELECT 'All Orders' AS 'Type'
       , (
            SELECT COUNT(customer_email) FROM foodhubsales_flat_order
         ) AS 'Count'

       , CONCAT(
            ROUND(
            (
                SELECT COUNT(customer_email) FROM foodhubsales_flat_order
            ) / (
                SELECT COUNT(customer_email) FROM foodhubsales_flat_order
            ) * 100, 1), '%') AS 'Percentage'

UNION ALL

SELECT 'Single Orders' AS 'Type'
       , (
            COUNT(
                DISTINCT customer_email
            ) - (
                SELECT COUNT(*) FROM(
                    SELECT customer_email FROM foodhubsales_flat_order GROUP BY customer_email HAVING COUNT(*) > 1
                ) s
            )
         ) AS 'Count'

       , CONCAT(
            ROUND(
            (

            COUNT(
                DISTINCT customer_email
            ) - (
                SELECT COUNT(*) FROM(
                    SELECT customer_email FROM foodhubsales_flat_order GROUP BY customer_email HAVING COUNT(*) > 1
                ) s
            )
         ) / (
                SELECT COUNT(customer_email) FROM foodhubsales_flat_order
         ) * 100, 1), '%') AS 'Percentage'

FROM foodhubsales_flat_order

UNION ALL

SELECT 'Repeat Orders' AS 'Type'
       , (
            COUNT(
                customer_email
            ) - (
                SELECT COUNT(*) FROM(
                    SELECT customer_email FROM foodhubsales_flat_order GROUP BY customer_email HAVING COUNT(*) = 1
                ) s
            )
         ) AS 'Count'

       , CONCAT(
            ROUND(
            (

            COUNT(
                customer_email
            ) - (
                SELECT COUNT(*) FROM(
                    SELECT customer_email FROM foodhubsales_flat_order GROUP BY customer_email HAVING COUNT(*) = 1
                ) s
            )
         ) / (
                SELECT COUNT(customer_email) FROM foodhubsales_flat_order
         ) * 100, 1), '%') AS 'Percentage'

FROM foodhubsales_flat_order;