Well, I have the following table (Table1):
------------------------------------
GroupID oDate oDesc
------------------------------------
1 2016-05-01 A
2 2016-05-20 B
3 2017-03-01 C
4 2017-03-28 D
Then I have the following table (Table2):
------------------------------------
AutoID GroupID oItem
------------------------------------
1 1 abc
2 1 def
3 2 ghi
4 2 jkl
5 3 mno
6 4 pql
I want to know all oItem in Table2 that has a link to Table1 in a same year. The result should be like this:
---------------------------
oYear oItem
---------------------------
2016 abc
2016 def
2016 ghi
2016 jkl
2017 mno
2017 pql
Is there any idea how to do this? Thank you.
You can just use inner join to get the desired result.
select datepart(yyyy, t1.odate) as oyear, t2.oitem
from table1 t1
inner join table2 t2 on t1.groupid = t2.groupid
Use Year inbuilt function to extract year from date column. Try this
select Year(Odate) as Oyear,B.oItem
from table1 A inner join table2 B
on A.GroupID = B.GroupID
Related
This is my data:
id
key
nbr of subs
1
ABC
10
1
XXX
3
2
MNO
120
3
ABC
5
3
FGH
110
I need the key for the record (ID) that has the max nbr of subscriptions:
id
key
nbr of subs
1
ABC
10
2
MNO
120
3
FGH
110
I don't mind deleting the extra records, or electing the ones I need and insert them into other table. Any ideas?
SELECT P.Key, MAX(P.[Nbr of Subcriptions])
FROM P
GROUP BY P.Key;
Thank you very much
You need correlated subquery. Try below SQL-
SELECT t1.ID, t1.Key, t1.NBR FROM Table1 as t1
INNER JOIN (SELECT Table1.ID, Max(Table1.NBR) AS MaxOfNBR
FROM Table1 GROUP BY Table1.ID) as t2 ON (t1.NBR = t2.MaxOfNBR) AND (t1.ID = t2.ID);
I have 2 tables:
Table1 - Criteria
Office_ID Bus_Stream Bus_Criteria Crit_Value
1 ABC 0 20
1 ABC 1 21
1 ABC 2 7
2 ABC 0 15
2 ABC 1 12
2 ABC 2 21
3 XYZ 1 17
3 XYZ 2 3
Table2 - Limit
Bus_Stream GroupID TypeID SubgroupID Bus_Limit
ABC 20 21 7 50
ABC 15 12 21 100
XYZ 99 17 3 120
I need to create a join that allows me to pull back:
Result
Bus_Stream Office_ID GroupID TypeID SubgroupID Bus_Limit
ABC 1 20 21 7 50
ABC 2 15 12 21 100
XYZ 3 (null) 17 3 120
Essentially, I need to join Table1.Crit_Value based on the following:
Table1.Bus_Criteria Table2
0 GroupID
1 TypeID
2 SubGroupID
with the added complication that if one or two of the 0/1/2 values from Bus_Criteria is missing, the joins will still occur on the remaining criteria.
I have tried a number of combinations of AND/OR on the join to no avail.
Any ideas folks?
This may be what you're after.. use a case statement on the join.
The problem here is your data in t2 isn't normalized, you could also unpivot the 3 data columns in t2 so the join is more natural.
SELECT T2.Bus_Stream, T1.Office_ID, T2.GroupID, T2.TypeID, T2.SubGroupId, T2.bus_Limit
FROM T1
INNER JOIN T2
on T1.Bus_Stream = T2.Bus_Stream
and T1.Crit_value = case when T1.Bus_Critiera = 0 then T2.GroupID
when T1.Bus_Critiera = 1 then T2.TypeID
when T1.Bus_Critiera = 2 then T2.SubGroupID
end
Did you try something like this?
SELECT
t1.*, t2.*
FROM Table1 t1
INNER JOIN Table2 t2 ON
t1.Bus_Stream = t2.Bus_Stream AND
CASE
WHEN t1.Bus_Criteria = 0
THEN t2.GroupID = t1.Crit_Value
WHEN t1.Bus_Criteria = 1
THEN t2.TypeID = t1.Crit_Value
ELSE
t2.SubGroupID = t1.Crit_Value
END
eg
Names
id | name
1 abc
2 efg
Area
id | areaName
3 area1
4 area2
The query should return
id | name | areaid
1 abc 3
1 abc 4
2 efg 3
2 efg 4
This should give the ecpected result:
select a.id, a.name, b.id from names,area
When you want to join all records from table A to all records from table B (i.e. get a Cartesian product, you can use CROSS JOIN:
SELECT Names.id, Names.name, Area.areaid
FROM Names
CROSS JOIN Area
ORDER BY Names.id, Area.areaid
I have Table1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
and a Table2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
I want to get the following Table,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
I.e get the rows from the first table where the ID+Program is not in second table. I am using MS SQL Server express 2012 and I don't want to add any columns to the original databases. Is it possible to do without creating temporary variables?
Several ways to do this, here's one using not exists:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
One possible variant is to use LEFT JOIN:
SELECT
Table1.*
FROM
Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table1.Program = Table2.Program
WHERE
Table2.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.