I have this query:
SELECT gamer_id,COUNT(*) AS sum, SUM(amount) as amount
FROM sales_list
WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30' AND gamer_id NOT IN
(SELECT gamer_id FROM sales_list WHERE rdate < '2012-04-01' GROUP BY gamer_id)
GROUP BY gamer_id
This query printed me "2" results and I'm looking just "1".
I have 1 gamer's that deposit 2 times in April and I don't want to count the total deposits just to count the total gamer's that deposited.
any advice?
I don't really get it, but you want the numbers of deposits of a gamer only or the amount he deposited in addition?!
This should deliver number of deposits:
SELECT gamer_id, COUNT(gamer_id) AS sum, SUM(amount) as amount
FROM sales_list
WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30'
GROUP BY gamer_id
HAVING COUNT(gamer_id)>0
Edit:
SELECT DISTINCT gamer_id
FROM sales_list
WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30'
AND gamer_id NOT IN (SELECT gamer_id
FROM sales_list
WHERE rdate < '2012-04-01')
GROUP BY gamer_id
try this:
SELECT sl.gamer_id,COUNT(*) AS sum, SUM(sl.amount) as amount
FROM sales_list sl
WHERE sl.rdate BETWEEN '2012-04-01' AND '2012-04-30' AND sl.gamer_id NOT IN
(SELECT sl1.gamer_id FROM sales_list sl1 WHERE sl1.rdate < '2012-04-01' GROUP BY sl1.gamer_id)
GROUP BY sl.gamer_id
Related
I am trying to run a query to find the count and percentage of artisan based on gender. The query is running absolutely fine on the SQL server 8. But not on my live server which is 4.9.5.
The below is my query.
SELECT industry
, count(*) as cnt
, (count(*)*100.0/sum(count(*))over()) as perc
FROM `artisan_work`
GROUP BY industry
ORDER BY cnt DESC
In any database, you should be able to use:
SELECT aw.industry, count(*) as cnt,
count(*) * 100.0 / tot.cnt as perc
FROM artisan_work aw cross join
(SELECT COUNT(*) as cnt FROM artisan_work) tot
GROUP BY aw.industry
ORDER BY cnt DESC
SELECT industry
, count(*) as cnt
, (count(*)*100.0/(select count(*) from artisan_work )) as perc
FROM artisan_work a
GROUP BY industry
ORDER BY cnt DESC
I have this table in my database:
I need a query that shows total cost, monthly totalcost, monthly count(roomId), total count(roomId), grouped by room ID.
I tried this query but the result wasn't like what I need
SELECT room_id, type, numberOfBeds,
(SELECT SUM(total_cost)
WHERE date_booked BETWEEN '".$dateFrom."' and '".$dateTo."') as monthlyIncome,
SUM(total_cost) as totalIncome,
COUNT(roomId) as totalReservations,
(SELECT COUNT(roomId)
WHERE date_booked BETWEEN '".$dateFrom."' and '".$dateTo."') as monthlyReservation
FROM indvproj_rooms, indvproj_room_booking_details
WHERE indvproj_rooms.room_id=indvproj_room_booking_details.roomId;
Question is not clear. Whats is difference between monthly and total cost when you are doing SUM??
Please check below query and results if it is same you needed.
SELECT
*
FROM
(
SELECT
roomId,
SUM(total_cost) as monthly_cost,
COUNT(roomId) as monthly_count
FROM
room
group by
roomId,
YEAR(date_booked),
MONTH(date_booked)
)
as r
LEFT JOIN
(
SELECT
roomId as rid,
SUM(total_cost) as total_cost,
COUNT(roomId) as total_count
FROM
room
group by
roomId
)
as t
on r.roomId = t.rid
https://www.db-fiddle.com/f/rEu6RzQTMdFBNS4QxtaAQR/0
How to get sum of all rows in a UNION ALL query. I am trying to get sum a column of every UNION table, And Sum of result after query execution.
$select = $db->prepare("SELECT SUM(money) AS cash FROM table1 WHERE money > 0 GROUP BY id
UNION ALL
SELECT SUM(payment) AS cash FROM table2 WHERE payment > 0 GROUP BY id
UNION ALL
SELECT SUM(pay) AS cash FROM table3 WHERE pay > 0 GROUP BY id
");
$select ->execute();
for($i=0;
$rows = $select ->fetch();
$i++){
$result = $rows['sum(cash)'];
A possible solution with only mysql could be the following, which returns a single value of the summation of all three columns in the tables:
SELECT SUM(pt.psum) AS tsum FROM
(SELECT SUM(money) AS psum FROM table1 WHERE money > 0 GROUP BY id
UNION ALL
SELECT SUM(payment) AS psum FROM table2 WHERE money > 0 GROUP BY id
UNION ALL
SELECT SUM(pay) AS psum FROM table3 WHERE money > 0 GROUP BY id) pt
For clarity,
psum: partial sum
pt: partial sum table
tsum: total sum
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;
}
I have a query that selects order info between a selected time period. I want to include a where clause that limits the order info to all orders that have only 1 order total(through out all time).
Here is what I have so far:
SELECT o.orders_id, o.customers_id, o.customers_name, o.payment_method, o.date_purchased,o.orders_status, o.shipping_status, ot.value
FROM orders as o
LEFT JOIN orders_total as ot ON o.orders_id = ot.orders_id
WHERE date_purchased between '2011-07-30' AND '2011-08-30 23:59:59'
AND ot.class = 'ot_total'
AND o.customer_service_id = ''
OR o.customer_service_id IS NULL
ORDER BY orders_id DESC
This query gives me all orders in the specified time period. I need to include a subquery(or something similar) that counts all previous(through out all time) orders(order_count) BY customers_id. Then include a 'HAVING order_count < 2' in the where clause.
Is this possible? Does this make sense?
Just add this in you where close:
AND (
SELECT COUNT(o.id)
FROM orders o2
WHERE o2.customers_id = o.customers_id
) < 2
Or if you want to return the orders count, add it in your SELECT clause, and add a HAVING clause:
SELECT o.orders_id, ..., (
SELECT COUNT(o.id)
FROM orders o2
WHERE o2.customers_id = o.customers_id
) as orders_count
...
HAVING orders_count < 2