0

以下查询告诉我同一天有多少销售额。还有多少新用户和积分。

这是我的工作查询(效果很好!):

SELECT Substring(purchase.`date`,1,10)                       AS `DayDate`, 
                   Sum(Cast(Substring(purchase.`item`, 12) AS UNSIGNED))     AS `Credit`,
                   Count(1)                                                  AS `Sales`,
                   (SELECT Count(*) FROM enswitch_mobile_users WHERE Date(purchase.`date`) = Substring(enswitch_mobile_users.`creation_date`,1,10))  AS `New_users`
                        FROM   (SELECT item, date 
                                    FROM enswitch_new_iphone_purchases WHERE `status`=1
                                    UNION 
                                    SELECT item, date 
                                    FROM enswitch_new_android_purchases WHERE `status`=1) AS `purchase` 
                        WHERE purchase.`date` >= :from_date AND purchase.`date` <= :to_date
                GROUP  BY `DayDate` 
                ORDER  BY `DayDate` DESC

我在这里搜索 3 个表。 enswitch_new_android_purchases enswitch_new_iphone_purchase 它们都有 item、user_id、status 和 date 列。一个条目的示例:

date: 2012-08-01 16:24:30 item: xsalnx.sip.70 user_id: 1337 status:1

还从 (id, creation_date, mobile id, ...) 中提取 mobile_users 数量enswitch_mobile_users并按特定日期分组。

如果购买的用户或新用户是测试人员,我想要做的是添加测试。如果是这样,我想在我的查询中忽略这些数据。

我将测试人员保存在名为: enswitch_testing_devices(id, name, mobile_id) 的表上。

我可以使用enswitch_mobile_users(mobile_id 列)加入数据。

到目前为止,我试图让它工作但没有运气.. 我该怎么做这个查询?

4

2 回答 2

0

试试下面 - 当你得到用户时添加一个子句 - 得到除测试之外的所有用户。现在,一个示例数据和结构表会有所帮助。(我认为您可以重新编写查询。)

SELECT Substring(purchase.`date`,1,10)                       AS `DayDate`, 
                   Sum(Cast(Substring(purchase.`item`, 12) AS UNSIGNED))     AS `Credit`,
                   Count(1)                                                  AS `Sales`,
                   (
                        SELECT Count(*) 
                        FROM enswitch_mobile_users 
                        WHERE 
                            Date(purchase.`date`) = Substring(enswitch_mobile_users.`creation_date`,1,10) 
                            AND enswitch_mobile_users.mobile_id NOT IN ( select mobile_id from enswitch_testing_devices WHERE 'is tester')
                    )  AS `New_users`
                        FROM   (SELECT item, date 
                                    FROM enswitch_new_iphone_purchases WHERE `status`=1
                                    UNION 
                                    SELECT item, date 
                                    FROM enswitch_new_android_purchases WHERE `status`=1) AS `purchase` 
                        WHERE purchase.`date` >= :from_date AND purchase.`date` <= :to_date
                GROUP  BY `DayDate` 
                ORDER  BY `DayDate` DESC
于 2013-02-04T10:36:50.207 回答
0

我设法解决了这个问题。

如果有人想知道。

function search_daily_sales($from_date, $to_date ,$show_testers)
{
        // If not showing the testers
    if ($show_testers === false) 
    {
        $sql = "SELECT Substring(purchase.`date`,1,10)                            AS `DayDate`,
                        Sum(CASE WHEN td.mobile_id IS NULL 
                            THEN Cast(Substring(purchase.`item`, 12) AS UNSIGNED) 
                            ELSE '0' END)                                         AS `Credit`,
                        Sum(CASE WHEN td.mobile_id IS NULL THEN '1' ELSE '0' END) AS `Sales`,
                        (SELECT Count(*) FROM enswitch_mobile_users 
                            WHERE 
                                Date(purchase.`date`) = Substring(enswitch_mobile_users.`creation_date`,1,10)
                            AND enswitch_mobile_users.`mobile_id` NOT IN (SELECT mobile_id FROM enswitch_testing_devices))  AS `New_users`,
                    MIN(CASE WHEN td.mobile_id IS NULL THEN '0' ELSE '1' END)    AS `tester`

                    FROM   (SELECT item, date, user_id
                                FROM enswitch_new_iphone_purchases WHERE `status`=1
                                UNION 
                                SELECT item, date, user_id
                                FROM enswitch_new_android_purchases WHERE `status`=1) AS `purchase` 

                    LEFT JOIN enswitch_mobile_users mu ON mu.id = purchase.user_id
                    LEFT JOIN enswitch_testing_devices td ON td.mobile_id = mu.mobile_id

                    WHERE purchase.`date` >= :from_date AND purchase.`date` <= :to_date
            GROUP  BY `DayDate` 
            ORDER  BY `DayDate` DESC";
    }
    else 
    {
        $sql = "SELECT Substring(purchase.`date`,1,10)                       AS `DayDate`, 
               Sum(Cast(Substring(purchase.`item`, 12) AS UNSIGNED))     AS `Credit`,
               Count(1)                                                  AS `Sales`,
               (SELECT Count(*) FROM enswitch_mobile_users WHERE Date(purchase.`date`) = Substring(enswitch_mobile_users.`creation_date`,1,10))  AS `New_users`
                    FROM   (SELECT item, date 
                                FROM enswitch_new_iphone_purchases WHERE `status`=1
                                UNION 
                                SELECT item, date 
                                FROM enswitch_new_android_purchases WHERE `status`=1) AS `purchase` 
                    WHERE purchase.`date` >= :from_date AND purchase.`date` <= :to_date
            GROUP  BY `DayDate` 
            ORDER  BY `DayDate` DESC";
    }

    $result = DB::query(Database::SELECT, $sql)->bind(':from_date', $from_date)->
                                                bind(':to_date', $to_date)->execute();

return $result;
}
于 2013-02-04T13:39:11.630 回答