Multiple currency orderBy query - mysql

How can I do a SELECT with an ORDER BY item.price ASC condition on a multiple currency database table?
I'm not sure if it is possible.
Item table has price, currency properties with a lot of currencies of all types.

One way would be to have another table with a conversion rate which would allow you to express each price converted to a common currency. Your ORDER BY would then be done based on the common currency.
For example:
SELECT item, price as local_price, price * common_conversion_rate as comparison_price
FROM item_table
left join conversion_rates ON item_table.currency = conversion_rates.currency
ORDER BY common_conversion_rate * price
The conversion_rates table then contains columns:
currency common_conversion_rate
USD 1.0
GBP 1.4
ZAR 0.1
...
This assumes that you can just pull the currency from your item table and multiply it by a factor from the conversion_rates table.

Related

Total amount of sales done for each product using SQL

Here is the structure of 1st Table called Product.
PRODID PDESC PRICE CATEGORY DISCOUNT
101 BALL 10 SPORTS 5
102 SHIRT 20 APPAREL 10
Here is the structure of 2nd table called SaleDetail.
SALEID PRODID QUANTITY
1001 101 5
1001 101 2
1002 102 10
1002 102 5
I am trying to get total sales amount for each product by joining 2 tables. Here is the SQL i tried but its not giving correct result.
select a.prodid,
(sum((price - discount))),
sum(quantity),
(sum((price - discount))) * sum(quantity)
from product a
join saledetail b on a.prodid = b.prodid
group by a.prodid
2nd column of the query is giving incorrect final price. Please help me correct this SQL.
Please find an indicative answer to your question in the fiddle.
A problem stems from the aggregation of the difference of price. In case that the same product has two different prices, then these prices would be aggregated to one.
Moreover, you multiple the sums of the prices and quantities, while you need to perform the calculation on every sample. Look at the answer by #DanteTheSmith.
You might consider to use the SaleDetail table on the left side of your query.
SELECT SD.PRODID,
P.Price-P.Discount AS Final_Price,
SUM(SD.QUANTITY) AS Amount_Sold,
SUM((P.Price-P.Discount)*SD.QUANTITY) AS Sales_Amount
FROM SaleDetail AS SD
JOIN Product AS P
ON SD.PRODID = P.PRODID
GROUP BY SD.PRODID, P.Price-P.Discount
It would help if you built the example in SQL fiddle or gave the creates for the tables, but if I have to guess your problem is:
(sum((price - discount))) * sum(quantity)
needs to be:
sum((price - discount) * quantity)
(price - discount) * quantity is the function you wanna apply PER ROW of the joined table then you wanna add all those up with SUM() when grouping by prodid.
Furthermore, you can notice that (price - discount) needs to be done ONLY ONCE PER ROW so a quicker version would be to do:
(price-discount) * sum(quantity)
That would give you the total money earned for that product across all the sales you made, and I am guessing this is what you want?
I just notice you have a problem with 2nd column, dunno if that has been in question all along:
(sum((price - discount)))
Why are you summing? Do you want the money earned per product per unit of the product? Well guess what, your price is the same all the time, same as your discount so you can simply go with:
(price-discount) as PPP
NOTE: This assumes the discount is numerical (not percentage) and is applicable to all your sales, also the price is forever the same all which is not real life like.

SubTotal Query based on condition of other column

I am creating a spare part management database in Microsoft Access. I have two table which are ItemTable and EntryTable. ItemTable holds information about each item with unique ItemID and EntryTable holds information of each items usage. I need to calculate the total stock left for each items based on the usage.
So as you can see, for the ItemID with 2, i need to calculate the total stock left based on the usage of In or Out of Status field.
If status is In then plus elseif status is Out then minus. Then total the stock of ItemID 2. Thus the total stock for ItemID 2 will be 3. I have figured out by total and group by for the ItemID but i cannot figure out the way to subtotal based on condition from other column. Thank you.
You can do it with conditional aggregation:
select itemid,
sum(iif(status = 'In', 1, -1) * quantity) as total
from entrytable
group by itemid

Show IDs for the highest MAX values in a matrix

I have a matrix that's grouped by different company brands. Essentially, I'm showing the highest value spent by a customer for each company brand (I've used MAX in my expression to get those). I also want to show Customer IDs attributed to those MAX values. Does anybody know how to do that? Is there any expression?
Example:
Company Brand: Nike | Max value spent: £500 | Customer ID: ???
Thanks
You can use subqueries to retrieve summed values in your SQL Query
SELECT T1.CustomerID, T1.CompanyBrand,
(SELECT Max(S1.ValueSpentField) FROM TableThatContainsValueSpent S1 WHERE T1.CustomerID = S1.CustomerID)
FROM Company T1

multiple currencies in a single table

How to make a MySQL table that could contain multiple type of currencies ($, Euros, ...) ? And if I want to make a final report is their a way to make a sum of those currencies other than adding multiple if statements to the sum ?
There may be several aspects involved. So here is some brainstorming:
You offer the same product in different currencies, e.g. a toy for 10 EUR or 11 USD. So you'd probably have an item table plus an item_price table, where the latter has product number, currency and price. (An alternative would be to have just one price, e.g. in USD and a currency table with conversion rates and you'd calculate the foreign price. But then you'd get "ugly" prices, e.g. 10.57, rather than typical prices like 10.50, 10.90 or 10.99. But well, you could have a price adjustment funtion for that, too. And you'd have to keep your conversion tables up-to-date or live with possible losses.)
A customer buys products and pays the bill. The bill is in one currency (store price and currency in the bill), but you also get your money in your currency, as PayPal or the bank convert it at a daily rate. Do you need to store this real amount, too? Then that would be another column in your bill.
I don't know how it is about taxes. Different currencies sounds like different countries. Maybe you'd have to deal with these in your database, too. I just don't know. The prices you show and store are usually gross prices (the price a customer actually pays) and the taxes (could be different VAT percentages with different products in one single bill) would have to be calculated.
As to getting sums: with all the information stored you'd get them with joins from the tables. No if-then-else in my opinion.
As per my opinion you can create a Country Table which contains
CountryID, CountryName, CurrencyCode, ExchangeRate. Now In that
country table you have to add all countries which you want to add but
you have to keep one thing in mind that you have to decide 1 currency
as base currency and put exchangeRate as 1 for that currency and
convert all other currencies exchangeRate based on the base currency
and insert into that table. i.e. Keep base currency as USD so insert
1 record with USD and exchangeRate must be 1 for that currency.
Suppose I am adding India as country than exchangeRate for that
country is based on USD as 66.40 Rs. insert all other countries
according to this entries.
Now when you want to add any transaction related to money in any
table then keep exchangeRate column with that amount column. ind
insert the exchangeRate and amount same as user's currency. i.e. If
my user is in India country, so it's currency is INR. Now that user
wants to add 1000 Rs. then you have to enter that 1000 Rs amount in
transaction table as it is without any conversion with exchange Rate
of 66.40 (fetch from country table). Same way If user currency is USD
and he wants to add a transaction of 100$ than store 100 as Amount
and exchangeRate as 1.
Now when you want to create any reports then just divide exchangeRate
with Amount, so you will get report in your base currency and after
conversion in base currency you can generate each report in any
currency as per your requirement.

SELECT rows having equal column(c1) value and adding adding another column(c2) value. Performing same for all the distinct column values for column c1

I need to write an SQL Query for MySQL Database which gives me the data according to the necessary conditions
I have a table having structure like this
id: orderId
amount : itemAmount
rate : itemRate
time : orderTime
Now I have to select all the rows having same rates and then have to add all the items having same rates. This should work for each rate means for eg.
row with id 1 is having rate $20 with 2 items and row 3 is having rate $20 with 5 items
row with id 2 is having rate $40 with 4 items and row 4 is having rate $40 with 7 items
I should get result is
rate: $20 totalItem : 7
rate: $40 totalItem : 11
As I stated in my comment, I don't know what your columns are called, I'll assume in my answer they're called id, amount, rate and time.
SELECT rate, COUNT(*) AS totalItems -- Select the rate, and count everything
FROM theRelevantTable -- Use the rows in this table
GROUP BY rate -- But do the selection per group.
In clearer English:
Group the rows in theRelevantTable by their rate, then, for every group, give me the rate and count how many rows there are in the group.
I hope this is what you meant. I do not entirely understand your question.
maybe this? SQL FIDDLE to play with
ASSUMPTIONS: you have a table called orders with columns rate, amount, time, id.
amount stores a quantity of items, rate is the price they are at, and time is when it occurred.
SELECT
rate,
SUM(amount)
FROM orders
GROUP BY rate