ProductId | BrandId | Views
1 | 1 | 3
2 | 1 | 2
3 | 2 | 3
4 | 2 | 4
Need write sql query to return this values:
BrandId | ViewsSummary
1 | 5
2 | 7
Please, how to do it?
It's hardly "tricky" - you're simply looking to group your results with an appropriate aggregate function:
SELECT BrandId, SUM(Views) AS ViewsSummary FROM my_table GROUP BY BrandId
See it on sqlfiddle.
Related
I have a table like this:
+--------+--------+
| userID | itemID |
+--------+--------+
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
+--------+--------+
I am trying to select all the userID's that has all the different itemID's.
Meaning, if I were to expand itemID's in the future to have itemID 5 too, the same query would select all the userID's that has itemID 3, 4 and 5.
I've struggled with this problem for several hours now, but not managed to find the general query I am looking for.. I would appreciate all the help I could get!
Here is one method:
select userid
from t
group by userid
having count(distinct itemid) = (select count(distinct t2.itemid) from t t2);
I have the following data in a Mysql table
ID | code | code order
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
2 | 1 | 1
2 | 2 | 2
2 | 3 | 3
3 | 1 | 1
3 | 4 | 2
3 | 5 | 3
4 | 1 | 1
4 | 4 | 2
4 | 5 | 3
4 | 6 | 4
How would I write a query to return the following results
code pattern 1,2,3 = 2 (count)
code pattern 1,4,5 = 1 (count)
code pattern 1,4,5,6 = 1 (count)
basically I need to find out the most popular code sequence, each sequence is grouped by an unique ID. The order the codes of the is also important. i.e
1,4,5,6 is different to 1,5,4,6
cheers
In MySQL, this is probably most easily done using two aggregation:
select pattern, count(*)
from (select id, group_concat(code order by code) as pattern
from t
group by id
) p
group by pattern;
I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo
I have 2 tables:
stock
StockID | ItemName
1 | hat
2 | hammer
3 | banana
4 | elephant
5 | book
and Basket
BasketID | StockID | Quantity
1 | 3 | 5
2 | 2 | 20
3 | 1 | 7
4 | 2 | 60
5 | 5 | 23
6 | 1 | 17
7 | 3 | 3
8 | 4 | 6
9 | 3 | 1
10 | 2 | 1
11 | 2 | 13
I'm trying to make an SQL query which out puts the StockID, ItemName, Total Quantity Sold, and the Number of Orders that Item had.
I have this:
SELECT stock.StockID, stock.ItemName, SUM( basket.Quantity ) AS QuantitySold
FROM stock
JOIN basket ON stock.StockID = basket.StockID
GROUP BY stock.Itemname
ORDER BY stock.StockID
LIMIT 0 , 30
Which works fine, but when I try adding:
COUNT (DISTINCT basket.BasketID)
I just get a message saying I have a Syntax Error.
I am fairly new to all this, so sorry if my logic is wrong, but shouldn't that just count the distinct values tied to stockID, as it does pretty much that with the SUM of quantity sold, where it locates all the basket.Quantity values tied to the stockID in the basket table.
All help much appreciated -Tom
Not sure if this is the full answer to your question, but I don't think that in MySQL you can have a space between the function name and the leading parenthesis like you do with COUNT.
I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)