This following query is telling me how many sales was at the same day. also how many new users, and credits.
This is my working query (it works great!):
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
I am searching here in 3 tables. enswitch_new_android_purchases
enswitch_new_iphone_purchase
they both has item, user_id, status and date columns. example for one entry:
date
: 2012-08-01 16:24:30
item
: xsalnx.sip.70
user_id
: 1337
status
: 1
Also pulling the mobile_users amount from enswitch_mobile_users
(id, creation_date, mobile id, ...) and grouping with specific day date.
What I am trying to do is to add a test if the user who bought, or the new user is a tester. if so I want to ignore this data on my query.
I'm saving the testers on a table called: enswitch_testing_devices
(id, name, mobile_id).
And I can join the data with enswitch_mobile_users
(mobile_id column).
So far I tried to make it work but had no luck.. How can I do this query ?
Try below - just added a clause when you get the users - get all the users except test. Now a sample data and structure table would help. (I think you can re-write the query.)
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
I manage to SOLVE this issue.
If any one wondered.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.