mySQL question on how to add more info to a column - mysql

Got this table containing info about customers, dates and their purchases values.
Let's say some of them bought something in November but nothing in December.
Now, I am trying to have all the customers + all they spent and to use IFNULL() to the ones that have not bought anything in Dec. I am filtering it for Dec and all I get is the ones that purchased something in Dec, yeah... I know but I am sure there's a way how to do that but I just can't figure it out. Any help is more than welcome. Cheers
Customers Date Spent
John1 2000-11-01 12
John2 2000-11-02 33
John3 2000-11-03 13
John4 2000-11-04 24
John5 2000-11-05 36
John6 2000-12-01 55
John7 2000-12-02 16
John8 2000-12-04 33
John9 2000-12-03 18
John10 2000-12-03 13

You can enumerate the customers in a subquery, then bring the december records with a left join:
select c.customers, coalesce(sum(t.spent), 0) total_spent
from (select distinct customers from mytable) c
left join mytable t
on t.customers = c.customers
and t.date >= '2000-12-01'
and t.date < '2001-01-01'
group by c.customers
This gives you one row per customer, with their total spent in December 2000 - or 0 if they did not spent during that month.
In a real-life situation, you would typically have a separate table to store the customers, that you would use instead of the subquery.

Related

How to display the days when there are no records in MariaDB?

I have the following table called employees:
employee
name
101
John
102
Alexandra
103
Ruth
And the table called records:
employee
assistance
101
2022-02-01
101
2022-02-02
101
2022-02-07
Let's suppose that I want to display the employee number, name and the days of the month in which there were absences between 2022-02-01 and 2022-02-07 (taking into account that days 05 and 06 are weekends). In that case, the result would be the following:
employee
name
absence
101
John
4,5
How do I get that result?
So far I have developed a query where the days of the month in which there are attendances are displayed. Said query is as follows:
SELECT e.employee,
e.name,
r.assistance AS assistance,
OF employees and
JOIN LEFT(SELECT employee, GROUP_CONCAT(DIFFERENT EXTRACT(DAY SINCE assistance)
ORDER BY STATEMENT(DAY FROM assistance)) AS assistance FROM records
WHERE assistance BETWEEN '2022-02-01' AND '2022-02-07' GROUP BY employee) r ON e.employee = employee
WHERE (r.no_employee IS NOT NULL) ORDER BY name ASC
I would like to know how to implement the days in which there were absences and not consider the weekends. I've done several tests but I'm still stuck. I'm working with MariaDB 10.4.11
You use a recursive common table expression (requires mariadb 10.2+ or mysql 8) to get the list of dates in the date range, and join against that:
with recursive date_range as (
select '2021-12-01' dt
union all
select dt + interval 1 day from date_range where dt < '2021-12-07'
)
select employee.employee, group_concat(day(date_range.dt) order by date_range.dt) faults
from date_range
cross join employee
left join records on records.employee=employee.employee and records.assistance=date_range.dt
where weekday(date_range.dt) < 5 and records.employee is null
group by employee.employee
fiddle
If you are just looking for one employee, add that as a where condition.

With a GROUP BY select, can you sort by the projection's SUM?

I have a SELECT that uses GROUP BY X. It uses SUM in the projection. Is there any way to sort on that SUM? The group result with the highest value should be first in results table. I suspect there is no way to sort on this SUM since as each group is completed it's output and gone; there isn't any "collection" to sort. There is something totally different I have to do here. Do you have any hints?
Thank you.
(beginner)
If I understand your question right, the answer is quite simple:
SELECT customer, SUM(amount) FROM mytable GROUP BY customer ORDER BY SUM(amount);
I'm not sure you meant this but here is an example
assume that there is data in a table which shows payments people have made to a bank.
But people pay different money in different times.
*id* *payment* *time*
1 10 01 04 2016
2 20 01 02 2016
1 35 14 03 2016
3 22 21 01 2016
2 50 01 04 2016
Now you want to calculate who has paid max money.
SELECT person_id, MAX(payment) AS totalPayment
from payments
group by person_id
ORDER BY totalPayment DESC

How to pass parameters in subquery mysql?

So, I have a mysql table with user id(id) and date of transaction(dot) that looks like:
id dot
-------------------------------
101 2015-06-12 12:18:42 UTC
102 2015-06-12 12:18:40 UTC
103 2015-06-12 12:18:42 UTC
101 2015-07-12 12:18:42 UTC
and so on.
(Output for this data should be:
Year Month Num of users
-----------------------------
2015 06 0
2015 07 2
)
It logs all the transactions that are made. For each month m, I want to find out the count of users by month and year who transacted in m-1 month but not in m month. The results need to be grouped by year and month. Ideally, table should look like (http://sqlfiddle.com/#!9/b80f49/1)
Year Month Num of users
-----------------------------
2015 05 0
2015 06 2
2015 07 1
2015 08 4
Now for a single month(E.g. 05/2015), I can hardcode:
SELECT "2015" AS Year,"05" AS Month, "COUNT(DISTINCT id) FROM table WHERE
MONTH(dot)=4 AND YEAR(dot)=2015
AND id NOT IN
(SELECT id FROM table WHERE MONTH(dot)=5 AND YEAR(dot)=2015)
To group the count of users using GROUP BY, the query would look like:
SELECT YEAR(dot) as Year,MONTH(dot),COUNT(DISTINCT id) as Month FROM table
WHERE id NOT IN(SELECT id FROM table
WHERE DATEDIFF(dot_parent,dot_this_table)<30 AND DATEDIFF(dot_parent,dot_this_table)>=0)
Here dot_parent is the dot of the parent query and dot_this_table is the dot of the subquery. Now the problem here is that I can't pass the dot_parent inside the subquery. Is there a way to do that or frame the query in another way such that its logical structure remains similar, since I would have to make similar queries for multiple date ranges.
You must query the same table thrice: once for the months to show, once to find the users in the previous months, once for user matches in the months in question. You'd select distinct users per month, as you are not interested in whether a user had more than one transaction in a month or not.
Here is the complete query:
select
this_month.year,
this_month.month,
count(prev_month_users.user) - count(this_month_users.user) as users
from
(
select distinct year(timing) as year, month(timing) as month
from transactions
) this_month
left join
(
select distinct
year(timing) as year, month(timing) as month, id as user,
year(date_add(timing, interval 1 month)) as next_month_year,
month(date_add(timing, interval 1 month)) as next_month_month
from transactions
) prev_month_users
on prev_month_users.next_month_year = this_month.year
and prev_month_users.next_month_month = this_month.month
left join
(
select distinct year(timing) as year, month(timing) as month, id as user
from transactions
) this_month_users
on this_month_users.user = prev_month_users.user
and this_month_users.year = prev_month_users.next_month_year
and this_month_users.month = prev_month_users.next_month_month
group by this_month.year, this_month.month;
Result:
year month users
2015 5 0
2015 6 2
2015 7 1
2015 8 3
Note that I show three users for August (users 101, 102, 104). User 101 had two transactions in July, but it is still three users who had transactions in July but not in August.
Here is your SQL fiddle back: http://sqlfiddle.com/#!9/b80f49/13

MySQL - How to sum up first occurrences from many different products

I have a big view called: how_many_per_month
name_of_product | how_many_bought | year | month
p1 20 2012 1
p2 7 2012 1
p1 10 2012 2
p2 5 2012 2
p1 3 2012 3
p2 20 2012 3
p3 66 2012 3
How to write MySQL query in order to get only first few occurences of product p1, p2, p3 at once?
To get it one by one for first 3 months I can write:
SELECT name_of_product , sum(how_many_bought) FROM
(SELECT name_of_product, how_many_bought FROM `how_many_per_month`
WHERE name_of_product= 'p1' LIMIT 3) t
How to do it to all possible products at once so my result for taking only first month is like:
p1 20
p2 7
p3 66
For two months:
p1 30
p2 12
p3 66
The problem is that some products are published in different months and I have to make statistic how many of total of them are sold in first month, first 3 months, 6 months, 1 year divided by total.
Example using union
select
name_of_product,
sum(how_many_bought) as bought,
"first month" as period
from how_many_per_month
where month = 1
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 2 month" as period
from how_many_per_month
where month <= 2
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 6 month" as period
from how_many_per_month
where month <= 6
group by name_of_product
union
select
name_of_product,
sum(how_many_bought) as bought,
"first 12 month" as period
from how_many_per_month
where month <= 12
group by name_of_product
Demo: http://www.sqlfiddle.com/#!2/788ea/11
Results are different a little bit from your expectation. Are you sure that you write them properly? If you need to gain more speed in query time you can use group by case as I've already said.
I'm not quite sure what you're trying to achieve as the description of your question is a bit unclear. From what I've read so far, I understand you want to show the total of how many ITEM_X, ITEM_Y, ITEM_Z were sold for the past 1,3,6 months.
Based on the data you've provided, I've created this sqlfiddle that sums all results and groups them by item. This is the query:
SELECT
name_of_product,
sum(how_many_bought) as how_many_bought
FROM how_many_per_month
WHERE year = 2012
AND month BETWEEN 1 AND 3
GROUP BY name_of_product
-- NOTE: Not specifying an year will result in including all "months"
which are between the values 1 and 3 for all years. Remove it
in case you need that effect.
In the example above the database will sum all sold items between months 1 and 3 (including) for 2012. When you execute this query in your application just change the range in the BETWEEN X AND X and you'll be good to go.
Additional tip:
Avoid using sub-queries or try using the as a last resort method (in case there's simply no other way to do it). They are significantly slower than normal and even join queries. Usually sub-queries can be transformed into a join query.
SELECT
hmpm.name_of_product , SUM(hmpm.how_many_bought)
FROM (
SELECT name_of_product
FROM how_many_per_month
/* WHERE ... */
/* ORDER BY ... */
) sub
INNER JOIN how_many_per_month hmpm
ON hmpm.name_of_product = sub.name_of_product
GROUP BY hmpm.name_of_product
/* LIMIT ... */
MySQL not support LIMIT in subquery, but you need ordering and condition. And why not have id_of_product field?

Complex MySQL statement to list fishing standings?

I'm trying to make a standings chart using multiple tables. One keeps tracks of meetings attended to, each meeting counting as 5 points and the other table keeps track of results of tournaments. This is a fishing club site.
I have the following so far and can show the meeting points in order but the tournament results separate from that. I'd like to find a single albeit complex SQL statement to list out current standings.
I need to show the angler name which I can grab separate from a different table, then each month's 5 points listed along with the tournament result amount from the results table, these are all added up to finally list the total from all tournaments and meetings.
SELECT aid, sum(here*5) as total
FROM rollcall GROUP BY aid ORDER BY total DESC
SELECT aid, weight, weight-penalty as fweight
FROM `results` where tid=2 order by fweight desc
So an example is:
place angler JAN FEB MARCH ... Total Points
1 name1 5 50 5 45 0 38 143
2 name2 5 49 5 47 5 31 142
...
Is that clear at all?
What if you build your query something like this?
SELECT aid,
sum(SELECT count(1) from meetings WHERE MONTH(columndatetime) = 1 * 5) AS JAN,
sum(SELECT count(1) from meetings WHERE MONTH(columndatetime) = 2 * 5) AS FEB,
/
-- add same logic to the rest of the months
/
sum(SELECT count(1) from meetings WHERE YEAR(columndatetime) = 2013 * 5) as total
FROM rollcall GROUP BY aid ORDER BY total DESC
Where columndatetime is the name of your column that has the date and time for the meetings etc...
The last one takes all for the year.
Could this help you out?