I have two tables as follows:
Table A
Docnumber Line Amount Doctype
1000 1 100 3
1000 2 200 3
1001 1 300 5
Table B
Docnumber Debit Credit Account
1000 100 0 5410
1000 200 0 5560
1001 0 300 6790
I'm trying to create a select which looks like this:
Docnumber Line Amount Account
1000 1 100 5410
1000 2 200 5560
1001 1 300 6790
So, logically, I'm trying to figure out a way to join A.Amount to B.Debit where A.Doctype = 3 and join A.Amount to B.Credit where A.Doctype = 5
As you can see, I'm a novice but any help would be greatly appreciated
Assuming SQL Server. Also assuming that Docnumber is intended to play a role in your JOIN as well, despite not being explicitly stated in the question.
You can use any conditions on the JOIN that you want:
SELECT A.Docnumber, A.Line, A.Amount, B.Account
FROM A JOIN B
ON A.Docnumber = B.Docnumber
AND ((A.Doctype = 3 AND A.Amount = B.Debit)
OR (A.Doctype = 5 AND A.Amount = B.Credit))
ORDER BY A.Docnumber, A.Line;
Alternatively, you could put it into the WHERE clause, which is probably more clear:
SELECT A.Docnumber, A.Line, A.Amount, B.Account
FROM A JOIN B ON A.Docnumber = B.Docnumber
WHERE (A.Doctype = 3 AND A.Amount = B.Debit) OR (A.Doctype = 5 AND A.Amount = B.Credit)
ORDER BY A.Docnumber, A.Line
Actually easier than you might think!
SELECT *
FROM A
INNER JOIN B
ON (A.Doctype = 3 AND B.Debit = A.Amount)
OR (A.Doctype = 5 AND B.Credit= A.Amount)
Related
How to exclude all records for PUID from the result when A.PUID = B.EUID and A.PID <> B.LID
Table A
PUID PID PCODE
100 1003 RG
200 1006 CA
200 1007 CA
300 1008 RG
Table B
EUID LID ECODE
100 1003 RG
100 1004 RG
100 1005 RG
200 1006 CA
300 1009 RG
Expected Result
PUID PID ECODE
300 1008 RG
Tried with not exists, didn't work. screen print for tables attached
You can try the below - DEMO
select A.* from A
join
(
select A.puid
from A left join B on A.puid=B.puid and A.pid=B.pid
group by A.puid having sum(case when B.pid is null then 0 else 1 end)=0
)X on A.puid=X.puid
Have a look at join logic for the database, if you join on a column you can take those that are the same (contains) or those that null (does not contain) there are different types of joins that can be used in combination with a where statement and in your homework assignment your mentors likes you to understand and apply them.
Open google and look at joins, or look at this https://www.w3schools.com/sql/sql_join.asp
I think I would use a left join and window functions:
select a.*
from (select a.*,
count(b.euid) over (partition by a.puid) as num_matches
from a left join
b
on a.puid = b.euid and a.pid = b.lid
) a
where num_matches = 0;
Here is a db<>fiddle.
i need to write one efficient query on the 2 tables below, with the conditions follow the tables:
First Table
CityCode CustomerID AccountID
Paris 1 1
Roma 2 1
London 1 2
Paris 3 2
Roma 4 3
Berlin 5 4
Second Table
Credit_card_ind Credit AccountID
0 1000 1
1 5000 2
0 2300 3
1 30000 4
0 - doesn't have card
1 - has a card
In the query we need the following conditions:
1. all customers which their credit is above 5000
2. not showing accounts where one of their customers doesn't have credit card
3. all the accounts where their credit is below 30000
4. not showing accounts where one of their customers is from 'Roma'
5. showing accounts with more than 1 customers.
(*) - there might be no records return.
i wrote the query as follow and i wanted to confirm its the best way to do so, where the intention is to reduce the number of times we approach the tables and doing Joins:
Select c.AccountID,
c.CustomerID
From Customers as c
Join credit_cards as ca on c.AccountID = ca.AccountID
Where ca.credit > 5000 And ca.credit < 30000
And c.AccountID not in (
Select Distinct newTBL.AccountID
From (
Select c1.CustomerID,
c1.AccountID
From customers as c1
Join credit_cards as ca1 on c.AccountID = ca.AccountID
Where ca1.credit_card_ind = 0
Or c1.CityCode like ‘Roma’
) as newTBL
)
And c.AccountID in (
Select newCus.AccountID
From (
Select AccountID,
Count(CustomerID) as [Num_of_Cus]
From customers
Group by AccountID
) as newCus
Where newCus.[Num_of_Cus] > 1
)
First, you can simplify your query by removing the nested subqueries:
Select c.AccountID, c.CustomerID
From Customers c Join
credit_cards ca
on c.AccountID = ca.AccountID
Where ca.credit > 5000 And ca.credit < 30000 And
c.AccountID not in (Select c1.CustomerID
From customers c1 Join
credit_cards ca1
on c.AccountID = ca.AccountID
Where ca1.credit_card_ind = 0 Or c1.CityCode like 'Roma'
) And
c.AccountID in (Select AccountID
From customers
Group by AccountID
Having Count(CustomerID) > 1
);
This may help. You can also write this using window functions, which are probably more efficient. I think the following query captures your original conditions:
select c.AccountID, c.CustomerID
from (select c.*, count(*) over (partition by c.accountid) as cnt,
max(case when c.CityCode like 'Roma' then 1 else 0 end) as cnt_Roma
from customers c
) c join
credit cr
on c.accountid = cr.accountid
where ca.credit > 5000 And ca.credit < 30000 and
c.cnt > 0 and c.cnt_Roma = 0 and
ca.credit_card_ind <> 0;
Try this.
SELECT cs.AccountID
FROM customer cs
JOIN credit cr
ON cs.AccountID = cr.AccountID
WHERE CityCode <> 'Roma'
AND Credit_card_ind = 1
AND Credit > 5000
AND Credit < 30000
GROUP BY cs.AccountID
HAVING Count(cs.CustomerID) > 1
I Have 3 tables Customer, Bank and BankTransaction.
On My View I want to display each customer with their Balance in the bank account.
Here is my tables
Customer
Id Name
---------
1 John
2 Jack
Bank
Id CustomerId BankName
----------------------------------
1 1 HSBC
2 2 HSBC
BankTransaction
Id BankID MoneyIn MoneyOut Balance
---------------------------------------------
1 1 1000 0 1000
2 1 0 500 500
3 2 2000 0 2000
4 2 2000 0 4000
5 2 1000 0 5000
Now I want to Display following data view query
John 500
Jack 5000
Last Balance of each customer
Try this
SELECT A.Name,
Sum(C.MoneyIN) - Sum(C.MoneyOut) AS Balance
FROM #Customer A
JOIN #Bank B ON A.Id = B.id
JOIN #BankTransaction C ON B.Id = C.BankID
GROUP BY A.Name
Using Window function you can get the result. Try this.
;WITH cte
AS (SELECT Row_number() OVER (partition BY b.id ORDER BY a.id DESC) rn,
c.Name,
a.balance
FROM BankTransaction a
JOIN bank b ON a.BankID = b.Id
JOIN Customer c ON c.Id = b.CustomerId)
SELECT name, balance
FROM cte
WHERE rn = 1
I have a table A as
ID Settle Date
1 1/15/2013
2 2/15/2013
and Table B as
ID From Rate
1 1/1/2013 1.5
1 2/1/2013 2
2 1/1/2013 4
2 2/1/2013 5
I want a join such that i get
ID Settle Date Rate
1 1/15/2013 1.5
2 2/15/2013 5
Basically, want to get the rate applicable for the date. Please advise
Use BETWEEN in your join:
select
a.ID
SettleDate,
Rate
FROM TableA a
INNER JOIN TableB b
ON a.SettleDate BETWEEN b.From AND b.To
AND a.ID = b.ID
OK Adjusting for your "small" change (which isn't small):
select
a.ID
SettleDate,
Rate
FROM TableA a
INNER JOIN TableB b
ON a.ID = b.ID
AND b.[From] = (SELECT MAX([From]) FROM TableB WHERE ID = a.ID AND From <= a.SettleDate)
I have two tables
TableA
Custid prodid Amt1
1 10 200
2 20 300
3 10 400
4 30 300
5 20 400
6 30 200
TableB
prodid Amt2
10 100
20 200
30 300
and I want to sum of all amount group by prodid. Can anyone give me both ansi and Tsql queries?
SELECT a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum
FROM TableA a JOIN TableB b ON (a.prodid = b.prodid)
GROUP BY a.prodid;
What about that? :-)
SELECT SUM(a.Amt1),SUM(b.Amt2)
FROM (TableA AS a INNER JOIN TableB AS b ON
a.prodid = b.prodid)
GROUP BY a.prodid
I believe this will work anywhere:
SELECT a.Custid,a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum FROM TableA a JOIN TableB b ON (a.prodid = b.prodid) GROUP BY a.Custid,a.prodid;
try this... I think it will also work if some product id not in TABLEB
SELECT a.prodid,SUM(a.Amt1),SUM(b.Amt2)
FROM TableA AS a left outer JOIN TableB AS b ON a.prodid = b.prodid
GROUP BY a.prodid