my set-up is like this
Table1: company_group - company_id, company_name
Table2: store - store_id, store_name
Table3: sales - sales_id, company_id, store_id, date, sales
Table4: wh_sales - wh_sale_id, company_id, store_id, date, sales
Table5: purchase - purchase_id, company_id, store_id, date, purchase
Now I am able to get the data for the first four tables using the select and union query, but I can't make out how and which join I should use to get the data for the table5 in the same table
I am using the query for the first four tables like
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'
now how can I include the Table5 so that I can get the result like
company_name store_name month sales purchase
company-a store-c December 40000 45000
Use the following query
select `company_name, `store_name`, MONTH, sum(`sales`) as sales, sum(purchase) as purchase from (
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales` , 0 as purchase
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, 0 as purchase
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH, 0 as sales, purchase
from company_group, store,purchase
where `company_group`.`company.id`=`purchase`.`company.id`
and `store`.`store.id`=`purchase`.`store.id`) a
group by company_name,store_name, 'MONTH'
The group by clause is not needed for the inner queries since there is no group function. I have moved it to the outermost query
SELECT t1.`company_name`, t1.`store_name`,t1.`MONTH`,t1.`sales`, `purchase`.`purchase` FROM (
select `company_group`.`company_id`, `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_id`,`company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH') AS t1
LEFT JOIN `purchase`
ON t1.`company_id`=`purchase`.`company_id`
AND t1.`store_id`=`purchase`.`store_id`
Not sure if this is valid syntax specifically for MySQL, but you should get the idea anyway.
UPD: Rewritten the query fixing errors mentioned in comments
Sales vs whole & sales, displaying the purchase price for both right ?
Try this one.
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`, `purchase`.`purchase`
from company_group, store,sales, purchase
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, `purchase`.`purchase`
from company_group, store,wh_sales, purchase
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'
Adding purchase.purchase to the SELECT clause and the purchase TABLE to the FROM clause provides access to the table's purchase price field. Which, according to your projected table's purchase column is essential AND the simplest method.
Related
I'm trying to have this query show the empid and ordercount from each year. The only year being returned is 2006. The order table itself has a lot of orders ranging from 2006-2008.
select empid, year(orderdate) as orderyear, count(empid) as ordercount
from orders
group by empid
order by year(orderdate);
If you want the order count for each empid and each year, you need to group by the year too:
SELECT empid, YEAR(orderdate) AS orderyear, COUNT(empid) AS ordercount
FROM orders
GROUP BY empid, YEAR(orderdate)
ORDER BY YEAR(orderdate);
I am trying to export sales report from table 'sales'
It has field names pname, price, sold_to, pquantity, pamount, date
Here I don't wish to display the same company I sold_to over and over if the company purchased same product on 2021-02-13 so I tried to group by the query but then it isn't showing total sum of pquantity and pamount in results even though it groups same company.
could someone help me out with fixing the query?
Here is my MYSQL query:
SELECT * FROM sales
WHERE DATE_FORMAT(date, '%Y-%m-%d') BETWEEN '$from' AND '$to'
GROUP BY pname, sold_to, pquantity, pamount, date
ORDER BY sale_id DESC
I am definitely doing it wrong would appreciate if someone could help me correct the logic.
You need to use SUM() to get sum of quantity and amount
SELECT pname, sold_to, date, SUM(pquantity), SUM(pamount)
FROM sales
WHERE DATE_FORMAT(date, '%Y-%m-%d') BETWEEN '$from' AND '$to'
GROUP BY pname, sold_to, date
ORDER BY sale_id DESC
I am using sakila.payment table.
Columns: payment_id, customer_id, staff_id, rental_id, amount, payment_date, update_date
I am using this query to get customers spending the highest amount for each month.
How can I get the Nth highest spending customer for each month?
select customer_id,`month`,max(total_amount) from
(SELECT customer_id,count(customer_id) as `count`,month(payment_date) as `month`,sum(amount) as total_amount
FROM sakila.payment
group by month(payment_date),customer_id
order by `month` asc, `total_amount` desc)t
group by `month`
Try the following, if you are using MySQL 8.0 then it will work with row_number()
select
customer_id,
month,
total_amount
from
(
select
customer_id,
month,
total_amount,
row_number() over (partition by month order by total_amount desc) as rnk
from
(
select
customer_id,
count(customer_id) as `count`,
month(payment_date) as `month`,
sum(amount) as total_amount,
from sakila.payment
group by
month(payment_date),
customer_id
) cal
) mnt
where rnk = 1
Needs help with my SQL query
Query :
Select customer_id,
if (call_details ='International' , 'International Calls', 'National Calls'),
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by call_details
My Result is displaying as below
Please let me know why National Calls is not getting grouped. I wanted to find the sum of national calls and international calls
use below SQL:
select customer_id,
call_details,
sum(minutes) as minutes
from(
Select customer_id,
if (call_details ='International' , 'International Calls', 'National Calls') as call_details,
minutes
from call_minutes
where date between '$from' and '$to') x
group by customer_id,call_details
You don't need a subquery for this, because MySQL allows you to use column aliases in the group by (not all databases do). So:
Select customer_id,
(case when call_details = 'International'
then 'International Calls'
else 'National Calls'
end) as call_group,
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by customer_id, call_group;
Note: this assumes that you want separate rows for each customer. If not, remove customer_id from both the select and the group by:
Select (case when call_details = 'International'
then 'International Calls'
else 'National Calls'
end) as call_group,
sum(minutes)
from call_minutes
where date between '$from' and '$to'
group by call_group;
If you want a list of customer ids in each group, you can always add group_concat(customer_id).
Here is my query what i have tried:
SELECT DATENAME(MONTH,timeofpurchase) as 'Month',
COUNT(*) 'Transactions', SUM(totalprice) as 'Total Sales'
FROM transactions
GROUP BY DATENAME(MONTH,timeofpurchase)
ORDER BY DATENAME(MONTH,timeofpurchase) DESC
A simple workaround I can think of is to build another table that stores 1 and January, 2 and February, and so on. Then you do a join and order by the numerical column.
Check for more information: ORDER BY FIELD
This should help:
SELECT DATENAME(MONTH,timeofpurchase) as 'Month',
COUNT(*) 'Transactions', SUM(totalprice) as 'Total Sales'
FROM transactions
GROUP BY DATENAME(MONTH,timeofpurchase)
ORDER BY FIELD(MONTH,'January','February','March','April',...,'December') DESC;