This question already has answers here:
MySQL : isn't in GROUP BY
(5 answers)
Closed 4 years ago.
I have the following table and i want to generate a view. See the result.
How can i achieve that?
I tried with group_cat but that doesnt work :(
id product_id cat_id date
1 1 1 2018-05-01
2 1 1 2018-05-02
3 1 1 2018-05-04
4 1 1 2018-05-05
5 1 1 2018-05-06
6 1 1 2018-05-07
4 1 1 2018-05-08
5 1 1 2018-05-09
6 1 1 2018-05-10
7 1 2 2018-05-01
8 1 2 2018-05-02
9 1 2 2018-05-04
10 1 2 2018-05-05
11 1 2 2018-05-06
12 1 2 2018-05-07
13 1 2 2018-05-08
14 1 2 2018-05-09
15 1 2 2018-05-10
Result:
product_id cat_id dates
1 1 2018-05-01,2018-05-02,2018-05-03,2018-05-04,etc comma seperated
1 2 2018-05-01,2018-05-02,2018-05-03,2018-05-04,etc comma seperated
Query:
select
tmp.product_id, tmp.cat_id, group_concat(tmp.date separator ',') as dates
from xxxx as tmp
group by tmp.cat_id;
Your group by should be tmp.product_id, tmp.cat_id
select
tmp.product_id, tmp.cat_id, group_concat(tmp.date separator ',') as dates
from demo as tmp
group by tmp.product_id, tmp.cat_id
Demo
Try this:
SELECT product_id, cat_id, GROUP_CONCAT(date SEPARATOR ',') dates FROM `table_name` GROUP BY product_id, cat_id
Related
I am using MySQL 5.7 and My Table is:
cp_id
cp_name
cp_version
cp_parent_id
1
playlist1
1
1
2
playlist1
2
1
3
playlist1
3
1
4
playlist2
1
4
5
playlist2
2
4
6
playlist3
1
6
7
playlist3
2
6
8
playlist3
3
6
9
playlist4
1
9
As you can see from the table that:
A single playlist can have more than one version but will have the same parent id.
Result I Require is:
I want to add a column to the result which contains whether that row is the cp version row or not.
cp_id
cp_name
cp_version
cp_parent_id
max_version
1
playlist1
1
1
0
2
playlist1
2
1
0
3
playlist1
3
1
1
4
playlist2
1
4
0
5
playlist2
2
4
1
6
playlist3
1
6
0
7
playlist3
2
6
0
8
playlist3
3
6
1
9
playlist4
1
9
1
Thanks, In Advance
On MySQL 8+ we can use MAX as an analytic function:
SELECT *, MAX(cp_version) OVER (PARTITION BY cp_parent_id) = cp_verson AS max_version
FROM yourTable
ORDER BY cp_id;
On earlier versions of MySQL we can use a join approach:
SELECT t1.*, t2.cp_version_max = t1.cp_version AS max_version
FROM yourTable t1
INNER JOIN
(
SELECT cp_parent_id, MAX(cp_version) AS cp_version_max
FROM yourTable
GROUP BY cp_parent_id
) t2
ON t2.cp_parent_id = t1.cp_parent_id
ORDER BY
t1.cp_id;
I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid
This question already has an answer here:
How to sum the duplicate values from mysql table
(1 answer)
Closed 4 years ago.
I have a MySQL table with rows containing duplicate values of Ref_Nr column. So i want to sum the values of Points with respect Ref_Nr, to u_id and r_date columns.
id u_id r_date Points Ref_Nr
1 1 2018-04-11 1 3
2 1 2018-04-11 2 3
3 2 2018-04-11 3 4
4 2 2018-04-11 4 4
5 3 2018-04-11 6 2
6 3 2018-04-11 6 2
7 1 2018-04-10 3 3
8 1 2018-04-10 5 3
9 1 2018-04-10 2 4
10 1 2018-04-10 2 4
11 2 2018-04-10 3 3
12 2 2018-04-10 5 3
13 3 2018-04-10 2 4
14 3 2018-04-10 2 4
Here is the my sql query what i tried, but i am not getting proper output
SELECT u_id, Ref_Nr ,r_date, SUM(Points) AS Points
FROM my_table ORDER BY r_date, Ref_nr,u_id;
Here is the expected output, please help me to solve this problem
u_id r_date Points Ref_Nr
1 2018-04-11 3 3
2 2018-04-11 7 4
3 2018-04-11 12 2
1 2018-04-10 8 3
1 2018-04-10 4 4
2 2018-04-10 8 3
3 2018-04-10 4 4
You need to group tha data. You can't mix normal column selects with aggregate functions like sum()
SELECT r_date, Ref_nr, u_id,
SUM(Points) AS PointSum
FROM my_table
GROUP BY r_date, Ref_nr, u_id
ORDER BY r_date, Ref_nr, u_id;
I have below customer table with max qty.
I need a unique user who has max qty order by its group id.
Input:
id, customer_id, customer_group_id qty
1 1 3 1
2 1 3 10
3 1 3 5
4 2 2 10
5 2 2 1
6 2 2 2
7 3 1 5
8 3 1 10
9 4 4 1
10 4 4 2
11 4 4 2
Output should be:
id, customer_id, customer_group_id, qty
11 4 4 2
10 4 4 2 - This should be not selected
2 1 3 10
4 2 2 10
8 3 1 10
Query:
SELECT * FROM customer
WHERE qty IN ( SELECT MAX(qty) FROM customer GROUP BY customer_id)
ORDER BY customer_group_id DESC;
I tried above query but seems not working.
I have one table named viewlist as follows
Id article_id viewdate
--------------------------------------------------
1 1 2015-07-01
2 1 2015-07-01
3 1 2015-07-01
4 2 2015-07-01
5 2 2015-07-01
6 1 2015-07-02
7 2 2015-07-02
8 1 2015-07-03
9 2 2015-07-03
10 1 2015-07-05
11 1 2015-07-05
----------------------------------------------------
i need to write a MySQL query to get count and article_id by grouping adjacent viewdate field
wanted result as follows
article_id count date_period
-----------------------------------------------------------------
1 4 2015-07-01 - 2015-07-02
2 3 2015-07-01 - 2015-07-02
1 1 2015-07-03 - 2015-07-04
2 1 2015-07-03 - 2015-07-04
1 2 2015-07-05 - 2015-07-06
Is there any simple query to get this type of result?
you may use something on these lines
SELECT v.article_id, COUNT(*), CONCAT(b.dt, ' - ', b.dt2) as 'date_period'
FROM viewlist v INNER JOIN
(SELECT a.dt, DATE_ADD(a.dt, INTERVAL 1 DAY) AS dt2 FROM (SELECT DISTINCT(viewdate) AS dt FROM viewlist WHERE (DAY(viewdate) MOD 2) = 1) a) b ON v.viewdate BETWEEN b.dt AND b.dt2
GROUP BY 1, 3
ORDER BY b.dt