I have user_cost information and hourly discount information.
I am trying to get the below result using below two tables.
The discount_per_time is applied every time of user_cost, and the users of the same time shares the discount.
Discounts are applied sequentially for each row, and are performed until no more discounts are available.
I thought it would be possible using presto's window function, so I tried it for a few days but couldn't do it. Can I create the below result through query?
table: user_cost
time user_id cost
1 aaa 20
1 bbb 5
2 aaa 11
2 bbb 5
3 aaa 15
4 aaa 1
4 bbb 1
table: discount_per_time
discount_id cost
d-1 10
d-2 5
RESULT:
time user_id type cost discount_id discounted_cost
1 aaa discount d-1 10
1 aaa discount d-2 5
1 aaa usage 5
1 bbb usage 5
2 aaa discount d-1 10
2 aaa discount d-2 1
2 bbb discount d-2 4
2 bbb usage 1
3 aaa discount d-1 10
3 aaa discount d-2 5
4 aaa discount d-1 1
4 bbb discount d-1 1
+ add:
You should be able to see which discounts apply to which users and how much per time.
The discounted_cost per time cannot be greater than the usage cost and the discountable cost.
If all of the cost of the time is discounted, "usage" is not displayed (instead, the value before discount can be inferred through discounted_cost).
e.g.
User 'aaa' uses a cost of 20 and user 'bbb' uses a cost of 5 in a time.
Both the 10 discount of d-1 and the 5 discount of d-2 are applied to aaa, so the final cost of aaa becomes 5. All discounts are applied to aaa, so the final cost of bbb is still 5.
aaa uses 11 and bbb uses 5. d-1 gives aaa a discount of 10, d-2 gives a discount of 1 so aaa does not charge. 4 out of 5 of d-2 left. This can be applied to bbbs. So the final cost of bbb is 1.
When both users' cost is 1, those are discounted by using only 2 out of 10 of d-1. There is no final usage for both users.
Related
i have a table below that looks like the current one below. How can i put the values for date, count2, amount2 from columns into rows just ONCE?
current:
date type count amount type2 date count2 amount2
1/1/20 00 5 13.49 ZZZ 1/1/20 0 5.00
1/1/20 12 6 14.69 ZZZ 1/1/20 0 5.00
1/1/20 11 10 20.66 ZZZ 1/1/20 0 5.00
expected
date type count amount
1/1/20 12 6 14.69
1/1/20 12 6 14.69
1/1/20 11 10 20.66
1/1/20 ZZZ 0 5.00
what can you do it just splits data flow into two select steps.
in the first select step use your first three columns.
in the second select step use your remaining other columns. and use filter rows to remove redundancy. then use dummy step and link to both select steps.
My project needs me to come up with a query which can compare any 2 months data side by side, by just keying in the dates of the 2 months.
I have done 2 separate queries that can only do single month data because I can only enter 1 date per query. I tried to combine this 2 separate query into 1 single query by selecting the columns from each table but it gives me blank data.
I will need some help in combining the 2 queries together, into 1 table as a view form and allowing the user to key in the 2 dates they want to get their data from. Below will be the 2 queries result I can achieve and also the end result I want to achieve from combining this 2 queries.
Conditions to merge the two table is that the company will be the same for both dates, and the item the company bought (if any). If the company did not buy the item on the month , data should be blank.
Query 1 : User will enter "First month" they want the data from
Inv Number Company Date Item Price Quantity Total
123 ABC 1/1/2018 Table 5 3 15
123 ABC 1/1/2018 Chair 2 4 8
345 XYZ 1/1/2018 Table 5 5 25
345 XYZ 1/1/2018 Chair 2 6 12
Query 2: User will enter "Second Month" they want the data from
Inv Number Company Date Item Price Quantity Total
999 ABC 1/2/2018 Table 4 3 12
999 ABC 1/2/2018 Chair 2 5 10
899 XYZ 1/2/2018 Table 4 3 12
End result : User will be allowed to key in both dates they want the data from
Inv Number Company Date Item Price Quantity Total Date Item Price Quantity Total Inv Number
123 ABC 1/1/2018 Table 5 3 15 1/2/2018 Table 4 3 12 999
123 ABC 1/1/2018 Chair 2 4 8 1/2/2018 Chair 2 5 10 999
345 XYZ 1/1/2018 Table 5 5 25 1/2/2018 Table 4 3 12 899
345 XYZ 1/1/2018 Chair 2 6 12
I've got a data set like the following - Quantities and Sales $ aggregated by week and product
Week Product Quantity Sales
---- ------- -------- -----
1 12a 6 600
2 12a 4 400
3 12a 3 300
4 12a 1 100
5 12a 3 300
6 12a 1 100
7 12a 4 400
8 12a 6 600
9 12a 2 200
For every week, I need to sum quantity and sales for that week plus the previous 3 weeks
Desired result would be:
Week Product Quantity Sales
---- ------- -------- -----
1 12a 14 1400 --> Week 1 + Week 2 + Week 3 + Week 4 but row labeled Week 1
2 12a 11 1100
I feel like I need a loop to evaluate each week
Use window functions:
select t.*,
sum(quantity) over (partition by product
order by week
rows between current row and 3 following
) as quantity,
sum(sales) over (partition by product
order by week
rows between current row and 3 following
) as sales
from t;
I have a question on this query forever and never figured it out, need some help and thanks in advance for all answers!
I have table 1: Payment (storing historical payment info, I only look at one month data and only type#1 payment, so i need to apply two filters here)
Pymt
ServiceID InsuranceID Amount Month Type
1 A $10 Jun15 1
2 A $15 Jun15 1
3 B $15 Jun15 1
4 C $30 Jun15 1
5 D $50 Jun15 1
Ins
Insurance ID Name
A AAA
B BBB
C CCC
D DDD
Expected Result
Service ID Insurance ID Name Amount Month Type
1 A AAA $10 Jun15 1
2 A AAA $15 Jun15 1
3 B BBB $15 Jun15 1
4 C CCC $30 Jun15 1
5 D DDD $50 Jun15 1
So there are two tasks here:
1. filter table 1 by Jun 15 and Type 1
add a new column"Name" in table 1 to indicate the name of the insurance WITHOUT adding new rows ( it is like a vlookup ) so monthly amount total is the same
I tried differnt joins and WHERE(filter) always added new rows to result which I don't want, please advise.
Thanks!
select p.ServiceID, i.InsuraneID, i.Name, p.Amount, p.Month, p.Type
from Pymt p
inner join Ins i on p.InsuranceID = i.InsuranceID
where p.Month='Jun15' and p.Type=1
I have a table like following
ID DATE ACCT TYPE AMOUNT SEQ CHK# TRC
1 6/5/2014 1234 C 10,000 1 1001
2 6/5/2014 3333 3,000 2 123 1002
3 6/5/2014 4444 5,000 3 234 1003
4 6/5/2014 5555 2,000 4 345 1004
5 6/5/2014 2345 C 3,000 1 1007
6 6/5/2014 5555 2,500 2 255 1008
7 6/5/2014 7777 500 3 277 1009
8 6/6/2014 1234 C 5,000 1 2001
9 6/6/2014 7777 3,000 2 278 2002
10 6/6/2014 8888 2,000 3 301 2003
The rows with TYPE = C are parent rows to the child rows that follow sequentially.
The parent rows do not have CHK# and child rows do have CHK#. Each
parent row has seq# = 1 and child rows have sequential numbers. (if it
matters) From above table, row ID 1 is the parent row to the rows with
ID 2 ~ 4. The AMOUNT on the child rows add up to the parent row's
amount.
Querying for transaction for date of '6/5/2014' on account # 2345 with
the amount of 3,000 - result should be rows with ID 6 and 7.
Is such query possible using MS-SQL 2008? If so, could you let me
know?
Well, based on the data that you have, you can use the id column to find the rows that you want. First, look for the one that has the check in that amount. The look for the subsequent ids with the same group. How do you define the group? That is easy. Take the difference between id and seq. This difference is constant for the parent and child rows.
So, here is goes:
select t.*
from table t
where (t.id - t.seq) = (select t2.id - t2.seq
from table t2
where t2.type = 'C' and
t2.acct = '2345' and
t2.date = '6/5/2014'
) and
t.type is null;