Fiddler Here: http://sqlfiddle.com/#!9/9579da/3
Given this table of products
CountryId Product Quantity
1 Apple 100
1 Banana 5000
2 Carrot 50000
3 Apple 500
4 Apple 250
6 Apple 6000
And this table of available money
CountryId Quantity Rate
1 4135 0.005
1 870.5 1
1 1000 1
2 7249.71 0.007
2 1788 0.01
2 10 1
2 352 10
3 1900 0.09
4 29877 0.005
5 7108 0.005
Where Rate represents an exchange rate for the country. Not shown in the money table are seller Ids.
So for the first product, There are 100 apples in country 1. The Pricing for those Apples would be 100 * (0.005) because there was 4135 available money units for the 0.005 rate, of which we only needed 100 for our apples.
Another example: There are 5000 bananas for country 1. The pricing of those Bananas would be 4135 * (0.005) + 865 * (1). There wasn't enough 4135 money units at rate 0.005 so it had to take 865 units from the next available rate which was 1.
I am trying to use this logic and join the money table onto the products table and have a Price column. I have no idea how to start because it is not a simple Join
Related
If I have a table of dates, prices, product_ids, I'd like to return all product_IDs where the current price has dropped X% over Y days.
So for example, find all products that have dropped in price by 10% or more over past 4 weeks, or 20% or more over past 8 weeks.
How can I query for that?
Sample table Data
Sell_Date
Product_ID
Price
2021-01-04
0001
50.00
2021-01-03
0001
45.00
2021-03-02
0001
37.00
2021-01-04
0002
75.00
2021-02-04
0002
65.00
How could I return rows 2, 3 and 5 from above for showing which products had prices that had lowered by at least 10% over the past month or, row 3 if I queried for 20% price reductions over past 2 months?
ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table I need to sum the approver amount and claimed amount based on the contractor. Like the below given table. All has been done by using the stored procedure.
ID pcID contractor approver claimed TotalApprover TotalClaimed
--------------------------------------------------------------------------
1 1 one 1000 900 1000 900
2 1 two 200 100 200 100
3 1 three 1000 1000 1000 1000
4 1 six 100 11 100 11
5 2 six 100 22 200 33
6 3 six 120 1 320 34
7 4 three 102 10 1120 1001
Like the above table I need an output adding(sum) in ascending order based on contractor.
Thanks in advance.
You can use window function
select t.*,
sum(approver) over( partition by contractor order by ID) TotalApprover,
sum(claimed) over( partition by contractor order by ID) TotalClaimed,
from table1 t
You want cumulative sums:
select t.*,
sum(approver) over (partition by contractor order by claimid) as totalapprover,
sum(claim) over (partition by contractor order by claimid) as totalclaim
from t;
This is just accumulated the corresponding values by the dimensions you are asking for.
I have the following MySQL table structure
id product_id order_id order_shipping shipping_code
1 23 1 2.00 GB
2 12 1 2.00 GB
3 5 2 3.50 GB
4 6 2 3.50 GB
5 34 3 3.00 FR
6 8 3 3.00 FR
7 4 4 5.00 IN
I'd like to group the rows by the shipping_code and include the total orders and total shipping cost per code. Something like this:
Code Orders Cost
GB 2 5.50
FR 1 3.00
IN 1 5.00
I'm struggling with the total cost part. The shipping cost is for the order as a whole, not the product. It's just duplicated in each row relating to the same order. So for order #1 the shipping cost was £2, not £4.
How do I do a sum of that column but only for the distinct order rows?
To get around the duplicated shipping costs for each order you need to group your data by order_id first, and then COUNT the number of orders and SUM the shipping costs:
SELECT code, COUNT(o.shipping) AS orders, SUM(o.shipping) AS cost
FROM (SELECT MIN(shipping_code) AS code, MIN(order_shipping) AS shipping
FROM orders
GROUP BY order_id) o
GROUP BY code
Output:
code orders cost
GB 2 5.5
FR 1 3
IN 1 5
Demo on dbfiddle
I have a fee table that contains fees in a list of price cutoffs based on item pricing. For example the first fee range is for items that are priced at $0 and up to the next tier have a $1 fee. Items that have a price of $25 up to the next tier have a $2 fee, etc. Here is the fee table:
Fee
Cutoff Fee
------ ---
0 1
25 2
100 3
Here is the item table with the prices:
Item
Id Price
------ ------
1 32
2 18
3 2
4 100
The result should look like this:
Desired Item Fees Result
Id Price Fee Total Price with fee
----- ------- ------- -----------
1 32 2 34
2 18 1 19
3 2 1 3
4 100 3 103
Creating the result has been challenging. Here is what the cartesian product of the join result looks like between the two tables:
Id Price Cutoff Fee
--- ------- ------- ---
1 32 0 1 -- eliminate because price is above the next cut 25
2 18 0 1
3 2 0 1
4 100 0 1 -- eliminate because price is above the next cut 25
1 32 25 2
2 18 25 2 -- eliminate because price is below this cut
3 2 25 2 -- eliminate because price is below this cut
4 100 25 2 -- eliminate because price is above (equal to) the next cut 100
1 32 100 3 -- eliminate because price is below this cut
2 18 100 3 -- eliminate because price is below this cut
3 2 100 3 -- eliminate because price is below this cut
4 100 100 3
the first where is simple:
where price >= cut
this narrows the list down to:
Id Price Cutoff Fee
--- ------- ------- ---
1 32 0 1 -- eliminate because price is above the next cut 25
2 18 0 1
3 2 0 1
4 100 0 1 -- eliminate because price is above the next cut 25
1 32 25 2
4 100 25 2 -- eliminate because price is above (equal to) the next cut 100
4 100 100 3
Here's the question: how do i filter out records that are in the next pricing tier? This is the sql i have so far.
select price, cutoff, fee from item, fee
where price >= cutoff;
I have tried a subquery:
select price, cutoff, fee,
(select min(cutoff), fee from fee where cutoff > price) as nextfee
from item, fee
where price >= cutoff;
but this gives the error:
Operand should contain 1 Column(s)
One way to approach this problem is to use a correlated subquery to get the first fee in the fee table where the cutoff is greater than or equal to the price:
select id, price, fee, (price + fee) as totalprice
from (select id, price,
(select fee
from fee
where price >= cutoff
order by cutoff desc
limit 1
) as fee
from item
) i
What about this? No subjoins, better for performance and simpler.
SELECT i.id, i.price, MAX(f.fee), i.price + MAX(f.fee)
FROM item i INNER JOIN fee f
ON i.price > f.cutoff
GROUP BY i.id, i.price
Consider the following table (portfolio). It is a transaction log for a stock-market investor. Each day, he either buys, sells or holds (previously bought stocks which are not yet being sold) stocks (identified by sp100_id):
_date sp100_id action price
-----------------------------------
2011-03-21 11 buy 10.50
2011-03-21 55 buy 60.00
2011-03-21 99 buy 5.15
2011-03-22 11 sell 9.80
2011-03-22 55 sell 61.50
2011-03-22 99 hold 5.60
2011-03-23 1 buy 95.00
2011-03-23 2 buy 25.60
2011-03-23 99 hold
2011-03-24 1 sell 96.00
2011-03-24 2 hold
2011-03-24 99 hold
2011-03-25 11 buy 8.90
2011-03-25 2 sell 28.00
2011-03-25 99 hold
The log stops at 2011-03-25. For 2011-03-26, I want to know:
- what stocks are still left in the portfolio
- for what price and on what date these stocks were orginally bought
If we do this manually:
- stock 11 is bought on 2011-03-21, sold on 2011-03-22, but bought again on 2011-3-25 for 8.90 and we haven't sold it since, so it is still in portfolio on 2011-03-26
- stock 55 is bought on 2011-03-21 and sold on 2011-03-22 so not in portfolio anymore
- stock 99 is bought on 2011-03-21 and we have held it and never sold so it is still in portfolio on 2011-03-26 for a price of 5.15
- stock 1 and 2 are both bought and sold before 2011-03-26
So the portfolio on 2011-03-26 consists of:
sp100_id buy_date buy_price
-------------------------------
11 2011-03-25 8.90
99 2011-03-21 5.15
My question is: with what query can the above output be returned from the table?
SQLFiddle here
select t1.sp100_id, t1._date as buy_date, t1.price
from (select * from portfolio where action='buy') t1
left join (select * from portfolio where action='sell') t2
on t1.sp100_id=t2.sp100_id
and t1._date<t2._date
where t2.sp100_id is null
Here is a sqlfiddle demo
select t0.* from portfolio t0
join
(
select sp100_id,max(_date) mdate from portfolio t
where action = 'buy'
and
not exists (select sp100_id from portfolio t2
where t2.sp100_id=t.sp100_id
and t2._date>t._date
and t2.action='sell')
group by sp100_id
) t1 on (t0.sp100_id=t1.sp100_id) and (t0._date=t1.mdate)