I have the following table:
id tax rate quantity
1 20 400 5
2 20 566 2
3 5 200 4
Here is my expected output:
taxableamount taxamount total
3132.00 626.40 3758.40
800.00 40.00 840.00
Let me explain my attempts here:
If each row have the same tax value, I calculated the amount (rate × quantity) from each row of data and then calculate the tax amount (amount × tax ÷ 100) and displayed the result in a single row even if they come from multiple rows. But if the rows do not have same tax value, it will be displayed in separate rows.
$query = "SELECT SUM(
CASE WHEN tax=tax
THEN rate*quantity ELSE 0 END) AS taxableamount,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS tax,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)*tax/100 ELSE 0 END) AS taxamount,
SUM(CASE WHEN tax=tax
THEN (rate*quantity)+(rate*quantity)*tax/100 ELSE 0 END) AS total
FROM pricing group by tax";
while($row = show($query)):
echo '<td>'.$row->taxableamount.'</td>';
echo '<td>'.$row->taxamount.'</td>';
echo '<td>'.$row->total.'</td>';
endwhile;
The above code gives me wrong and unexpected result. So looking at my expected output, how do I go about it. Please help.
You should remove your CASE statements. As i said in my comment, tax it´s allways equal to tax, because you are comparing with the same row. I have added the tax column, so you know the amounts for each kind of tax:
SELECT
SUM(rate * quantity) AS taxableamount,
SUM(rate * quantity * tax / 100) AS taxamount,
SUM(rate * quantity * (1 + tax) / 100) AS total,
tax
FROM
pricing
GROUP BY
tax
what you are looking for is a group by tax in your sql statement. remove the remove case when. and two lines giving col called "tax"? that lokks like an error to me.
group by
aggregation
Related
I have table with
seller_id, transaction_type, sub_total, total_commission_fee, refund_fee, cancellation_fee
3 transaction_type
payment, cancel, refund
i want to get the sum of sub_total, total_commission_fee, refund_fee, cancellation_fee for each seller_id
sample
seller_id transaction_type sub_total total_commission_fee refund_fee cancellation_fee
3 order 40 0 0 0
4 order 10 0 0 0
3 cancel 0 0 0 3
3 refund 28 0 2 0
i want result like this
seller_id payment_total(sum of all sub_total transaction_type order) cancel_total(sum of all cancellation_fee transaction_type cancel) refund_total (sum of all refund_fee transaction_type refund)
i can get total without transaction type.
Transaction::groupBy('seller_id')
->selectRaw('sum(sub_total) as total, seller_id')
Is there a way to get result as i want.
Else i have to do a get request and loop through each one.
This may cause problem when the table becomes big
And what is this kind of operations called?
You can use IF(condition, value_if_true, value_if_false) to sum the values:
Transaction::groupBy('seller_id')
->selectRaw('SUM(IF(transaction_type = "order", sub_total, 0)) AS payment_total,
SUM(IF(transaction_type = "cancel", cancellation_fee, 0)) AS cancel_total,
SUM(IF(transaction_type = "refund", refund_fee, 0)) AS refund_total,
seller_id')
You can use directly with group by seller id(seller_id). please try to use below query
Transaction::groupBy('seller_id')
->selectRaw('SUM(sub_total) AS payment_total,
SUM(cancellation_fee) AS cancel_total,
SUM(refund_fee) AS refund_total,
seller_id')
I have this query for getting out the total amount of check after discount:
Please note that i have to multiply the quantity with price first
SELECT SUM(pino_order_item.ord_item_quan * pino_menu_item.menu_item_price) * (1 - pino_table_order.order_discount)
FROM pino_order_item join pino_menu_item join pino_table_order
WHERE ord_order_id = %s AND menu_item_id = ord_item_mid
I'm trying to echo this on my page with PHP like:
echo number_format($row_get_total['SUM(ord_item_quan * menu_item_price) * (1 - order_discount)'],2);
It's not working at all .. it's giving me totally wrong numbers.
Please help me identify my coding problem and kindly note that all what i want is to multiply the quantity of order item with the price then calculate the discount entered as percentage number (20) or (30) for example and get 2 results (one before discount and one after discount)
Thank you in advance ....
I'm not seeing any group by keyword in the query, if you're trying to get the order total, you will need to group by the order_id.
You said the discount value is stored as integers like 20 or 30. Subtracting that from 1 would give you unexpected values like -19 or -29 respectively. Which is probably the cause of the weird number you're complaining about.
Correct SQL should say something like this:
SELECT SUM( tblItems.price ) * ( 1 - ( tblOrders.discount / 100 ) ) AS totalCheck FROM tblItems JOIN tblOrders USING order_id WHERE tblItems.order_id GROUP BY order_id
Then something like this to echo the result:
echo $row[ 'totalCheck' ]
I have 3 tables:
PRICE
id price date
1 50 20130716
2 30 20130717
TVA
id val start end
1 7 20080101 20103112
2 8 20110101
MARGIN
id qty marg
1 500 25
2 600 20
3 800 15
4 1000 13
5 1250 11
...
Now I have this query which doesn't works:
$quantity = '557';
$link->query("
SELECT (
(price+marg)*((val+100)/100)
)
FROM PRICE
JOIN TVA
JOIN MARGIN
WHERE date = '20130717'
AND end = ''
AND qty = '$quantity'
");
The problem is that there isn't a qty = '557' on the table.
What I'd like to do is to select the most near quantity to (in this case) '557'.
So if:
$quantity = '557' the query should select 600
$quantity = '701' the query should select 800
$quantity = '1238' the query should select 1250
etc.
Is this possible?
If you put it into a procedure, you can use something like this (sort-of pseudocode):
CREATE PROCEDURE pDoStuff(#target INTEGER)
AS
DELCARE #qty INTEGER
-- Get closest qty
#qty = SELECT TOP 1 qty
FROM table
ORDER BY ABS(#target - qty) ASC
-- use that "actual" qty in your query
SELECT ((price+marg)*((val+100)/100)
FROM price
JOIN TVA
JOIN MARGIN
WHERE date = 'thedate'
AND end = ''
AND qty = #qty
GO
The syntax is incorrect, but it gives you an idea. This will allow you to select ALL rows from your original query with the closest quantity value. Most of the other answers here will limit your final results to one row (which may or may not be what you actually want).
...
AND `qty` <= $quantity
ORDER BY `qty` DESC
LIMIT 1
You can get value bigger than yours, ordered ascending and limit to 1 result. so you can firt value bigger or equal yours
SELECT (
(price+marg)*((val+100)/100)
)
FROM PRICE
JOIN TVA
JOIN MARGIN
WHERE date = '20130717'
AND end = ''
AND qty >= '$quantity'
ORDER BY qty ASC LIMIT 1
With same method you can get value lower than your number and see which one is closer to your qty
I have SQL tables:
id_details
year-month
type (only have 2 values , income and outcome)
amount
and table have row like this:
(year-month) (type) (amount)
November-2012 income 50000
November-2012 income 30000
December-2012 income 20000
November-2012 outcome 10000
December-2012 outcome 50000
December-2012 income 10000
What I want is, showing a query with result like this:
(year-month) (sum income) (sum-outcome) (sub-total balance)
November-2012 80000 10000 70000
December-2012 30000 50000 -20000
finally
total-balance = 50000
How can I do this?
or can you recommend me better tables design?
SELECT `year-month`,
SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END) `sum-income`,
SUM(CASE WHEN type = 'outcome' THEN amount ELSE 0 END) `sum-outcome`,
SUM(amount) `sum-total balance`
FROM <JOINS>
GROUP BY `year-month`
I need to find a better way to get the discount for each article price in our web shop depending on which pricelist or pricelist/discount list a customer has. I think this is possible to do in just one query instead of the 5 I have today, but I really do not know where to start.
All our customers have a pricelist and some have both pricelist and one extra discount list. Today we have about 25 different pricelists and about 100 extra discount lists.
All the pricelists are structured in the same manner; they have a price group and a discount in percent.
For example pricelist 01 could look like
A 20
B 35
C 20
The extra discount list is structured in a different manner and can have a fixed price or percentage. It also has three different priority levels: discount based on the article code (has priority 1), based on category (has priority 2) and based on price group (has priority 3).
Discount list 0013 could look like:
In the article tab
PL-2344-444 40 (%)
P-0455-23 200 (SEK)
In the category tab
C12 50 (%)
N12 35 (%)
Today I have three different queries to see if I get a hit in the discount list:
First I check to see if I get a hit in priority 1: (FIXED returns f and PERCENTAGE r)
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013'
If the above returns 0, I do the second query, priority 2:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013'
And the last one priority 3:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013'
If none of the extra discount lists returns 0 I get the discount from the pricelist
SELECT DISCOUNT FROM PUPRIREG WHERE PRICELIST = '01' AND PRICEGROUP = 'F'
I call the function like follows
$discount = discount($articlecode, $category, $pricegroup);
function discount($articlecode, $category, $pricegroup){
$articlecode = sanitizingData($articlecode);
$category = sanitizingData($category);
$pricegroup = sanitizingData($pricegroup);
// do priority 1
// prio 2
// prio 3
// pricelist
return $discount;
}
I would be so happy if someone could show me how to do this. I am using mysqli and php.
Many thanks
Best regards linda
You can do the queries with a union:
(SELECT DISCOUNT, FIXED, PERCENTAGE, 1 priority FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 2 priority FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 3 priority FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, 0 fixed, 0 percentage, 4 priority FROM PUPRIREG
WHERE PRICELIST = '01' AND PRICEGROUP = 'F')
order by priority;
The additional artificial priority column and order by ensures, that you get the discounts properly sorted.