I have a table with values like the following
Name DatePurchased QuantityPurchased
A 2/3/2012 1
A 2/4/2012 1
A 2/5/2012 2
B 2/2/2012 1
B 2/3/2012 2
I want to output the following
Name DatePurchased QuantityPurchased
A 2/3/2012 1
A 2/4/2012 2 // as I have purchased 2 upto this date
A 2/5/2012 4 // as I have purchased 4 upto this date
B 2/2/2012 1
B 2/3/2012 3
My query
SELECT Name, `DatePurchased` , SUM(QuantityPurchased)
FROM table1
GROUP BY DatePurchased
does not do the math right. I know whats wrong but can't figure out the solution.
Thanks
Try:
SELECT t1.Name, t1.DatePurchased, SUM(t2.QuantityPurchased)
FROM table1 t1
LEFT JOIN table1 t2
ON t1.Name=t2.Name AND t1.DatePurchased >= t2.DatePurchased
GROUP BY Name, t1.DatePurchased
This joins table1 to itself within Name and such that t1s date is always at least t2s date, and sums up t2s QuantityPurchased (for each name,date in t1).
(Try performing the same query with SELECT *, and without the SUM and GROUP BY to see the joined table. Then the SUM and GROUP BY will become clear).
Rewriting the answer, misread it the first time. Now I understand that you want a running total. I believe something like this should work.
Select B.Name,B.DatePurchased,SUM(B.QuantityPurchased)
FROM Table1 AS A
INNER JOIN Table1 AS B
ON A.Name=B.Name AND B.DatePurchased <= A.DatePurchased
GROUP BY B.Name,B.DatePurchased
hope it helps!
SELECT Name, `DatePurchased` , SUM(QuantityPurchased)
FROM table1
GROUP BY Name, DatePurchased
ORDER BY Name, DatePurchased
Related
I have create this query:
SELECT table1.id, b.sum
FROM table1
CROSS JOIN (SELECT SUM(id) sum
FROM table1) b
ORDER BY id DESC;
But this produces results like this:
id
sum
3
6
2
6
1
6
Sum value print only one time. Can you help me.
But I want this result :
id
sum
3
6
2
1
This should do it:
select
id,
CASE WHEN id=(max(id) over())
THEN sum(id) over (order by id) END as 'sum'
from cte1
order by id desc;
more info see: Window Function Concepts and Syntax
I have 2 tables, the first table:
And here's the 2nd table:
Desired Output:
Jan | 1,000,000
Feb | 0.000
Mar | 0.000
is it possible with single query ?
SELECT
t2.`month`,
SUM(t1.sales) as sales
FROM
table2 AS t2
LEFT JOIN table1 AS t1 ON t2.`month` = DATE_FORMAT(t1.date, "%b")
GROUP BY t2.`month`
ORDER BY sales desc
Hope this helps you.
But For this you have to make a little bit changes like in your table Your month name should be of 3 characters only , because we are joining the table name by the month name and as you can see in table there is a date added so we will convert it to the other format with 3 characters name using DATE_FORMAT(t1.date, "%b") , so it's gonna work for you
In case you dont want to use the 2nd table try this
SELECT DATE_FORMAT(t1.date, "%b") AS MONTH, SUM(t1.sales) FROM table1 AS t1 GROUP BY t1.date
SELECT s.Month, SUM(f.Sales)
FROM firstTable f, secondTable s
GROUP BY MONTH (Date)
should work for you.
i have two tables having following structure
Table A
itemId categoryId orderDate
==========================================
1 23 2016-11-08
1 23 2016-11-12
1 23 2016-11-16
Table B have the structure
categoryId stock price
==========================================
23 500 600
However mine desired output should be as like
Result C
price stock orderdate qty
600 500 2016-11-08 (first order date) 3 (3 time appearance in first table)
Here is what i have tried so far
select b.price,b.stock from B b, A a
where b.categoryId = (
select a.categoryId
from A
GROUP BY categoryId
HAVING COUNT(categoryId)>1
)
and (a.orderdate = (
select MIN(orderdate)
from A
where categoryId = b.categoryId)
)
i have following result
price stock orderdate
600 500 2016-11-08
i have no idea how do find qty as it is appeared 3 times in first table.
I think you want the records in table a grouped by item id and category id, so include these two in your group by statement. Then the other columns you have to aggregate using MIN, MAX, AVG, SUM, etc. I use MIN which will give you the smallest number in the group for that particular column, although it shouldn't matter in this case whether you use MIN or MAX or AVG - it's all the same. Then COUNT(*) will just count the number of recrods in the group.
Also, joins are generally preferred over listing tables with commas.
SELECT a.itemid, a.categoryid, MIN(b.price), MIN(b.stock), min(a.orderdate), count(*) as qty
FROM a
INNER JOIN b ON a.categoryid = b.categoryid
GROUP BY a.itemid, a.categoryid
You also need to select COUNT(*)
how about use following sql
select min(price), min(stock), min(orderDate), COUNT(categoryId)
from A,B where A.categoryId = B.categoryId
GROUP by A.categoryId
You could create views for your subqueries and give them meaningful names e.g. CategoriesUsedInMultipleOrders, MostRecentOrderByCategory. This would 'optimize' you query by abstracting away complexity and making it easier for the human reader to understand.
This is the Query with the appropriate join method see Result:
SELECT B.price, B.stock, MIN( A.orderDate ) AS orderdate, COUNT( * ) AS qty
FROM TableA A, TableB B
WHERE A.categoryId = B.CategoryId
GROUP BY A.categoryId, B.price, B.stock
I have a database with the first five columns like this:
ID NAME QUANTITY PRICE KIND
1 Dog 2 5 A
2 Cat 1 6 B
3 Dog 2 5 C
4 Bird 5 5 C
(DOG QUANTITY and PRICE will always be the same)
What I want to do to is to something like
SELECT KIND, SUM(QUANTITY * PRICE) GROUP BY KIND WHERE DISTINCT NAME
So that I get something that looks like this:
A 10
B 6
C 25
(The duplicate DOG is eliminated.)
I know my syntax above is grossly wrong -- it's just seems to be the most eloquent way of explaining what sort of thing I'm looking for.
In other words, I want to get rid of non-distinct NAMES then SUM the rest. I seem to be able to do one or the other but not both.
Any ideas? If worse comes to worst I can do it as a loop in PHP rather than as a single MYSQL query.
I'm not really clear about either what the rules are or why your table is in that format (with repeated name, quantity,price) but here is one way of getting your expected output.
select kind, SUM(quantity*price)
from
(
SELECT name, quantity, price, min(kind) kind
FROM YourTable
group by name, quantity, price
) t
group by kind
Here I chose the item with the lowest ID as the one to keep:
Select T.Kind, Sum( T.Quantity * T.Price ) As Total
From Table As T
Where Id = (
Select Min(T2.Id)
From Table As T2
Where T2.Name = T.Name
)
Group By T.Kind
Assuming that your table is unique on Name and Kind, you can do:
Select T.Kind, Sum( T.Quantity * T.Price ) As Total
From Table As T
Where T.Kind = (
Select Min(T2.Kind)
From Table As T2
Where T2.Name = T.Name
)
Group By T.Kind
I use similar queries (10) as following queries (modified) to find sum
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil', 'anil', 'gaikwad')
...and:
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil1', 'anil1', 'gaikwad1')
i want to make a single query of the above 10 queries. is it possible?
You can use UNION
SELECT 'subset1', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil', 'anil', 'gaikwad')
UNION
SELECT 'subset2', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil1', 'anil1', 'gaikwad1')
However, you probably query these sets of students for a reason, perhaps anil, salil and gaikwad are one group of students. If so, you should reflect this in the database structure, not in your code.
You could add a field 'SUbset' or 'Group' or whatever that is, to students table, so it looks like this:
name group_id
salil 1
anil 1
gaikwad 1
salil1 2
...
Then you can do
select group_id, sum(amount) from students group by group_id
Try something like this
SELECT sum(amount) AS amount
FROM students INNER JOIN
(SELECT 'salil%' Val UNION SELECT 'anil%' UNION SELECT 'gaikwad%') s ON students.NAME LIKE s.Val
WHERE sex='M'
This allows you to use the values in the second Table to join with LIKE.