I have a table
Table item kit
item_kit_id item_id
=======================
1 1
1 1
1 2
2 2
2 237
how to display result
item_kit_id item_id count
===============================
1 1 2
1 2 1
2 2 1
2 237 1
Thanks
You should use group by with both column its return your expected result
SELECT item_kit_id,item_id,count(*)
FROM kit
GROUP BY item_kit_id,item_id;
For more know about GROUP BY
you can use below query-
select item_kit_id, item_id, count(item_id) as count
from item_kit
group by item_kit_id, item_id;
Related
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
I need to count unique number of items in the order.
Please take a look at the following example:
tablel
Order item
1 111
1 111
1 123
1 341
1 341
1 861
2 999
2 831
3 731
3 731
3 721
3 777
Expected result:
Order | Item Count
-------------------
1 | 4
2 | 2
3 | 3
SELECT order, COUNT(DISTINCT item)
FROM table1
GROUP BY order
A bit or word play.
I guess you mean the number of unique items in an order.
select Order, count(item)
from table
group by order
I have following data
id category_id bookmark_id
1 1 1
2 2 1
3 3 1
4 4 2
5 5 1
and mysql query is
SELECT * from table_name group by bookmark_id
it returns
id category_id bookmark_id
1 1 1
4 4 2
which is fine
But i want one more colum extra
id category_id category_list bookmark_id
1 1 1,2,3,5 1
4 4 4 2
Any idea how to achieve above result?
select min(category_id) as min_cat,
group_concat(distinct category_id order by category_id) as category_list,
bookmark_id
from your_table
group by bookmark_id
i want to count that how many times that each pair of SKU have been ordered together(have same orderid)
I have an order table of customers that has 2 column ORDERID and SKUID
for an example
ORDERID SKUID
1 1
1 2
1 3
2 1
2 2
2 4
3 1
3 4
i want a result table as
SKU1 SKU2 COUNT
1 2 2
1 3 1
1 4 2
2 3 1
2 4 1
3 4 0
thank you
You can do this with a self-join and aggregation:
select o1.sku, o2.sku, count(distinct o1.orderid) as numorders
from orders o1 join
orders o2
on o1.orderid = o2.orderid and o1.sku < o2.sku;
If you know that the skus are unique in each order, then you can use count(*) instead of count(distinct).
I have the table like this
id rate_user_id content_id points category_id
1 100 1 5 1
2 101 1 3 1
3 100 2 8 1
4 103 2 11 1
So I want to looking highest point of content in this category 1
Content_id 2 = 19 points .
U can do this by
select content_id,sum(points) from user group by content_id order by sum(points) desc limit 1;
Thanks
have a look at the command MAX.
http://technet.microsoft.com/en-us/library/ms187751.aspx
Something like
SELECT MAX(Pts) FROM
(SELECT SUM(points) Pts GROUP BY content_id) A