I have a product that sells monthly and I have the user's email
I want to see the total sum purchased per month per email.
Also the total amount purchased per email for all months combined
Try Like This :
I understand question like this
select email,sum(amount),'2' as ord from (
select email,sum(amount) as amount ,month(d1) from t1 group by
month(d1),email) as t group by email
union all
select email,sum(amount) as amount,
'1' as ord from t1 group by month(d1),email
order by ord
This query return first show sum of amount per month and email after that show sum of amount per email
Related
My database table contains value in this way
IMAGE FOR TABLE DATA
I want to track down same email which has been used more than one times for a particular month and year.
In the above scenario, email that has been repeated multiple times was sandeshphuya#gmail.com, jes#gmail.com and ramu#gmail.com for different months. I want to track down customer repetition following their email for each month and year.
The query that I am using right now is
SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear, email,
COUNT(*) AS 'Count'
FROM 'tablename'
GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m') HAVING COUNT(*) > 1 ORDER BY `booked_on`;
GROUP BY email was used as it generates the repeated email and GROUP BY DATE_FORMAT(booked_on, '%Y'-%m') was used to track down total email repeated for each month/year.
This prints out data as
IMAGE FOR SELECT QUERY
How can I track down total repeated email following month and year? The expected result is
RESULT EXPECTED
You can use your query as a subquery for a new group by:
select sub.monthYear,count(*)
from
(SELECT DATE_FORMAT(booked_on, '%Y-%m') as monthYear,
email,
COUNT(*) AS 'Count'
FROM 'tablename'
GROUP BY email,DATE_FORMAT(booked_on, '%Y-%m')
HAVING COUNT(*) > 1
ORDER BY `booked_on`) as sub
GROUP BY sub.monthYear
I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID
I have been wondering on google to find solution to my problem but all I get is beyond my understanding because I am new to database.
I am using Mysql.
Problem
I have thousands of records in table out of which I have to show
month wise percentage of records of whole current year. Following is structure of order table
This is how I want to show data
I'd cross join two queries. The first would sum the number of products sold each month and the second would sum the total number of products sold that year:
SELECT `month`, `num_month` / `num_year` * 100 AS "percent"
FROM (SELECT MONTH(`dateOfOrder`) AS "month", COUNT(*) AS num_month
FROM `order`
WHERE YEAR(`dateOfOrder`) = 2018
GROUP BY MONTH(`dateOfOrder`) a
CROSS JOIN (SELECT COUNT(*) AS num_year
FROM `order`
WHERE YEAR(`dateOfOrder`) = 2018) b
I've got 2 tables, which look something like this
Device
device_id (PK)
other stuffs
Record (+-500k rows)
id (autoincrement PK)
device_id (FK)
date
direction (1 or 2)
amount
And I need queries which will be able to do following things
get sum of amounts of one device between two dates
get sum of amounts of all devices between two dates (group sum by device_id)
get sum of amounts of all devices between two dates and distinguish direction (group sum by device_id, direction)
My queries now look like this
SELECT SUM(amount) as total FROM Record WHERE device_id='anId' AND date BETWEEN min_date AND max_date
SELECT radar_id, SUM(amount) as total FROM Record WHERE date BETWEEN min_date AND max_date GROUP BY device_id
SELECT radar_id, SUM(amount) as total FROM Record WHERE date BETWEEN min_date AND max_date GROUP BY device_id, direction
the first query is completed in acceptable time (under 1s), but the other ones takes 30s+.
Is there a way to increase performance?
I have 2 tables.ms_expese and track_expense.Using this table generate a fact table
I want the expense_name in ms_expense,expense_amount from track_expense.
I want to get the sum of expense_amount for a particular expense_name based on date.The date in the order of 1,2...12 as month id
SELECT DATE_Format(a.date,'%b') as month_id,b.expense_name AS expense_type, sum(a.expense_amount) AS expense_amount FROM ms_expense b JOIN track_expense a on a.`expense_id`=b.`expense_id` group by DATE_Format(a.date,'%b')
how to put the month id in the order of 1,2,..12 and my date format y-m-d
I get the month in apr,aug and so on but i need jan as 1,feb as 2
I have 25 expenses(expense name).In this query i got the total expense amount of first expense only.I want the total expense of all expenses in every month
CREATE TABLE fact AS
(<your select query>)
Your select query can be in the following form
SELECT MONTH(date)as month_id,expense_name,sum(expense_amount)
FROM ms_expense JOIN track_expense using (expense_id)
group by expense_name,MONTH(date)