I want to write a sql query to get best product of each year from a table . I have grouped the product-id and sum the qty to get the total number of products per id.
I converted the datetime function into year to get the year but output is wrong..
Anyone can help me with this?
SELECT Year(ModifiedDate), ProductID, SUM(OrderQty) AS TotalQuantity
from Sales.SalesOrderDetail
GROUP BY ProductID, year(ModifiedDate)
having count(*) > 3000
ORDER BY SUM(OrderQty) DESC
From your comment:
query is working fine if the set having condition to 2000. but if i
set it to 3000 it return nothing. if i use this "having count() >
2000" so i get two records. first of 2013 which return total quantity
3913 and record of 2014 with total quantity 2902. but when i change
condition to this "having count() > 3000 " it return nothing. but it
should return the 2013 record
You select the sum of OrderQty, yet you filter on the count and expect it to be the same. Count is the number of records, it doesn't care about the actual values in the records.
When you filter with
having sum(OrderQty) > 3000
you will only get the 2013 record.
We can do it in two-part.
I) create a temporary table for year-wise, id wise sum of qty.
create table tempSaleOrderDetail as select year(ModifiedDate) year,sum(OrderQty) orderqty, ProductID from Sales.SalesOrderDetail group by year(ModifiedDate), productId order by 1 desc,2 desc;
II) Fetch the data.
select A.* from tempSaleOrderDetail A inner join (select year ,max(orderqty) orderqty from tempSaleOrderDetail group by year) B on A.year=B.year and A.orderqty=B.orderqty ;
If any year has more than one Id's performance is same so this query will give both results.
Related
I'm taking a SQL class and I need help with a question.
I have the following table on a phpmyadmin server
patient (id, age)
with a total of 100 patients
I'm told to find the sum of people that have same age using SQL.
I wrote this query:
SELECT COUNT(age)
FROM patient
HAVING (COUNT(age) > 1);
but it returns 100 as a result and when I did this query and found the number of patients who have the same age for each age and calculated the count manually I found 78 and I checked it manually and it's indeed 78.
SELECT COUNT(*)
FROM patient
GROUP BY age
HAVING (COUNT(*) > 1);
What's wrong with my code?
Start with a subquery that gets the counts of people with each age. Then filter this to counts more than 1 so it's just people with the same age.
Then in the main query you use SUM() to add all these counts together, to get the total number of patients.
SELECT SUM(count)
FROM (
SELECT COUNT(*) AS count
FROM patient
GROUP BY age
HAVING count > 1
) AS x
select age, count(*) from patient group by age;
So what i'm trying to do here, is that i am trying to count the number of repeat users (users who made more than one order) in a period of time, let it be month day or year, the case here is months
i'm currently running mysql mariadb and i'm pretty much a beginner in mysql, i've tried multiple subqueries but all have failed till now
This is what i have tried so far ..
This returns all the number of users with no ordering count condition
Since people are asking for sample data, here is what the data is looking like at the moment:
Order_Creation_Date - User_ID - Order_ID
2019-01-01 123 1
2019-01-01 123 2
2019-01-01 231 3
2019-01-01 231 4
This is the query i am using to get the result but it keeps on returning total number of users within the month
select month(o.created_at)month,
year(o.created_at)year,
count(distinct o.user_uuid) from orders o
group by month(o.created_at)
having count(*)>1
and this returns the number of users as 1 ..
select month(o.created_at)month,
year(o.created_at)year,
(select count(distinct ord.user_uuid) from orders ord
where ord.user_uuid = o.user_uuid
group by ord.user_uuid
having count(*)>1) from orders o
group by month(o.created_at)
Expected result will be from the sample data above
Month Count of repeat users
1 2
If you want the number of users that make more than one purchase in January, then do two levels of aggregations: one by user and month and the other by month:
select yyyy, mm, sum( num_orders > 1) as num_repeat_users
from (select year(o.created) as yyyy, month(o.created) as mm,
o.user_uuid, count(*) as num_orders
from orders o
group by yyyy, mm, o.user_uuid
) o
group by yyyy, mm;
I think you should try something like this which will return USer_ID list Month and Year wise who ordered more that once for the period-
SELECT
[user_uuid],
MONTH(o.created_at) month,
YEAR(o.created_at) year,
COUNT(o.user_uuid)
FROM orders o
GROUP BY
MONTH(o.created_at),YEAR(o.created_at)
HAVING COUNT(*) > 1;
For more, if you are looking for the count that how many users placed more that one order, you can just place the above query as a sub query and make a count on column 'user_uuid'
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'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups
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)