Sum is not done if joined table is empty with MySQL - mysql

I have the following tables structure and trying to make a report from these:
___BillableDatas
|--------|------------|---------|--------------|------------|
| BIL_Id | BIL_Date |BIL_Rate | BIL_Quantity | BIL_Status |
|--------|------------|---------|--------------|------------|
| 1 | 2018-03-01 | 105 | 1 | charged |
| 2 | 2018-03-01 | 15 | 2 | notcharged |
| 3 | 2018-03-01 | 5 | 1 | notcharged |
|--------|------------|---------|--------------|------------|
___SalesTaxes
|--------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|--------|--------------|------------|
| 8 | Tax 1 | 5.000 |
| 9 | Tax 2 | 15.000 |
|--------|--------------|------------|
STX_Amount is a percentage.
___ApplicableTaxes
|-----------|-----------|
| ATX_BILId | ATX_STXId |
|-----------|-----------|
| 1 | 8 |
| 1 | 9 |
|-----------|-----------|
ATX_BILId is the item ID link with ___BillableDatas.
ATX_STXId is the tax ID link with ___SalesTaxes.
I need to get to sum of the items per day
- without tax
- with tax
So mething like this:
|------------------|---------------|------------|
| BIL_RateNonTaxed | BIL_RateTaxed | BIL_Status |
|------------------|---------------|------------|
| 105.00 | 126.00 | charged | <- Taxes #8, #9 applicable
| 35.00 | 35.00 | notcharged | <- No taxes here
|------------------|---------------|------------|
Explications on the totals:
105 = 105*1 -- (total of the charged item multiply by the quantity)
35 = (15*2)+5 -- (total of the notcharged items multiply by the quantity)
126.00 = 105+(105*(5+15)/100)
35.00 = as no taxe, put the non taxed value.
My last try was this one:
SELECT BIL_Status
, SUM(BIL_Rate*BIL_Quantity) BIL_RateNonTaxed
, IFNULL(SUM((BIL_Rate*BIL_Quantity)+(BIL_Rate*BIL_Quantity*total_sales_tax/100)), SUM(BIL_Rate*BIL_Quantity)) BIL_RateTaxed
FROM
( SELECT b.*
, SUM(t.STX_Amount) total_sales_tax
FROM ___BillableDatas b
LEFT JOIN ___ApplicableTaxes bt
ON bt.ATX_BILId = b.BIL_Id
LEFT JOIN ___SalesTaxes t
ON t.STX_Id = bt.ATX_STXId
GROUP
BY ATX_BILId
) x
GROUP
BY BIL_Status
This query works just when each item has a linked taxe (case of my item #1). When item has no linked taxes (item #2 and #3), the sum is not made.
Please see this SQLFiddle to help you if needed:
http://sqlfiddle.com/#!9/433a3f/2
The only one error with the link is I should have 35 and not 30.
Thanks.

The subquery was grouping by the wrong thing. You are grouping by ATX_BILId, but I think you really wanted to get all the unique billable data bill_ids. At least the following query returns what you expected. The only difference is changing "GROUP BY ATX_BILId" to "GROUP BY BIL_Id"
SELECT BIL_Status
, SUM(BIL_Rate*BIL_Quantity) BIL_RateNonTaxed
, IFNULL(SUM((BIL_Rate*BIL_Quantity)+(BIL_Rate*BIL_Quantity*total_sales_tax/100)), SUM(BIL_Rate*BIL_Quantity)) BIL_RateTaxed
FROM
( SELECT b.*
, SUM(t.STX_Amount) total_sales_tax
FROM ___BillableDatas b
LEFT JOIN ___ApplicableTaxes bt
ON bt.ATX_BILId = b.BIL_Id
LEFT JOIN ___SalesTaxes t
ON t.STX_Id = bt.ATX_STXId
GROUP
BY BIL_Id
) x
GROUP
BY BIL_Status
Link to SQL Fiddle
In general, when you are troubleshooting this type of query, the first thing to do is examine the returned rows without the group bys. It becomes easier to see the problem when you run the following query.
SELECT BIL_Status,
BIL_Rate,
BIL_Quantity,
total_sales_tax
FROM
( SELECT b.*
, SUM(t.STX_Amount) total_sales_tax
FROM ___BillableDatas b
LEFT JOIN ___ApplicableTaxes bt
ON bt.ATX_BILId = b.BIL_Id
LEFT JOIN ___SalesTaxes t
ON t.STX_Id = bt.ATX_STXId
GROUP
BY ATX_BILId
) x

Related

MySQL select N latest rows for each product from 3 relational tables

Now i have this code which return latest record for each product. But i don't know how to modify this to get for example 3 latest rows for each product.
I want to compare latest product prices and i need few latest rows of each.
shops
id | shopId
-----------
1 | 2345
2 | 6573
products
id | shopId | title | active | pDateAdded | pDateUpdate
---------------------------------------------------------------------------
18 | 1 | Honda | 1 | 2021-03-07 01:56:34 | 2021-03-07 04:36:34
19 | 2 | Subaru | 1 | 2021-03-07 03:43:34 | 2021-03-08 04:36:34
20 | 1 | VW | 1 | 2021-03-07 07:21:34 | 2021-03-09 04:36:34
21 | 2 | Ford | 0 | 2021-03-07 11:37:34 | 2021-03-10 04:36:34
prices
id | shopId | productId | price | dDateAdded
-----------------------------------------------------
224 | 1 | 18 | 2385 | 2021-03-09 12:39:57
225 | 2 | 19 | 1523 | 2021-03-09 13:14:44
226 | 1 | 20 | 5489 | 2021-03-09 17:32:18
227 | 1 | 18 | 2256 | 2021-03-10 18:22:13
228 | 2 | 19 | 1600 | 2021-03-10 21:33:21
229 | 1 | 20 | 5321 | 2021-03-10 14:15:56
230 | 1 | 18 | 2137 | 2021-03-11 05:55:25
231 | 2 | 19 | 1666 | 2021-03-11 17:31:49
232 | 1 | 20 | 5001 | 2021-03-11 20:18:01
This command return only 1 latest record from prices table for every product from products table for specific shopId
SELECT s.*, c.*, d.*
FROM shops AS s
LEFT JOIN products AS c ON c.shopId = s.id
LEFT JOIN (
SELECT productId, MAX(dDateAdded) MaxDate
FROM prices
GROUP BY productId
) MaxDates
ON MaxDates.productId = c.id
LEFT JOIN prices AS d ON d.productId = c.id AND d.shopId = s.id AND MaxDates.MaxDate = d.dDateAdded
WHERE s.id = ".$shopId."
For example if shopId=1 this command get only that records (I omitted here the data from the other tables that are retrieved):
230 | 1 | 18 | 2137 | 2021-03-11 05:55:25
232 | 1 | 20 | 5001 | 2021-03-11 20:18:01
But i want to get for example 2 latest records for every product where shopId=1, so the records which i want to get:
(shops)id | (shops)shopId | title | active | price | dDateAdded
1 | 2345 | Honda | 1 | 2256 | 2021-03-10 18:22:13
1 | 2345 | Honda | 1 | 2137 | 2021-03-10 14:15:56
1 | 2345 | VW | 1 | 5321 | 2021-03-11 05:55:25
1 | 2345 | VW | 1 | 5001 | 2021-03-11 20:18:01
To select N latest rows needs to allocate row number and to filter by N rows. However, the ROW_NUMBER function is not supported in MySQL 5.7.
So that you need to simulate the ROW_NUMBER function like the follwing:
You can get the desired result by adding subquery with row number to your query like the below:
DB Fiddle
SELECT
s.id,
s.shopId,
c.title,
c.active,
d.price,
d.dDateAdded
FROM shops AS s
LEFT JOIN products AS c ON c.shopId = s.id
LEFT JOIN prices AS d ON d.productId = c.id AND d.shopId = s.id
--
LEFT JOIN (
SELECT
p1.id,
COUNT(p2.dDateAdded) + 1 row_num
FROM prices p1 LEFT JOIN prices p2
ON p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
GROUP BY p1.id, p1.shopId, p1.productId, p1.dDateAdded
) AS w
ON d.id=w.id
--
WHERE
s.id = 1 AND
w.row_num <= 2
DB Fiddle
SELECT
id,
shopId,
productId,
price,
dDateAdded
FROM (
SELECT p1.*,
(
SELECT COUNT(*)+1 FROM prices p2
WHERE
p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
) row_num
FROM prices p1
) p
WHERE
shopId = 1 AND
row_num <= 2
ORDER BY id
DB Fiddle
SELECT p.* FROM prices p
INNER JOIN (
SELECT
p1.id,
COUNT(p2.dDateAdded) + 1 row_num
FROM prices p1 LEFT JOIN prices p2
ON p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
GROUP BY
p1.id,
p1.shopId,
p1.productId,
p1.dDateAdded
) w
ON p.id=w.id
WHERE
p.shopId = 1 AND
w.row_num <= 2
ORDER BY p.id
Other way using a variable

How to get hotel total room and booked room in single query

How to get hotel total room and booked room in single query.
I used 2 queries get result. I need this in single query.
The issue I am experiencing since all counts are in this table htl_room_information.id_hotel
Booked room:
SELECT x.hotel_name
, count(i.id_hotel) room
FROM htl_booking_detail d
JOIN htl_branch_info_lang x
ON x.id=d.id_hotel
JOIN htl_room_information i
ON d.id_room=i.id
group
by x.hotel_name;
+------------------------------------------+------+
| hotel_name | room |
+------------------------------------------+------+
| hotel | 3 |
| hotel1 | 1 |
| hotel2 | 4 |
| hotel3 | 13 |
| hotel4 | 9 |
| hotel5 | 3 |
| hotel6 | 3 |
| hotel7 | 2 |
+------------------------------------------+------+
Total Rooms
SELECT (htl_branch_info_lang.hotel_name) as hotel_name,count(htl_room_information.id_hotel) as total_room FROM htl_room_information ,htl_branch_info_lang where htl_room_information.id_hotel=htl_branch_info_lang.id group by htl_branch_info_lang.hotel_name;
+------------------------------------------+------------+
| hotel_name | total_room |
+------------------------------------------+------------+
| hotel | 219 |
| hotel2 | 25 |
| hotel3 | 16 |
| hotel4 | 5 |
| hotel5 | 55 |
| hotel6 | 27 |
| hotel7 | 56 |
| hotel8 | 52 |
+------------------------------------------+------------+
Use dependent subqueries.
SELECT x.hotel_name,
(
SELECT count(i.id_hotel)
FROM htl_booking_detail d
JOIN htl_room_information i
ON d.id_room=i.id
WHERE x.id=d.id_hotel
) as room,
(
SELECT count(i.id_hotel)
FROM htl_room_information i
WHERE i.id_hotel=x.id
) as total_room
FROM htl_branch_info_lang x
I assume that the hotel_name is unique in the htl_branch_info_lang table. If not, you have to put distinct behind the first SELECT.
Hoping, I understood your question correctly.
Please check below query.
select b.hotel_name hotel_name, a.room ,
b.total_room
from
(SELECT htl_branch_info_lang.hotel_name,
count(htl_room_information.id_hotel) as room FROM htl_booking_detail,
htl_branch_info_lang, htl_room_information WHERE
htl_branch_info_lang.id=htl_booking_detail.id_hotel and
htl_booking_detail.id_room=htl_room_information.id group by
htl_branch_info_lang.hotel_name ) a RIGHT join (SELECT (htl_branch_info_lang.hotel_name) as
hotel_name,count(htl_room_information.id_hotel) as total_room
FROM htl_room_information ,htl_branch_info_lang
where htl_room_information.id_hotel=htl_branch_info_lang.id
group by htl_branch_info_lang.hotel_name) b
on a.hotel_name = b.hotel_name
;
You can use subquery
try this:
SELECT (a.hotel_name) as
hotel_name,count(htl_room_information.id_hotel) as total_room, (SELECT
count(htl_room_information.id_hotel) as room FROM htl_booking_detail,
htl_branch_info_lang b, htl_room_information WHERE
b.id=htl_booking_detail.id_hotel and
htl_booking_detail.id_room=htl_room_information.id and
b.hotel_name = a.hotel_name), count(htl_room_information.id_hotel) - (SELECT
count(htl_room_information.id_hotel) as room FROM htl_booking_detail,
htl_branch_info_lang b, htl_room_information WHERE
b.id=htl_booking_detail.id_hotel and
htl_booking_detail.id_room=htl_room_information.id and
b.hotel_name = a.hotel_name) as available FROM htl_room_information
,htl_branch_info_lang
a where htl_room_information.id_hotel=a.id group by
a.hotel_name;

SELECT and COUNT in one query from two tables with mySQL

Here my tables structure:
___Rooms
|--------|-------------|------------|
| ROO_Id | ROO_HotelId | ROO_Status |
|--------|-------------|------------|
| 1 | 1 | active |
| 2 | 1 | active |
| 3 | 1 | inactive |
|--------|-------------|------------|
___Subscriptions
|-------------|-----------|
| SUB_HotelId | SUB_Limit |
|-------------|-----------|
| 1 | 24 |
|-------------|-----------|
I want to select SUB_Limit and count the number of rooms (only the active one).
So the query should returns me something like this:
|-----------|------------|
| SUB_Limit | ROO_Number |
|-----------|------------|
| 24 | 2 |
|-----------|------------|
Why this query do not return me the desired output please ?
SELECT
SUB_Limit,
COUNT(ROO_Id) AS ROO_Number
FROM ___Rooms
LEFT JOIN ___Subscriptions
ON ___Rooms.ROO_HotelId = ___Subscriptions.SUB_HotelId
WHERE ROO_Status = 'active'
AND SUB_HotelId = 1
AND ROO_HotelId = 1
Actually, it gave me:
|-----------|------------|
| SUB_Limit | ROO_Number |
|-----------|------------|
| NULL | 0 |
|-----------|------------|
Try this below query once
SELECT
SUB_Limit, COUNT(ROO_Id) AS ROO_Number FROM
___rooms r,___subscriptions s
WHERE r.ROO_HotelId=s.SUB_HotelId AND ROO_Status='active'
Try below query:
SELECT s.sub_limit, count(ROO_Id)
FROM ___Rooms r
INNER JOIN ___Subscription s ON (s.SUB_HotelId = r.ROO_HotelId)
WHERE s.SUB_HotelId = 1
and r.ROO_STATUS = 'active'
You forgot GROUP BY
SELECT
S.SUB_Limit,
COUNT(*) AS ROO_Number
FROM ___Rooms AS R
RIGHT JOIN ___Subscriptions AS S
ON R.ROO_HotelId = S.SUB_HotelId
WHERE R.ROO_Status = 'active'
AND S.SUB_HotelId = 1
GROUP BY ROO_HotelId
UPD: group by is not necessary as we need only the number of all lines. Also RIGHT JOIN could not be available for your particular MySQL engine (unlike LEFT JOIN or INNER JOIN), so you would better use UNION constructions, to catch zero intersections with main table if I'm not mistaken.

COUNT and SELECT into the same query in mySQL

Form my two tables, I want to select SUB_Limit and count the number of rooms (only the active one).
So the query should returns me something like this:
|-----------|------------|-------------|
| SUB_Limit | ROO_Number | ROO_HotelId |
|-----------|------------|-------------|
| 10 | 0 | 1 |
| 15 | 3 | 2 |
| 5 | 2 | 3 |
| 25 | 0 | 4 |
|-----------|------------|-------------|
Why this query do not return me the desired output please ?
SELECT
ROO_HotelId,
SUB_Limit,
COUNT(ROO_Id) AS ROO_Number
FROM ___Rooms
LEFT JOIN ___Subscriptions
ON ___Rooms.ROO_HotelId = ___Subscriptions.SUB_HotelId
WHERE ROO_Status = 'active'
AND SUB_HotelId = 1
AND ROO_HotelId = 1
Actually, it gave me:
|-----------|------------|
| SUB_Limit | ROO_Number |
|-----------|------------|
| 15 | 3 |
| 5 | 2 |
|-----------|------------|
So I haven't the ___Subscriptions with no rooms.
Here the SQL Fiddle for help.
Thanks in advance.
The query below gives you the result you want.
SELECT
SUB_HotelId,
SUB_Limit,
COUNT(ROO_Id) AS ROO_Number
FROM ___Subscriptions
LEFT JOIN ___Rooms ON
___Rooms.ROO_HotelId = ___Subscriptions.SUB_HotelId
WHERE ROO_Status = 'active' or ROO_Status is null
GROUP BY SUB_HotelId
You need to check for ROO_Status = 'active' or NULL because if the subscription is not linked to a room, there will be no value for ROO_Status. Also, you need to group on SUB_HotelId, because ROO_HotelId will also be null if there is no room to join the subscription to.
Link to fiddle: http://sqlfiddle.com/#!9/c6920c/35

How to SUM column in MySQL left join

So i have table cont_selling
---------------------------------
cont_selling_id | date |
---------------------------------
1 | 2015-05-24 |
2 | 2015-06-06 |
---------------------------------
table 02 cont_sold
----------------------------------------------------
cont_sold_id | cont_selling_id | price |
---------------------------------------------------
1 | 1 | 10 |
2 | 1 | 10 |
3 | 1 | 30 |
4 | 2 | 20 |
5 | 2 | 10 |
--------------------------------------------------
and table 03 payment
----------------------------------------------
payment_id | cont_selling_id | paid |
-----------------------------------------------
1 | 1 | 10 |
2 | 2 | 10 |
3 | 1 | 20 |
4 | 1 | 10 |
5 | 2 | 10 |
-----------------------------------------------
now i need to SELECT table based on
now i want to merge all these three tables based on cont_selling table cont_selling_id column
and want to SUM cont_sold table price column and payment table paid column
this is what i want to do
expecting output
---------------------------------------------
cont_selling_id | price | paid |
---------------------------------------------
1 | 50 | 40 |
2 | 30 | 20 |
---------------------------------------------
so i tried like this in mysql query but it give wrong sum result
SELECT
SUM(Z.price) as total,
SUM(P.amount) as paid
FROM cont_selling S
LEFT JOIN cont_sold Z
ON S.cont_selling_id = Z.cont_selling_id
LEFT JOIN payment P
ON S.cont_selling_id = P.cont_selling_id
GROUP BY S.cont_selling_id
for this above query i m getting output like this
---------------------------------------------
cont_selling_id | price | paid |
---------------------------------------------
1 | 150 | 40 |
2 | 60 | 120 |
---------------------------------------------
Here how you can do it using the aggegare part into inner queries and then join
select
cs.cont_selling_id,
price,
paid
from cont_selling cs
left join(
select sum(price) as price , cont_selling_id from cont_sold
group by cont_selling_id
)x on x.cont_selling_id = cs.cont_selling_id,
left join(
select sum(paid) as paid , cont_selling_id from payment
group by cont_selling_id
)y
on y.cont_selling_id = cs.cont_selling_id;
You should make two different queries with SUM and then combine them to get the desired result:
SELECT T1.cont_selling_id,T1.price,T2.paid
FROM
(SELECT c.cont_selling_id,SUM(cs.price) as price
FROM cont_selling c LEFT JOIN
cont_sold cs ON c.cont_selling_id=cs.cont_selling_id
GROUP BY c.cont_selling_id) as T1 JOIN
(SELECT c.cont_selling_id,SUM(p.paid) as paid
FROM cont_selling c LEFT JOIN
payment p ON p.cont_selling_id=c.cont_selling_id
GROUP BY c.cont_selling_id) as T2 ON T1.cont_selling_id=T2.cont_selling_id
Result:
cont_selling_id price paid
----------------------------
1 50 40
2 30 20
Sample result in SQL Fiddle.
This untested query should work:
with a as(select cont_selling_id , sum(price) as totalprice from cont_sold group by cont_selling_id),
with a as(select cont_selling_id , sum(paid) as totalpaid from payment group by cont_selling_id),
select c.cont_selling_id , totalprice, totalpaid from cont_selling c left join a.count_selling_id = c.count_selling_id
left join b.count_selling_id = c.count_selling_id
You have to create temporary tables, because there is no dependency between your table for price and paid.