Mysql how to Count rows and group by value? - mysql

I am trying to get this simple query to work. I like to know how many times a certain vouchercode has been used and what the total discount value is per used voucher code.
The database table has fields discount_value, discount_data. The discount_data holds the vouchercode and discount_value the sum of discount per purchase id.
SELECT discount_data,
COUNT(*)
FROM wp_wpsc_purchase_logs
GROUP BY
discount_data
seems to work to get amount of voucher code used.
But how do i get the total discount_value per used voucher code?
regards

Is the SUM() function what you are looking for?
SELECT discount_data, COUNT(*), SUM(discount_value)
FROM wp_wpsc_purchase_logs
GROUP BY discount_data

Sum() function may be
SELECT discount_data, SUM(discount_value), count(*)
FROM wp_wpsc_purchase_logs
GROUP BY discount_data

Related

Understanding simple count logic in sql

I have a very basic question which I cannot answer myself but shouldn't take much of your time.
The following query works, it lists all the exhibition_category_id and counts the total of objects that are assigned to each category.
My question is: Why does it do it? I don't understand the query. It says count(*) - why doesn't it give me the total of different exhibition_category_id's (79), but instead counts, how many objects are assigned to each category?
Here is the query in question, as well as a screen shot from the actual output:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id
https://i.stack.imgur.com/6deMv.png
Hope its understandable what I am asking for, eager to improve my post based on feedback.
Cheers
Your query is a basic aggregation query:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id;
The GROUP BY specifies that the result set will contain one row for each value of eb.exhibition_category_id. The result set consists of two columns, one is the value that defines the row. The other is a count of the number of rows in each group. That is what COUNT(*) does.
If you wanted the total count of different eb.exhibition_category_id, then you want one row and COUNT(DISTINCT):
select count(distinct eb.exhibition_category_id)
from exhibition_brand eb;
The GROUP BY function groups the COUNT() by eb.exhibition_category_id, so the query groups the records by eb.exhibition_category_id, then counts the corresponding records.

SUM of column that has been GROUPED BY

I am using the following query:
SELECT mgap_growth
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category
mgap_growth is a column with identical amounts that differ only per mgap_ska_report_category, which is the reason for the grouping. Now hat I have normalized the individual amounts per category, how can I use SUM to tally their total?
Here is a screenshot of the data:
I only need the SUM of the growth amounts per category, not of all of the mgap_growth records, but Im unsure as to how to SUM after the grouping.
Thanks!
EDIT FOR ADDITIONAL QUERY:
Let me throw another issue into the mix: we know I need to SUM only once per category, but what if I needed to GROUP BY CUSTOMER? I just found out that there are multiple customers in the data, each is duplicated per growth record, but differ by category. I really need to use two groupings, one for category to single out and SUM the growth amount and then another the single out the customer.
Here is an image describing the data:
If I understand you correctly, you need to sum the results from the subquery.
SELECT SUM(mgap_growth) AS total_mgap_growth
FROM (SELECT mgap_growth
from mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category) AS x
This should should show the total growth per category for that particular account manager:
SELECT sum(mgap_growth) AS Growth, mgap_ska_report_category as Category
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category
Rather than thinking of doing the SUM after the grouping, you can do the two together in the one statement. You were 99% of the way there with what you had already.
To answer your additional question in the comment, you can add another column to group by. The order that you list them in the group by section is the important part. The overall grouping comes first. So assuming 'Customer' is the customer column name you would do this:
SELECT mgap_ska_report_category as Category, Customer, sum(mgap_growth) AS Growth
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category, customer
WITH ROLLUP
Note that changing the SELECT columns in the top line was just for aesthetics, you can put them in any order and will get the same data, but this will be the easiest to read.
This shows the growth per customer by category for that particular account manager.
Edited again to add WITH ROLLUP. This will give you the totals per category as well. Try it with and without the WITH ROLLUP to see the how it changes things.

MySQL get totals from grouped value

I have a table that's setup like this:
**user_items**
user_item_id
user_id
item_id
User's can have the same item_id associated. If I want to see as a whole, how many items there are assigned to users, this is simply a count.
However, I'd like to the total count factoring in distinct item_id's. So grouping by the item_id, but as a total.
Any idea how I can do this?
Thank you
Select count(*) from ...
is what you are looking for
EDIT: I'm sorry.. This is indeed wrong.
There must be an easier solution, but this will do what you want:
select count() from (select count() from YOURTABLE group by YOURCOL) as WHATEVER;
This counts the rows in the resulting table of the inner query.

SQL with condition on calculated value

I have a table with products, their amount and their price. I need to select all entries where the average price per article is between a range.
My query so far:
SELECT productid,AVG(SUM(price)/SUM(amount)) AS avg
FROM stock WHERE avg>=$from AND avg<=$to GROUP BY productid
If do this, it tells me avg doesn't exist.
Also I obviously need to group by because the sum and average need to be per wine
You need to put it in the HAVING clause. You cannot filter by the result of aggregates using WHERE.
MySQL does allow you to reference column aliases in HAVING. You might need
SELECT
productid,
AVG(price/amount) AS avg ,
/*SUM(price)/SUM(amount) AS avg (<--- Or perhaps this?)*/
FROM stock
GROUP BY productid
HAVING avg>=$from AND avg<=$to
But I'm not sure exactly what you are trying to do with AVG(SUM(price)/SUM(amount)) can you show some example data?

Problems with MySQL COUNT() and joins

I need to get a count of which types of tickets have been ordered on each account, and only on accounts that have incurred a transaction. I'm doing this with the following query for each price:
SELECT tickets.order_id as order_id, count(tickets.id) as count
FROM tickets,transactions
WHERE price = $price
AND tickets.order_id = transactions.order_id
GROUP BY tickets.order_id
This gets the right set of results, but on orders where there are multiple transactions the number for count(tickets.id) is multiplied by that number. What query do I need to use to avoid this problem? Do I need to use a different kind of join?
use
count (distinct tickets.id)