我正在使用phpMyAdmin,我有两个表:
___ SalesTaxes
|--------|----------|------------|
| STX_Id | STX_Name | STX_Amount |
|--------|----------|------------|
| 1 | Tax 1 | 5.00 |
| 2 | Tax 2 | 13.50 |
|--------|----------|------------|
___ BillableDatas
|--------|---------------|------------|------------|--------------|----------|---------------------|
| BIL_Id | BIL_BookingId | BIL_Date | BIL_Status | BIL_Quantity | BIL_Rate | BIL_ApplicableTaxes |
|--------|---------------|------------|------------|--------------|----------|---------------------|
| 1 | 2 | 2018-03-06 | notcharged | 2 | 100.00 | 1 |
| 2 | 2 | 2018-03-07 | notcharged | 3 | 105.00 | 1,2 |
|--------|---------------|------------|------------|--------------|----------|---------------------|
我希望每天列出___BillableDatas
中的可结算内容列表,具体取决于事物的状态(收费与未收费)。
这样的事情:
|------------|-----------------|--------------------|------------------|--------------------|
| Date | BIL_Sum_Charged | BIL_Sum_Notcharged | Taxes_ForCharged | TaxesForNotCharged |
|------------|-----------------|--------------------|------------------|--------------------|
| 2018-03-06 | 0.00 | 200.00 | 0.00 | 10.00 |
| 2018-03-07 | 0.00 | 315.00 | 0.00 | 58.275 |
|------------|-----------------|--------------------|------------------|--------------------|
我实际上有什么:
SELECT b.BIL_Date,
SUM(case when b.BIL_Status = "charged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") ) ))
as BIL_Sum_Charged,
SUM(case when b.BIL_Status = "notcharged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") )))
as BIL_Sum_Notcharged,
SUM(case when b.BIL_status = "charged" then s.STX_Amount else 0 end)
as STX_TAX_Charged,
SUM(case when b.BIL_status = "notcharged" then s.STX_Amount else 0 end)
as STX_TAX_NotCharged
FROM ___BillableDatas b
INNER JOIN ___SalesTaxes s
ON FIND_IN_SET(s.STX_id, b.BIL_ApplicableTaxes) > 0
WHERE b.BIL_HotelId='cus_CNHLMiMOzP5cuM'
AND b.BIL_BookingId='2'
GROUP BY b.BIL_Date
ORDER BY b.BIL_Date ASC
此查询出现问题的原因是我没有BIL_Sum_Charged
和BIL_Sum_NotCharged
的正确总和,因为总和不会乘以BIL_Quantity
列。
例如,我105
而2018-03-07我应该315
BIL_Sum_Notcharged
。最后的税金总额相同。
我的错误在哪里?
请参阅SQL小提琴: http://sqlfiddle.com/#!9/8b3cb/1
非常感谢你的帮助。
答案 0 :(得分:1)
经过讨论,这似乎就是你所需要的。希望这会有所帮助。
SELECT b.BIL_Date,
SUM(case when b.BIL_Status = "charged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") ) )) * b.BIL_Quantity
as BIL_Sum_Charged,
SUM(case when b.BIL_Status = "notcharged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") ))) * b.BIL_Quantity
as BIL_Sum_Notcharged,
SUM(case when b.BIL_status = "charged" then s.STX_Amount else 0 end) * (SUM(case when b.BIL_Status = "charged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") ) )) * b.BIL_Quantity) / 100
as STX_TAX_Charged,
SUM(case when b.BIL_status = "notcharged" then s.STX_Amount else 0 end) * ( SUM(case when b.BIL_Status = "notcharged" then b.BIL_Rate else 0 end
/ (1 + LENGTH(b.BIL_ApplicableTaxes)
- LENGTH( REPLACE ( b.BIL_ApplicableTaxes, ",", "") ))) * b.BIL_Quantity) / 100
as STX_TAX_NotCharged
FROM ___BillableDatas b
INNER JOIN ___SalesTaxes s
ON FIND_IN_SET(s.STX_id, b.BIL_ApplicableTaxes) > 0
WHERE b.BIL_HotelId='cus_CNHLMiMOzP5cuM'
AND b.BIL_BookingId='2'
GROUP BY b.BIL_Date
ORDER BY b.BIL_Date ASC;
根据讨论编辑。