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
Related
I have the table 'Table01' which contains the keys that should be mandatory:
id
1
2
3
4
And I also have the table 'Table02' which contains the records to be filtered:
id
customer
weight
1
a
100
2
a
300
3
a
200
4
a
45
1
b
20
2
b
100
3
b
17
1
c
80
4
c
90
2
d
30
3
d
30
4
d
50
So I want to identify which are the mandatory id's that the table 'Table02' does not have, and in turn identify which is the 'customer' of each id's that the table 'Table02' does not have.
The resulting table should look like this:
customer
id
b
4
c
2
c
3
d
1
What I have tried so far is a 'rigth join'.
proc sql;
create table table03 as
select
b.id
from table02 a
right join table01 b
on a.id=b.id
where a.id is null;
run;
But that query is not identifying all the id's that should be mandatory.
I hope someone can help me, thank you very much.
here is one way:
select cl.customerid , a.id
from
Table1 a
cross join
( select customerid
from table2
group by customerid
) cl
where not exists ( select 1 from table2 b
where b.customerid = cl.customerid
and b.id = a.id
)
You can use an EXCEPT between two sub-selects. The first creates a matrix of all possibilities, and the except table is a selection of the extant customers.
Example:
data ids;
do id = 1 to 4; output; end;
run;
data have;
input id customer $ weight;
datalines;
1 a 100
2 a 300
3 a 200
4 a 45
1 b 20
2 b 100
3 b 17
1 c 80
4 c 90
2 d 30
3 d 30
4 d 50
run;
proc sql;
create table want(label='Customers missing some ids') as
select matrix.*
from
(select distinct have.customer, ids.id from have, ids) as matrix
except
(select customer, id from have)
;
quit;
If you are doing it in SQL server. Something like #eshirvana above posted, but also you can use with cte:
;with cte as
(
SELECT t1.id, t2.Customer
FROM Table01 t1
cross join (select distinct customer from Table02)
)
SELECT a.customer, a.id FROM cte a
LEFT JOIN Table02 b
ON a.id=b.id AND a.customer=b.customer
where b.id is null
I have 2 tables A, B
Table A:
ID Value1 Value2
------------------------
1000 10 25
1001 4 12
1002 2 6
1003 1 8
Table B:
ID Value3 Value4
------------------------
1000 51 12
1003 3 10
What I need is to show the below result:
ID Value1 Value2 Value3 Value4
-----------------------------------------
1000 10 25 51 12
1001 4 12 NULL NULL
1002 2 6 NULL NULL
1003 1 8 3 10
The query I've tried:
SELECT I.AgentId AS AGENT
,COUNT(I.Indice) AS [Number of Inbound calls]
,AVG(I.WrapupDuration) AS [AVG Wrapup IN]
,COUNT(O.Indice) AS [Number of Out calls]
,AVG(O.WrapupDuration) AS [AVG Wrapup Out]
FROM [HN_Ondata].[dbo].[vwInboundCalls] I
LEFT JOIN [HN_Ondata].[dbo].vwOutboundCalls O
ON I.AgentId = O.AgentId
GROUP BY I.AgentId
You just need a LEFT JOIN and a query like below, you don't need another table:
SELECT *
FROM TABLE_A A
LEFT JOIN TABLE_B B ON A.ID = B.ID
You have to use LEFT JOIN to achieve your goal.
SELCT * FROM TABELA LEFT JOIN TABLEB ON TABLEA.ID = TABLEB.ID
You should use left join
SELECT
*
FROM
TABLEA T1
LEFT JOIN
TABLEB T2
ON
T1.ID = T2.ID
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
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)
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 tabeleA and tableB..
In table A, I have column(Name)
In table B, I have column(Amount)
Table A:
cat_id cat_name
1 Man
2 Women
3 General
Table B:
cat_id cat_price
1 12
1 18
1 34
1 23
2 21
3 31
1 21
3 56
Now in these two tables I have name and price which are linked by cat_id..
Now I want to display Name and price for that particular name and the price should be the total for particular categories......
Something like this:
TableNew:
cat_name cat_price
Man 104
Woman 21
General 87
please help..
thanks...
SELECT
tA.cat_name, SUM(tB.cat_price) AS price
FROM TableA tA
INNER JOIN TableB tB
ON tA.cat_id=tB.cat_id
GROUP BY tA.cat_id
select a.cat_name,
sum(b.cat_price) price
from tablea a
inner join tableb b
on a.cat_id = b.cat_id
group by a.cat_name
INNER JOIN.
GROUP BY Sql Server version in absence of Wiki Sql article.
select T1.cat_name, T2.total
from TableA T1
NATURAL JOIN (SELECT cat_id, sum(cat_price) as total
FROM TableB GROUP BY cat_id) T2
You can use an INNER JOIN for this:
SELECT
table_a.cat_name,
SUM(table_b.cat_price) AS price
FROM
table_a
INNER JOIN
table_b
ON
table_b.cat_id = table_a.cat_id
GROUP BY
table_a.cat_name;
Hope this helps.