MySQL Group by Sum Total - mysql

I'm getting an error when trying to group by a sum value.
select sum(price + adjustments) as adjusted_price from items group by adjusted_price
Is this not possible?
Example: I want to grab items that have the same adjusted price. Example ( $1.00 - .50 = .50) --- ( $2.00 - 1.50 = .50), I want to grab items that have .50 grouped together.
Thanks

Maybe you are looking for the grand total adjusted_price? If so you don't need the groupby
select
sum(adjusted_price)
from
(select price+adjustments as adjusted_price from items) tt
Edit:
Sounds like you want to group by the adjusted_price. Try the following which will return the frequency count as well:
select
adjusted_price, count(1)
from
(select price+adjustments as adjusted_price from items) tt
group by adjusted_price
Or the following should get you only the unique adjusted_price values:
select distinct
adjusted_price
from
(select price+adjustments as adjusted_price from items) tt

Is this not possible?
Forget about possibility, moreover that looks wrong and incorrect. Why you are trying to group by the SUM() result BTW? You should rather group by any other column in your table. something like below but can't say for sure unless see your table schema and sample data.
select sum(price) + sum(adjustments) as adjusted_price
from items
group by some_column_name;

Related

why UNION ALL command in mysql doesn't give back any results?

I am trying to merge two queries into one, but UNION is not working for me.
Here is the code:
SELECT
Customer_A,
Activity,
Customer_P,
Purchase
FROM (
SELECT
buyer_id as Customer_A,
COUNT(buyer_id) As Activity
FROM
customer_info_mxs
GROUP BY buyer_id
UNION ALL
SELECT
buyer_id as Customer_P,
SUM(purchase_amount) As Purchase
FROM
customer_info_mxs
GROUP BY buyer_id
)sub
I expect to have 4 columns as a result, but I get 2 instead (Customer_A) and(Activity).
If the query is supposed to return a list of customers, their number of purchases, and the total amount they’ve spent, then you can use a single query like this:
SELECT mxs.buyer_id as Customer,
COUNT(mxs.purchase_id) As Activity,
SUM(mxs.purchase_amount) As Purchases
FROM customer_info_mxs mxs
GROUP BY mxs.buyer_id;
Otherwise, your first subquery will always be a buyer_id and a value of 1.
Be sure to change purchase_id to whatever the unique id is for each purchase if you wish to see that number.
I think there is some confusion about the union statement. The union statement returns a row set that is the sum of all of the 'unioned' queries; since these queries have only 2 columns, the combined output only has two columns. The fact that the columns have different names is irrelevant. The column names in the output are being applied from the first query of the union.
One option is to just do
select buyer_id, count(buyer_id), sum(purchase_amount) from customer_info_mxs group by buyer_id
From your question, it looks like you are trying to do a pivot, turning some of the rows into additional columns. That could be done with ... some difficulty.
i read your comment,
'main goal is to creat a dataset in which returns 5 columns as: Customer_A, Activity (top 100), customer_P, Purchase(top 100), inner join of activity and purchase'
please try this query
SET #row_number = 0, #row_number2 = 0;
SELECT t1.Customer_A,t1.Activity, t2.Customer_P, t2.Purchase
from (
SELECT (#row_number:=#row_number + 1) AS n, t.Customer_a, t.Activity
from (
select buyer_id as Customer_A,COUNT(buyer_id) As Activity
FROM customer_info_mxs
GROUP BY buyer_id
order by Activity desc
Limit 100
)t
) t1
left join (
SELECT (#row_number2:=#row_number2 + 1) AS n,
FROM (
select buyer_id as Customer_P, SUM(purchase_amount) Purchase
FROM customer_info_mxs
GROUP BY buyer_id
order by Purchase desc
Limit 100
)t
) t2 on t2.n=t1.n
basic idea is, i just create some temporary number 0-99 to table 1 (t1) and join to temporary number on table 2 (t2)

count sets of dates where set bigger than one

Bit of a strange question this one, I'm trying to count sets of dates - but only give one point for each set.
I dont need to count single dates e.g 03/10/2016 and 04/10/2016
Each group of dates equals one point.
data:
01/10/2016
01/10/2016 count as 1
02/10/2016
02/10/2016
02/10/2016 count as 1
03/10/2016 dont count
04/10/2016 dont count
The result im looking for would be 2 as there only 2 sets of identical dates
(01/10/2016 and 02/10/2016)
so far I have:
SELECT
COUNT(
DISTINCT (
action.`actiondate2`
)
) AS nb
FROM
ACTION
GROUP BY actiondate2
HAVING
COUNT(
DISTINCT (
action.`actiondate2`
)
) > 1
You seem to want to count dates that appear more than once. I would go with:
select count(*)
from (select a.actiondate2, count(*) as cnt
from action a
group by a.actiondate2
having count(*) > 1
) a;
EDIT:
If you want to just see the dates, use the subquery:
select a.actiondate2
from action a
group by a.actiondate2
having count(*) > 1;

How to get ONLY the Grand Total of Group Counts?

This code works great for getting both a summary of individual Group counts and a Grand Total combined, but I only want the Grand Total of individual countries with no ROLLUP:
SELECT country, count( * )
FROM mytable
GROUP BY country
WITH ROLLUP
LIMIT 0 , 300
Researching a lot of examples, I was hoping this one would finally work but no joy (can't figure out what the MySQL error is in the code):
SELECT country,COUNT(*)
FROM mytable
GROUP BY country
COMPUTE Sum(Count(*))
Thanks for any assistance!
-stucko
If you want to get the number of rows returned by a GROUP BY you can do something like this
SELECT COUNT(*) FROM (
SELECT country, count( * )
FROM mytable
GROUP BY country
) a /*derived tables need an alias*/

What is the best way to select rows with maximum value?

I have come across a task, I managed to complete the objective but the solution I got is not optimum, I need more optimum solution. I have used normal Sub Queries May be Correlated Sub Query can solve this better.
This is the table i made
SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid;
The output of this is like:-
What I want is the custid having maximum Total.
One way to do it is using Order by Total DESC LIMIT 1 but this will give only 1 result.
What I did is
SELECT custid
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c1
WHERE total = (SELECT max(Total)
FROM (SELECT custid,
count(DISTINCT bid) AS Total
FROM loan
GROUP BY custid) c2)
This gives me correct result that is
What I want to do is reduce the code, because here I am writing the same thing again. I know there must be a simpler way to do it. Maybe a correlated query.
Looking for some good answers. This is basically to clear my concepts only
Sorry, if it is noob question. I am a noob to SQL.
After understand what OP want with #Ravinder 's tip,
I guess build in mysql function GROUP_CONCAT is what you need, sql is:
select custid_count.Total, GROUP_CONCAT(custid_count.custid order by custid_count.custid asc SEPARATOR ',') as custids from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
group by custid_count.Total
order by custid_count.Total desc
limit 1;
the result column custids is the max ids concated by ',' ,after the query, you need to split custids by ',' and convert each substring to number type you need,
Here is another way:
select * from loan
where custid =
(
select custid_count.custid from
(select custid, count(distinct bid) as Total from loan group by custid order by Total desc) as custid_count
order by custid_count.Total desc
limit 1
);
First find the custid with max count, then query all rows which match the custid,
I haven't tried this in mysql, but in the sql language I'm using it is fine to use a aggregation function without a group by so something like this
select custid, total, max(total) as maxtotal
from (select custid, count(distinct bid) as total
from loan
group by custid) c1;
would tag on every line both the individual customer total and the table wide max total, and you'd just have to filter on the ones that where the total was equal to the max total. That would give you a final answer of something like this:
select custid
from (select custid, count(distinct bid) as total
from loan
group by custid) c1
where total = max(total);

How to select data where a field has a min value in MySQL?

I want to select data from a table in MySQL where a specific field has the minimum value, I've tried this:
SELECT * FROM pieces WHERE MIN(price)
Please any help?
this will give you result that has the minimum price on all records.
SELECT *
FROM pieces
WHERE price = ( SELECT MIN(price) FROM pieces )
SQLFiddle Demo
This is how I would do it, assuming I understand the question.
SELECT * FROM pieces ORDER BY price ASC LIMIT 1
If you are trying to select multiple rows where each of them may have the same minimum price, then #JohnWoo's answer should suffice.
Basically here we are just ordering the results by the price in ascending order (ASC) and taking the first row of the result.
This also works:
SELECT
pieces.*
FROM
pieces inner join (select min(price) as minprice from pieces) mn
on pieces.price = mn.minprice
(since this version doesn't have a where condition with a subquery, it could be used if you need to UPDATE the table, but if you just need to SELECT i would reccommend to use John Woo solution)
Use HAVING MIN(...)
Something like:
SELECT MIN(price) AS price, pricegroup
FROM articles_prices
WHERE articleID=10
GROUP BY pricegroup
HAVING MIN(price) > 0;
Efficient way (with any number of records):
SELECT id, name, MIN(price) FROM (select * from table order by price) as t group by id
In fact, depends what you want to get:
- Just the min value:
SELECT MIN(price) FROM pieces
A table (multiples rows) whith the min value: Is as John Woo said above.
But, if can be different rows with same min value, the best is ORDER them from another column, because after or later you will need to do it (starting from John Woo answere):
SELECT * FROM pieces
WHERE price = ( SELECT MIN(price) FROM pieces)
ORDER BY stock ASC
To improve #sberry's answer, if the column has a null value then simply doing ORDER BY would select a row with null value. Add a WHERE clause to get correct results:
SELECT * FROM pieces
WHERE price>0
ORDER BY price ASC
LIMIT 1;
Or if there is a chance of having negative values and/or VARCHAR, etc. do:
SELECT * FROM pieces
WHERE price IS NOT NULL
ORDER BY price ASC
LIMIT 1;
To make it simpler
SELECT *,MIN(price) FROM prod LIMIT 1
Put * so it will display the all record of the minimum value