Join table and applying filter? - sql

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

Related

Is it possible to perform sequential row calculations in presto sql?

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.

Include "0" results in COUNT(*) aggregate

Good morning, I've searched in the forum one doubt that I have but the results that I've seen didn't give me a solution.
I have two tables.
CARS:
Id Model
1 Seat
2 Audi
3 Mercedes
4 Ford
BREAKDOWNS:
IdBd Description Date Price IdCar
1 Engine 01/01/2020 500 € 3
2 Battery 05/01/2020 0 € 1
3 Wheel's change 10/02/2020 110,25 € 4
4 Electronic system 15/03/2020 100 € 2
5 Brake failure 20/05/2020 0 € 4
6 Engine 25/05/2020 400 € 1
I wanna make a query that shows the number of breakdowns by month with 0€ of cost.
I have this query:
SELECT Year(breakdowns.[Date]) AS YEAR, StrConv(MonthName(Month(breakdowns.[Date])),3) AS MONTH, Count(*) AS [BREAKDOWNS]
FROM cars LEFT JOIN breakdowns ON (cars.Id = breakdowns.IdCar AND breakdowns.[Price]=0)
GROUP BY breakdowns.[Price], Year(breakdowns.[Date]), Month(breakdowns.[Date]), MonthName(Month(breakdowns.[Date]))
HAVING ((Year([breakdowns].[Date]))=[Insert a year:])
ORDER BY Year(breakdowns.[Date]), Month(breakdowns.[Date]);
And the result is (if I put year '2020'):
YEAR MONTH BREAKDOWNS
2020 January 1
2020 May 1
And I want:
YEAR MONTH BREAKDOWNS
2020 January 1
2020 February 0
2020 March 0
2020 May 1
Thanks!
The HAVING condition should be in WHERE (otherwise it changes the Outer to an Inner join). But as long as you don't use columns from cars there's no need to join it.
To get rows for months without a zero price you should switch to conditional aggregation (Access doesn't support Standard SQL CASE, but IIF?).
SELECT Year(breakdowns.[Date]) AS YEAR,
StrConv(MonthName(Month(breakdowns.[Date])),3) AS MONTH,
SUM(CASE WHEN breakdowns.[Price]=0 THEN 1 ELSE 0 END) AS [BREAKDOWNS]
FROM breakdowns
JOIN cars
ON (cars.Id = breakdowns.IdCar)
WHERE ((Year([breakdowns].[Date]))=[Insert a year:])
GROUP BY breakdowns.[Price], Year(breakdowns.[Date]), Month(breakdowns.[Date]), MonthName(Month(breakdowns.[Date]))
ORDER BY Year(breakdowns.[Date]), Month(breakdowns.[Date]

Combine 2 queries into 1 table with user entering the parameters twice for both queries

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

Find sequential child rows that have amounts which add up to parent row's amount

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;

SQL to generate sequence for a set of columns depending on a select criteria

I have an accounts journal entry table data like this:
TxnID VchNo SeqNo Date AccountName TxnAmt CrDr
103931 0 0 1-Apr-2011 ADVERTISEMENT 3600 D
103932 0 0 1-Apr-2011 MAYA OUTDOOR ADVERTISING 3600 C
103933 0 0 1-Apr-2011 NILESH BRANKS 850500 D
103934 0 0 1-Apr-2011 T.D.S ON CONTRACT A/C 45600 C
103935 0 0 1-Apr-2011 CHEQUE A/C 185000 C
103936 0 0 1-Apr-2011 PROFESSIONAL CHARGES 619900 C
103937 0 0 1-Apr-2011 ADVERTISEMENT (HOARDING) 77210 C
103938 0 0 1-Apr-2011 A111 COMMUNICATIONS 77210 D
103939 0 0 1-Apr-2011 SALARY A/C 55500 D
103940 0 0 1-Apr-2011 T JOHN 25000 C
103941 0 0 1-Apr-2011 PHILIPS 30500 C
TxnAmt might be Credit (C) or Debit (D). Please note that for TxnID 103931 and 103932 Debit and credit are annulled. For those pairs I want to generate a unique VchNo (Voucher No), say 101 with sequence No 1 and 2 respectively.
For TxnID 103933 there is a debit amount (850500) which is equal to credit amounts of TxnID 103934, 103935 and 103936 (45600 + 185000 + 619900). I want to set VchNo as 102 for these a transactions with sequence no 1, 2, 3, 4 respectively as shown in the following result set:
TxnID VchNo SeqNo Date AccountName TxnAmt CrDr
103931 101 1 1-Apr-2011 ADVERTISEMENT 3600 D
103932 101 2 1-Apr-2011 MAYA OUTDOOR ADVERTISING 3600 C
103933 102 1 1-Apr-2011 NILESH BRANKS 850500 D
103934 102 2 1-Apr-2011 T.D.S ON CONTRACT A/C 45600 C
103935 102 3 1-Apr-2011 CHEQUE A/C 185000 C
103936 102 4 1-Apr-2011 PROFESSIONAL CHARGES 619900 C
103937 103 1 1-Apr-2011 ADVERTISEMENT (HOARDING) 77210 C
103938 103 2 1-Apr-2011 A111 COMMUNICATIONS 77210 D
103939 104 1 1-Apr-2011 SALARY A/C 55500 D
103940 104 2 1-Apr-2011 T JOHN 25000 C
103941 104 3 1-Apr-2011 PHILIPS 30500 C
Assume the First Voucher Sequence No be 101, I want to update the VchNo and SeqNo as shown above.
Kindly help me to achieve this.
You can use a CTE (Common Table Expression) with a ROW_NUMBER and PARTITION BY clause:
;WITH YourData AS
(SELECT
(list of columns),
ROW_NUMBER() OVER(PARTITION BY VchNo ORDER BY TxnID DESC) AS 'SeqNo')
FROM
dbo.YourTable
WHERE (condition)
)
SELECT *
FROM YourData
Basically, the CTE partitions your data by VchNo - i.e. each VchNo defines a "partition" of data. The ROW_NUMBER function will sequentially number all rows, starting at each in each partition.
Just reread your question and it seems you don't have a VchNo value to begin with..... I don't completely understand is how you want to create your VchNo - these need to be based on some criteria, too - and I don't see anything in your original data that would "hold together" those values....
But you could definitely create two ROW_NUMBER function inside a single CTE, or you might need to do a first CTE for determining the VchNo and the a second one to determine the SeqNo based on that VchNo from the first CTE.