I have 2 tables jornal_main and journal_item , my journal_main table has got
id | po_no | period | post_date | vendor
1 | PO123 | 12 | 2018-02-12 | XYZ
2 | PO234 | 12 | 2018-02-13 | ABC
journal_item is
id | ref_id | type | sku | qty | desc
1 | 1 | poo | A123 | 12 | Order
2 | 1 | poo | B234 | 20 | Order
3 | 2 | por | A123 | 2 | Receive
4 | 2 | por | A123 | 3 | Receive
5 | 2 | por | B234 | 6 | Receive
Desired output is
po_no | date | vendor | item | ordered_qty | received_qty | balance
po123 | 2018-02-12 | XYZ | A123 | 12 | 5 | 7
po123 | 2018-02-12 | ABC | B234 | 20 | 6 | 14
i am not getting how to combine 2 queries in a single query. Here i have 2 queries which gives me ordered_qty and received_qty
for ordered_qty
select journal_main.id, journal_main.po_no, journal_main.post_date, journal_main.vendor, journal_item. sku, SUM(journal_item.qty) AS Oqty FROM journal_main INNER JOIN journal_item ON journal_main.id=journal_item.ref_id WHERE journal_item.type='poo' GROUP BY journal_item.sku, journal_main.id
for received_qty
select journal_main.id, journal_main.po_no, journal_main.post_date, journal_main.vendor, journal_item. sku, SUM(journal_item.qty) AS Rqty FROM journal_main INNER JOIN journal_item ON journal_main.id=journal_item.ref_id WHERE journal_item.type='por' GROUP BY journal_item.sku, journal_main.id
Just use conditional aggregation:
select ji.ref_id,
sum(case when ji.desc = 'Order' then qty else 0 end) as ordered,
sum(case when ji.desc = 'Receive' then qty else 0 end) as received,
sum(case when ji.desc = 'Order' then qty else - qty end) as total
from journal_item ji
where ji.desc in ('Order', 'Receive')
group by ji.ref_id;
This just provides the aggregation columns. You should be able to join in the rest of the columns that you want.
Related
Items Table
+----+------+
| id | name |
+----+------+
| 1 | abc |
| 2 | def |
| 3 | ghi |
+----+------+
Buy Table
+------+-------------+-------+---------+
| b_id | b_date | b_qty | b_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-01 | 10 | 1 |
| 2 | 2020-05-01 | 20 | 1 |
| 3 | 2020-05-02 | 5 | 2 |
| 3 | 2020-05-03 | 10 | 3 |
+------+-------------+-------+---------+
Rent Table
+------+-------------+-------+---------+
| r_id | r_date | r_qty | r_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-03 | 5 | 2 |
| 2 | 2020-05-03 | 10 | 2 |
| 3 | 2020-05-04 | 15 | 3 |
+------+-----------+---------+---------+
Sell Table
+------+-------------+-------+---------+
| s_id | s_date | s_qty | s_itmid |
+------+-------------+-------+---------+
| 1 | 2020-05-03 | 10 | 1 |
| 2 | 2020-05-05 | 20 | 3 |
| 3 | 2020-05-06 | 5 | 3 |
+------+-----------+---------+---------+
And I'm trying to get outputs with php foreach something like this ...
$trans_date
$buy_qty
$rent_qty
$sell_qty
$item
In case item id 1
+-------------+--------------+---------------+---------------+------+
| trans_date | buy_qty | rent_qty | sell_qty | item |
+-------------+--------------+---------------+---------------+------+
| 2020-05-01 | 30 | 0 | 0 | abc |
| 2020-05-02 | 0 | 0 | 0 | abc |
| 2020-05-03 | 0 | 0 | 10 | abc |
| 2020-05-04 | 0 | 0 | 0 | abc |
| 2020-05-05 | 0 | 0 | 0 | abc |
| 2020-05-06 | 0 | 0 | 0 | abc |
+-------------+--------------+---------------+---------------+------+
This is the query I've come for one table (b_date column has timestamp value)...
$query = $this->db->query("
SELECT FROM_UNIXTIME(b_date,'%d %M') AS date_b
, SUM(b_qty) AS qty_b
FROM buytable
WHERE b_itmid = 1
AND MONTH(FROM_UNIXTIME(b_date)) = MONTH(CURDATE())
GROUP
BY DATE(FROM_UNIXTIME(b_date))
");
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$result[] = $data;
}
return $result;
}
Finally I've found the correct answer https://stackoverflow.com/a/61875506/7554875
select d.date, b.qty_buy, r.qty_rent, s.qty_sell, i.name
from items i
cross join (
select b_date as date from buy
union all select r_date as date from rent
union all select s_date as date from sell
) d
left join (select b_date as date, b_itmid, sum(b_qty) qty_buy
from buy group by date, b_itmid) b
on b.date = d.date and b.b_itmid = i.id
left join (select r_date as date, r_itmid, sum(r_qty) qty_rent
from rent group by date, r_itmid) r
on r.date = d.date and r.r_itmid = i.id
left join (select s_date as date, s_itmid, sum(s_qty) qty_sell
from sell group by date, s_itmid) s
on s.date = d.date and s.b_itmid = i.id
where i.id = 1 and d.date >= date_format(curent_date, '%Y-%m-01')
order by d.date
I have three tables, products, customers, order
Product:
id | name |
1 | milk |
2 | bread|
3 | Pea |
Customer:
id | name | category
1 | James | retailer
2 | Paul | vendor
3 | Dave | retailer
Order:
id | product_id | customer_id | qty | price
1 | 1 | 2 | 23 | 50
2 | 2 | 2 | 4 | 30
3 | 3 | 2 | 6 | 10
4 | 2 | 1 | 9 | 30
5 | 3 | 1 | 2 | 10
6 | 1 | 3 | 6 | 50
7 | 3 | 3 | 7 | 10
When i do a query to show transactions by customers with category of vendor like
SELECT customer.name, product.name as pname, order.qty, order.price FROM customer, product, order
WHERE customer.id = order.customer_id
AND product.id = order.product_id AND customer.category = "vendor"
i will get something like:
name | pname | qty | price
Paul | milk | 23 | 50
Paul | bread | 4 | 30
Paul | pea | 6 | 10
I want this instead:
name | milk | bread | pea | total
Paul | 23 | 4 | 6 | 90
While that of retailers will look like this:
SELECT customer.name, product.name as pname, order.qty, order.price FROM
customer, product, order
WHERE customer.id = order.customer_id
AND product.id = order.product_id AND customer.category = "retailer"
I will get a table like this:
name | pname | qty | price
James | bread | 9 | 30
James | pea | 2 | 10
Dave | milk | 6 | 50
Dave | pea | 7 | 10
But i want this instead:
name | milk | bread | pea | total
James | 0 | 9 | 2 | 40
Dave | 6 | 0 | 7 | 60
Simply use conditional aggregation for pivoting columns. And be sure to use explicit joins instead of the deprecated implicit join as former has been the standard for 25 years in ANSI-92.
SELECT c.name,
SUM(CASE WHEN p.name = 'milk' THEN o.qty ELSE 0 END) as milk,
SUM(CASE WHEN p.name = 'bread' THEN o.qty ELSE 0 END) as bread,
SUM(CASE WHEN p.name = 'pea' THEN o.qty ELSE 0 END) as pea,
SUM(o.price) AS Total
FROM `customer` c
INNER JOIN `order` o
ON c.id = o.customer_id
INNER JOIN `product` p
ON p.id = o.product_id
WHERE c.category = 'vendor' -- same for retailer
GROUP BY c.name
I think that you cannot have this response structure directly from one simple select
name | milk | bread | pea | total
James | 0 | 9 | 2 | 40
Dave | 6 | 0 | 7 | 60
because your database is getting one row foreach retailers/customer order.
I know that using a server language like PHP or Java you will can handle the data and retrive like you want.
I need help to resolve this.
I have 4 tables :
Transactions
| id | cid | gt | rt |
| 1 | 6 | 2 | 5 |
| 2 | 6 | 9 | 7.5 |
| 3 | 6 | 3 | 9.7 |
| 4 | 3 | 3 | 7.0 |
| 5 | 3 | 7 | 6.8 |
| 6 | 9 | 4 | 2.5 |
| 7 | 9 | 2 | 5.4 |
Clients
| id | firstname | lastname | date |
| 1 | jean | moulin | 1987 |
| 2 | salah | fera | 1968
| 3 | marouan | youra | 2001 |
| 4 | amin | esa | 1963 |
| 5 | kamal | tara | 1789 |
| 6 | moad | mara | 2005 |
| 9 | safaa | dara | 2004 |
Produit A
| id | cid |
| 1 | 6 |
| 2 | 6 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 1 |
Produit B
| id | cid |
| 1 | 6 |
| 2 | 3 |
| 3 | 9 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 6 |
The result that i need is :
cid | name | date | pa | pb | gt | rt |
3 | | | | | | |
6 | | | | | | |
9 | | | | | | |
I need to select from transaction all distinct client id (pid) and select the firstname and last name (name = firstname lastname) and date from clients table and sum all values (gt) and (rt) and search in table produitA the number of products for this client by his id and the same thing for the table produitB.
What i do for this but it don't work is (suggested by Gimeniux):
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(gt) AS gt,
sum(rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
The probleme here is that gt and rt values are true for only the first client. For the second client and third and ... there is different values that are not true.
Although is hard for me to see the logic between your tables, you can use this query to get the result you desire. But i think it won't work if there are two same 'gt' or two same 'rt' values for one client.
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(distinct gt) AS gt,
sum(distinct rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
Row for pid=9 doesn't show because in the data you gave there is no client with id=9
i have this table called bag:
+--------+----------+---------+----------+
| bag_id | chara_id | item_id | item_qty |
+--------+----------+---------+----------+
| 1 | 1 | 2 | 22 |
| 2 | 1 | 1 | 55 |
| 3 | 3 | 1 | 2 |
| 6 | 3 | 4 | 2 |
| 7 | 4 | 4 | 2 |
| 8 | 5 | 4 | 2 |
| 9 | 6 | 4 | 2 |
| 10 | 1 | 5 | 1 |
| 11 | 1 | 2 | 1 |
| 12 | 1 | 2 | 1 |
| 13 | 1 | 2 | 1 |
| 14 | 1 | 8 | 1 |
| 15 | 1 | 6 | 1 |
| 16 | 1 | 8 | 1 |
| 17 | 1 | 6 | 1 |
+--------+----------+---------+----------+
the relationship goes as 1 chara = many item
now i dont want 1 chara = many duplicated item.
how can i make a query that delete's the duplicated values?
like chara_id: 1 has 3 duplicated item_id: 2
i want to delete the other 2.
Not the best way to do it. But the below should definetly work:
Delete from Bag
where bag_id
not in (
select min(bag_id) from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) > 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
UNION
select bag_id from bag a,
(select chara_id, item_id
from bag group by chara_id, item_id
having count(*) = 1) b
where a.chara_id = b.chara_id and a.item_id = b.item_id
)
You can simply join table bag with a subquery which gets the minimum bag_id for every combination of chara_ID and item_ID. Records that have null values on any fields on the subquery are the records that will be deleted.
DELETE a
FROM bag a
LEFT JOIN
(
SELECT chara_ID, item_ID, MIN(bag_ID) min_ID
FROM bag
GROUP BY chara_ID, item_ID
) b ON a.bag_ID = b.min_ID AND
a.chara_ID = b.chara_ID AND
a.item_ID = b.item_ID
WHERE b.min_ID IS NULL
SQLFiddle Demo
I want to find the three most sold products every day and
show them together with number of sales.
However if there are more than one product sharing the same
number of sales I just want to tell how many products got this
ranking.
I have two tables
Products:
+-----+---------+
| Pid | Product |
+-----+---------+
| 1 | Moon |
| 2 | Sun |
| 3 | Venus |
| 4 | Mars |
+-----+---------+
SalesRows:
+-----+---------+------------+
| Pid | No_sold | Sales_date |
+-----+---------+------------+
| 1 | 1 | 2013-01-01 |
| 2 | 5 | 2013-01-01 |
| 3 | 2 | 2013-01-01 |
| 2 | 2 | 2013-01-01 |
+-----+---------+------------+
Should give:
+------+--------------+-------+
| Rank | Product | Sales |
+------+--------------+-------+
| 1 | Sun | 7 |
| 2 | Venus | 2 |
| 3 | Moon | 1 |
+------+--------------+-------+
However this sales data:
SalesRows:
+-----+---------+------------+
| Pid | No_sold | Sales_date |
+-----+---------+------------+
| 1 | 1 | 2013-01-01 |
| 2 | 5 | 2013-01-01 |
| 3 | 2 | 2013-01-01 |
| 2 | 2 | 2013-01-01 |
| 4 | 1 | 2013-01-01 |
+-----+---------+------------+
Should give:
+------+--------------+-------+
| Rank | Product | Sales |
+------+--------------+-------+
| 1 | Sun | 7 |
| 2 | Venus | 2 |
| 3 | *2 products* | 1 |
+------+--------------+-------+
Any suggestions how to solve this last part?
This query may help you.
SELECT #rownum := #rownum + 1 rownum,
t.*
FROM (SELECT #rownum:=0) r,
(select case when indicator = 1 then Product
else concat( indicator, ' Products') end as Product, sales from (Select *, count(sales) as indicator from (SELECT Product,SUM(No_sold) AS sales FROM SalesRows
JOIN Products ON Products.Pid = SalesRows.Pid
WHERE Sales_date = curdate()
GROUP BY SalesRows.Pid ) a group by sales Order by sales desc) a) t
Try this :
SELECT Product,SUM(No_sold) AS sales FROM SalesRows
LEFT JOIN Products ON Products.Pid = SalesRows.Pid
WHERE Sales_date = '".$today."'
GROUP BY Pid