make sum of one column and join it with another table - mysql

I have two tables and I want one query that gets sum of table 2 (addonAmount) column and combine it with table 1
table 1: subscriptions
+-------+--------+-------------+
| subId | userId | subDuration |
+-------+--------+-------------+
| 80 | 4607 | 6 |
| 81 | 4607 | 12 |
| 82 | 4608 | 18 |
+-------+--------+-------------+
table 2: subscriptionAddons
+---------+-------+-------------+
| addonId | subId | addonAmount |
+---------+-------+-------------+
| 15 | 80 | 4 |
| 16 | 80 | 2 |
+---------+-------+-------------+
Query I used:
SELECT subscriptions.*, subscriptionAddons.addonAmount
FROM subscriptions LEFT JOIN subscriptionAddons
ON subscriptions.subId = subscriptionAddons.subId;
what I want
+-------+--------+-------------+------------+
| subId | userId | subDuration |addonAmount |
+-------+--------+-------------+------------+
| 80 | 4607 | 6 |6 |
| 81 | 4607 | 12 |NULL |
| 82 | 4608 | 18 |NULL |
+-------+--------+-------------+------------+

You need to group by the columns that define a record and sum the column you want as result.
SELECT s.subId, s.userId, s.subDuration, sum(sa.addonAmount) as addonAmount
FROM subscriptions s LEFT JOIN subscriptionAddons sa
ON s.subId = sa.subId
GROUP BY s.subId, s.userId, s.subDuration;

Related

GROUP sum BY two tables by joining

I'm trying to write a SQL query that will correctly group sales items sold_qyt and sub-total-price together as per product's category so I can show this on the printable invoice that product from Jelly Sheet = 4 at a rate of 62 subtotal for this category product is 248(4 * 62 = 248). but when I try to run the below-mentioned query it shows out-put as 12 but I want subtotal and sold_qyt segregated base on category.
I have tried to run different queries just one query gives the output which is mentioned below and this is for just the sum of all sold_qyt. DB example is also shown below
DB Example: (For better understanding)
Table # 1:
Category
ID | code | name
1 | 1 | jelly sheet
2 | 2 | 9D Glass
3 | 3 | Polished Glass
Table # 2:
Product:
ID | code | name | cost | category_id | price
1 | 1 | IP11JS | 50 | 1 | 62
2 | 2 | IP12JS | 50 | 1 | 62
3 | 3 | IP119D | 40 | 2 | 55
4 | 4 | IP129D | 40 | 2 | 55
5 | 5 | IP11PG | 18 | 3 | 25
6 | 6 | IP12PG | 18 | 3 | 25
Table # 3:
sale_items:
ID | sale_id | product_id | product_code | product_name | unit_price | sold_qyt | subtotal |
1 | 1 | 1 | 1 | IP11JS | 62 | 2 | 124 |
2 | 1 | 2 | 2 | IP12JS | 62 | 2 | 124 |
3 | 1 | 3 | 3 | IP119D | 55 | 2 | 110 |
4 | 1 | 4 | 4 | IP129D | 55 | 2 | 110 |
5 | 1 | 5 | 5 | IP11PG | 25 | 2 | 50 |
6 | 1 | 6 | 6 | IP12PG | 25 | 2 | 50 |
7 | 2 | 7 | 1 | IP11JS | 62 | 2 | 124 |
8 | 2 | 8 | 2 | IP12JS | 62 | 2 | 124 |
9 | 2 | 9 | 3 | IP119D | 55 | 2 | 110 |
10 | 2 | 10 | 4 | IP129D | 55 | 2 | 110 |
11 | 2 | 11 | 5 | IP11PG | 25 | 2 | 50 |
12 | 2 | 12 | 6 | IP12PG | 25 | 2 | 50 |
SQL Query which is run by me:
SELECT sale_id,
SUM(sold_qyt) AS sold_qyt
FROM sale_items
GROUP BY sale_id
kindly help me with this difficulty thanks in advance
Update: 1-21-2021
i execute new query
SELECT (sma_sale_items.sale_id, sma_categories.code AS sma_products.category_id, sma_products.code AS sma_sale_items.product_code,)
SUM(sold_qyt) AS sold_qyt
SUM(subtotal) AS subtotal
FROM sma_sale_items
LEFT JOIN sma_products ON sma_products.id=sma_sale_items.product_id
LEFT JOIN sma_categories ON sma_categories.code=sma_products.category_id
GROUP BY sma_sale_items.sale_id
ORDER BY sma_categories
but no luck :(
I want the output like this:
Expected OUT PUT:
ID | sale_id | category_name | sold_qyt | subtotal |
1 | 1 | Jelly Sheet | 4 | 248 |
2 | 1 | 9D Glass | 4 | 220 |
3 | 1 | Polished Glass | 4 | 100 |
4 | 2 | Jelly Sheet | 4 | 248 |
5 | 2 | 9D Glass | 4 | 220 |
6 | 2 | Polished Glass | 4 | 100 |
The ID column in your expected result set is very misleading - it appears to be just new ID value for the output result set rather than any of the ID values from the source tables.
If it is important for you then you can use this query:
SELECT ROW_NUMBER() OVER (ORDER BY sale_id, category_id),
sale_id,
category_name,
sold_qty,
subtotal
FROM (
SELECT c.ID as category_id,
si.sale_id,
c.[name] as category_name,
SUM(si.sold_qty) as sold_qty,
SUM(si.subtotal) as subtotal
FROM sale_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY c.ID,
si.sale_id,
c.[name]
) r
If it is not relevant and you only want the sale_id, category_name and the totals then simplify it to:
SELECT si.sale_id,
c.[name] as category_name,
SUM(si.sold_qty) as sold_qty,
SUM(si.subtotal) as subtotal
FROM sale_items si
JOIN product p ON p.ID = si.product_code
JOIN category c ON c.ID = p.category_id
GROUP BY si.sale_id,
c.[name]
ORDER BY sale_id, category_name

MySQL: I have two tables, 1 production and 1 archive. I need to select distinct records from both as some records are identical

I have a MySQL database containing 2 tables. prod is an in-production and archive has archived records. The columns are exactly the same. prod does not have all records in archive.
I need to access distinct records in 1 join of the two tables so I'm not duplicating records and throwing calculations off in excel.
SELECT
a.order_no,
a.date_of_sale,
a.cost,
b.order_no,
b.date_of_sale,
b.cost
FROM
`prod` AS a
LEFT JOIN `archive` AS b ON a.order_no = b.order_no
Results:
+----------+--------------+------+-------------+-----------------+---------+
| order_no | date_of_sale | cost | order_no(1) | date_of_sale(2) | cost(2) |
+----------+--------------+------+-------------+-----------------+---------+
| 333 | 11-28-2017 | 10 | 333 | 11-28-2017 | 10 |
| 334 | 11-28-2017 | 12 | 334 | 11-28-2017 | 12 |
| 336 | 11-29-2017 | 30 | 335 | 11-28-2017 | 25 |
| 337 | 11-30-2017 | 15 | | | |
| 338 | 11-30-2017 | 17 | 338 | 11-28-2017 | 17 |
+----------+--------------+------+-------------+-----------------+---------+
How can I structure my query to filter records that are the same in both tables?
The result I'd like:
+----------+--------------+------+
| order_no | date_of_sale | cost |
+----------+--------------+------+
| 333 | 11-28-2017 | 10 |
| 334 | 11-28-2017 | 12 |
| 335 | 11-28-2017 | 25 |
| 336 | 11-29-2017 | 30 |
| 337 | 11-30-2017 | 15 |
| 338 | 11-30-2017 | 17 |
+----------+--------------+------+```
I suspect that you want union rather than a join:
select order_no, date_of_sale, cost from prod
union
select order_no, date_of_sale, cost from archive
union removes duplicates between the resultsets (and within the resultsets as well).

How to update a column with the number of rows that have a matching column pair?

I have a table called related_clues which lists the id's of pairs of clues which are related
| id | clue_id | related_clue_id | relatedness |
+----+---------+-----------------+-------------+
| 1 | 1 | 232 | 1 |
| 2 | 1 | 306 | 1 |
| 3 | 1 | 458 | 1 |
| 4 | 2 | 620 | 1 |
| 5 | 2 | 72 | 1 |
| 6 | 3 | 212 | 1 |
| 7 | 3 | 232 | 1 |
| 8 | 3 | 412 | 1 |
| 9 | 3 | 300 | 1 |
+----+---------+-----------------+-------------+
Eventually after a while we may reach two id's such as:
+--------+---------+-----------------+-------------+
| id | clue_id | related_clue_id | relatedness |
+--------+---------+-----------------+-------------+
| 121267 | 1636 | 38 | 1 |
| 121331 | 1636 | 38 | 1 |
+--------+---------+-----------------+-------------+
So in this case, for two distinct id values, we have the same (clue_id, related_clue_id) pair
In this case I would like the relatedness value to be updated to 2, signalling that there are two examples of this (clue_id, related_clue_id) pair. Like so:
+--------+---------+-----------------+-------------+
| id | clue_id | related_clue_id | relatedness |
+--------+---------+-----------------+-------------+
| 121267 | 1636 | 38 | 2 |
| 121331 | 1636 | 38 | 2 |
+--------+---------+-----------------+-------------+
So essentially I would like to run some SQL that sets the relatedness value to the number of times a (clue_id, related_clue_id) pair appears.
When I have no relatedness column present, and I simply run the SQL:
SELECT id, clue_id, related_clue_id, COUNT(*) AS relatedness
FROM `related_clues`
GROUP BY clue_id, related_clue_id
It gives me the required result, but of course this doesn't store the relatedness column, it simply shows the column if I run this select. So how do I permanently have this relatedness column?
You could use a update with join
Update related_clues a
INNER JOIN (
SELECT clue_id, related_clue_id, COUNT(*) AS relatedness
FROM `related_clues`
group by clue_id, related_clue_id
having count(*) = 2
) t on t.clue_id = a.clue_id
and t.related_clue_id = a.related_clue_id
set a.relatedness = t.relatedness
I would approach this as an update/join but filter out rows that don't need to be updated:
update related_clues rc join
(select clue_id, related_clue_id, COUNT(*) AS cnt
from `related_clues`
group by clue_id, related_clue_id
) t
on t.clue_id = rc.clue_id and
t.related_clue_id = rc.related_clue_id
set rc.relatedness = t.relatedness
where rc.relatedness <> t.relatedness;

Query is selecting count from joined table

I have a shipments table, and a shipments detail table. A shipment generally ships multiple cartons. I am trying to select a count of the shipment table and sum the quantity from the detail table. But my values are being selected from the joined table.
ex. count = 7, when count should be 4 from my shipment table
SELECT ss.tenant_id,
ss.order_id,
COUNT(ss.shipment_number),
SUM(sd.qty_shipped)
FROM shipment ss
LEFT JOIN detail sd
ON ss.id = sd.shipment_id
GROUP BY
ss.order_id,
ss.tenant_id;
output -->
tenant_id | order_id | count | sum
-----------+----------+-------+------
1 | 2573 | 7 | 1350
Data set -->
shipment
id | shipment_number | shipment_status | tracking_number | shipping_cost
------+-----------------+----------------+----------------+---------------
8332 | 1000048 | confirmed | 123 | 10.00
8333 | 1000049 | confirmed | 123 | 10.00
8334 | 1000050 | confirmed | 123 | 10.00
8335 | 1000051 | confirmed | 123 | 10.00
detail
id | carton_number | qty_shipped | order_id | shipment_id
-------+---------------+-------------+----------+------------
14654 | 1 | 200 | 2573 | 8332
14655 | 2 | 200 | 2573 | 8332
14656 | 1 | 200 | 2573 | 8333
14657 | 1 | 200 | 2573 | 8334
14658 | 2 | 200 | 2573 | 8334
14659 | 1 | 150 | 2573 | 8335
14660 | 2 | 200 | 2573 | 8335
I had to add distinct in my count.
select ss.tenant_id,
ss.order_id,
count(distinct ss.shipment_number),
sum(sd.qty_shipped)
from shipping_shipment ss
left join shipping_shipmentdetail sd
on ss.id = sd.shipment_id
GROUP BY
ss.order_id,
ss.tenant_id;

sql select query from two different tables WHERE active=1

i try select all columns from two different tables WHERE active = 1
i have 2 tables table_pro and table_basic,
sql:"select * from table_basic,table.name";
and 2 condition:
WHERE active = 1
WHERE table_pro.id = table_basic.name.id
how to make it correctly
Here is table_pro
+----+--------+---------+-----------+
| id | people | rooms | active |
+----+--------+---------+-----------+
| 1 | 5 | 10 | 0 |
| 2 | 12 | 17 | 0 |
| 3 | 21 | 38 | 1 |
+----+--------+---------+-----------+
Here is table_basic
+---------+-------+---------+------------+----------+
| name_id | name | balance | title | time |
+---------+-------+---------+------------+----------+
| 1 |shop | 100 | failed | 15:10:20 |
| 2 |factory| 75 | error | 15:10:20 |
| 3 |studio | 25 | timed_out | 15:10:20 |
+---------+-------+---------+------------+----------+
I'd like to have this output result only rows (from of all columns) with status active = 1
+-----+-------+----- --+--------+-------+----------+---------+--------+
| id | people| rooms | name |balance| title | time | active |
+-----+-------+--------+--------+-------+----------+---------+--------+
| 3 | 21 | 38 | studio |25 | timed_out| 15:10:20| 1 |
+-----+-------+--------+--------+-------+----------+---------+--------+
Thanks
SELECT A.id, A.people, A.rooms, B.name, B.balance, B.title, B.time, A.active
FROM
table_pro AS A
JOIN
table_basic AS B
ON
A.id = B.name_id
WHERE
A.id = 3
SELECT table_pro.*, table_basic.*
FROM table_pro
INNER JOIN table_basic
ON table_basic.name_id = table_pro.id
WHERE table_pro.active = 1