Sample data
+----+------------+-------+------------+
| ID | first_name | sales | sale_date |
+----+------------+-------+------------+
| 1 | Lindsey | 32.02 | 2007-03-12 |
| 2 | Nicole | 26.53 | 2007-03-12 |
| 3 | Britney | 11.25 | 2007-03-12 |
| 4 | Ashley | 18.96 | 2007-03-12 |
| 5 | Lindsey | 9.16 | 2007-03-11 |
| 6 | Nicole | 1.52 | 2007-03-11 |
| 7 | Britney | 43.21 | 2007-03-11 |
| 8 | Ashley | 8.05 | 2007-03-11 |
| 9 | Lindsey | 17.62 | 2007-03-10 |
| 10 | Nicole | 24.19 | 2007-03-10 |
| 11 | Britney | 3.40 | 2007-03-10 |
| 12 | Ashley | 15.21 | 2007-03-10 |
| 13 | Lindsey | 0.00 | 2007-03-09 |
| 14 | Nicole | 31.99 | 2007-03-09 |
+----+------------+-------+------------+
I try to find biggest total sales produce by any of this girl
I can find the biggest total sales by using this query
select first_name, sum(sales) as total
from cookie_sales
group by first_name
order by total desc limit 1;
I wonder is there a way to find same value using aggregate max function
If i using something like this
select first_name, max(sum(sales)) from cookie_sales group by first_name;
I will get 1111 Mysql error (Invalid use of group function )
Is there a way?
The query you wrote initially in your question is probably the best approach. I don't understand what would be wrong with that. No need to use subqueries here.
You can try creating a view and then try MAX on that. Something like this:
CREATE VIEW sales_sums AS
select first_name, sum(sales) as total
from cookie_sales
group by first_name
order by total desc limit 1;
SELECT first_name, MAX(total ) AS max_total
FROM sales_sums
GROUP BY first_name
You can use MAX function this way:
SELECT MAX(sum_sales)
FROM (
SELECT first_name, SUM(sales) sum_sales
FROM cookie_sales
GROUP BY first_name
) s
but then if you need to get also the first_name this query will get a little more complicated than your original one.
There is a way; you'll need to be using a MySQL version recent enough to support subqueries (should be anything 5.0 or newer):
SELECT first_name, MAX(total) FROM
(SELECT first_name, SUM(sales) AS total
FROM cookie_sales
GROUP BY first_name
ORDER BY total DESC) sum_query
GROUP BY first_name
LIMIT 0, 1;
Related
I have very little experience with relational databases and am struggling to figure out what I think is a fairly simple SQL query. I'm dealing with a table similar to below:
+----+--------+------------+------------+
| id | type | start_date | end_date |
+----+--------+------------+------------+
| 1 | type_1 | 2010-04-01 | 2011-04-01 |
| 1 | type_1 | 2011-04-02 | 2012-04-02 |
| 2 | type_1 | 2008-04-01 | 2010-04-01 |
| 2 | type_1 | 2010-04-02 | 2015-04-02 |
| 2 | type_1 | 2015-04-03 | 2016-04-03 |
+----+--------+------------+------------+
I'm trying to return the earliest start_date and latest end_date for each id in the table, so the result would look like:
+----+--------+------------+------------+
| id | type | start_date | end_date |
+----+--------+------------+------------+
| 1 | type_1 | 2010-04-01 | 2012-04-02 |
| 2 | type_1 | 2008-04-01 | 2016-04-03 |
+----+--------+------------+------------+
Again, I'm sorry if this is an easy question, but databases are not my primary responsibility. Thank you so much for your help!
you could use min() max() and group by id
select id, min(start_date), max(end_date)
from my_table
group by id
Use min and max to find the earliest and latest date within a sub query so you can select type as well.
SELECT tablename.id, type, start, end
FROM
(
SELECT id, MIN(start_date) AS start, MAX(end_date) AS end
FROM tablename
GROUP BY id
) a
INNER JOIN tablename on tablename.id = a.id
I have table named "invoices". I would like to sum the amount and grouped it by companies and date
+------------+------------------+-------------+-----------+
| company_id | company | date | amount |
+------------+--------------------------------+-----------+
| 1 | chevrolet | 2017-11-18 | 100 |
| 1 | chevrolet | 2017-11-18 | -70 |
| 1 | chevrolet | 2017-11-25 | 50 |
| 2 | mercedes | 2017-04-01 | 30 |
| 2 | mercedes | 2017-04-01 | -30 |
| 2 | mercedes | 2017-09-01 | 50 |
| 3 | toyota | 2017-05-12 | 60 |
+------------+------------------+-------------+-----------+
The desired result is:
+------------+------------------+-------------+-----------+
| company_id | model_name | date | amount |
+------------+--------------------------------+-----------+
| 1 | chevrolet | 2017-11-18 | 30 |
| 1 | chevrolet | 2017-11-25 | 50 |
| 2 | mercedes | 2017-04-01 | 0 |
| 2 | mercedes | 2017-09-01 | 50 |
| 3 | toyota | 2017-05-12 | 60 |
+------------+------------------+-------------+-----------+
How can I do it?
You already have the spec in english there, it just needs translating to SQL:
select company_id, model_name, date, sum(amount) as amount
from invoices
group by company_id, model_name, date
In MySQL you can (depending on how it's configured) get away without doing the GROUP BY line, and you might see SQLs like this on your travels through the world of MySQL:
select company_id, model_name, date, sum(amount) as amount
from invoices
MySQL is implicitly inserting the group by for you.. Personally I'd always recommend to put it in explicitly, as few other DBs do an "auto group by" and sticking to standard SQL makes your SQL knowledge more portable. You might also find strong proponents of the "group by should always be implicit" argument which, I acknowledge, has its merits :)
Together
SELECT
SUM (amount)
FROM <table-name>
GROUP BY company, date;
For grouping by company
SELECT
SUM (amount)
FROM <table-name>
GROUP BY company;
For grouping by date
SELECT
SUM (amount)
FROM <table-name>
GROUP BY date;
Use following:
SELECT company_id, model_name, date , SUM(amount) AS amount
FROM invoices GROUP BY company, date;
See here and here for more about GROUP BY clause and examples.
I have a mysql Table.
+----+----------------+-----------+---------------+---------------------+
| id | transaction_id | user_id | book_id | purchase_date |
+----+----------------+-----------+---------------+---------------------+
| 52 | bq4ren25a8w0 | ABC_00002 | xxxx951410361 | 2017-04-25 12:30:18 |
| 66 | j4ax039ilc00 | ABC_00260 | xxxx951410392 | 2017-04-25 12:30:18 |
| 67 | s2a57kp4g000 | ABC_00260 | xxxx951410361 | 2017-06-05 12:28:56 |
| 68 | h4z4h80ts000 | ABC_00260 | xxxx951410415 | 2017-06-07 12:47:24 |
| 69 | 2bt7ipsf9sy3 | ABC_00262 | xxxx951410392 | 2017-06-05 13:25:10 |
| 70 | x0rlwhm00000 | ABC_00264 | xxxx951410392 | 2017-06-09 14:23:08 |
+----+----------------+-----------+---------------+---------------------+
I need a result whic is group by book_id + purchase_date such that
+---------------+---------------------+-------+
| book_id | purchase_date | count |
+---------------+---------------------+-------+
| xxxx951410361 | 2017-04-25 12:30:18 | 10 |
| xxxx951410392 | 2017-06-05 14:27:19 | 12 |
+---------------+---------------------+-------+
I dont have much exposure in mysql, I just tried
Select Count(book_id) as book_id, purchase_date
FROM tbl_name
GROUP BY book_id, purchase_date
Its not working.
Need some help
You can use DATE to group the purchases by days.
SELECT book_id,COUNT(book_id),DATE(purchase_date) AS pdate FROM book_sales GROUP BY book_id,pdate;
If you want to know how many books were sold on a specific day, you can use:
SELECT DATE(purchase_date) AS purchase_date,COUNT(DATE(purchase_date)) AS books FROM book_sales GROUP BY DATE(purchase_date);
http://sqlfiddle.com/#!9/4cde14/6
You can try like this, hope you will get what you need.
SELECT book_id, purchase_date,COUNT(purchase_date) as count
FROM book_sales
GROUP BY purchase_date;
I have a MySql table with the following data:
| ID | House | Date |
| 1 | A | 2015-03-13 15:56:59 |
| 2 | A | 2015-03-11 12:19:45 |
| 3 | A | 2015-03-06 00:00:00 |
| 4 | B | 2015-03-13 16:07:21 |
| 5 | B | 2015-03-11 13:02:22 |
I'm trying to get the following results:
| ID | House | Date |
| 1 | A | 2015-03-13 15:56:59 |
| 4 | B | 2015-03-13 16:07:21 |
I've tried using subqueries and other types of things. Any ideas of what I could use?
Query:
select h1.id, h1.house, h1.date
from house h1 left join house h2
on h1.house = h2.house and h1.date < h2.date
where h2.date is null
This query makes no assumptions about the ordering of the data.
Demo: http://sqlfiddle.com/#!9/2da11/1
Assuming the table name is tableName
Select tbl.id, temp.house,
temp.datecol
from tableName tbl,
(
select max(tbl2.datecol),
tbl2.house
from tableName tbl2
group by tbl2.house
) temp
where tbl.house=temp.house
order by temp.datecol asc;
This can be done in a simpler way.
SELECT id, house, date FROM house WHERE date > '2015-03-13'
I need to find how many user brought a product (Note : I don't need total no of users only how many users brought).
Table name : buyer
+-----+----------+---------+
| id | products | uid |
|-----|----------|---------|
| 1 | soap | 2 |
| 2 | h_oil | 1 |
| 3 | soap | 3 |
| 4 | tea | 1 |
| 5 | soap | 2 |
| 6 | h_oil | 1 |
| 7 | soap | 3 |
| 8 | tea | 1 |
+-----+----------+---------+
I need a result like this table given below:
|-----|---------------|--------------|
| id | product_name | total_user |
|-----|---------------|--------------|
| 1 | soap | 2 |
| 2 | h_oil | 1 |
| 3 | tea | 1 |
|------------------------------------|
How can I do that in a MySQL query?
SELECT products product_name, COUNT(DISTINCT uid) total_user FROM purchases GROUP BY products;
You could use:
SELECT product_name, COUNT(*) AS total_user FROM buyer GROUP BY product_name;
Like this:
SELECT
COUNT(*)
FROM
buyer
GROUP BY
products
In short, you are looking for a group by statement
SELECT uid, product_name, count(id) as total_user FROM buyer GROUP BY uid
read more here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT
COUNT(id)
FROM
buyer
GROUP BY
products