How to get Count of Joined Rows - sql-server

There is this SQL Statement
SELECT t1.Name
,Count(t2.SubID) Totals -- I don't know how to do it.
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
Thanks.

Guessing... change the JOIN and add GROUP BY
SELECT t1.Name
,Count(t2.SubID) AS Totals
FROM Table t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name
The LEFT OUTER JOIN allows you to find count zero rows per t1.Name

SELECT t1.Name, Count(t2.SubID) Totals
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name

Related

Different Filters for Different Columns

I am relatively new to SQL and I have the following question. I have the following code:
Select * from table1
LEFT JOIN table2 ON table1.name = table2.name and table1.id = table2.id
LEFT JOIN (SELECT id FROM table2 GROUP BY id) newtable ON table1.id = newtable.id
As both left joins uses data from the same table, is it possible to combine the two joins into one? How would the filters work in this case?
If your goal just to join table2 based on distinct values, then you can use WHERE and GROUP BY:
Select
*
from table1 t1
LEFT JOIN table2 t2
ON t1.name = t2.name and t1.id = t2.id
WHERE t1 id in (SELECT s2.id FROM table2 s2 GROUP BY s2.id)

Join two tables to get a third column of specific format

Hi I have Table1 and Table2 in sqlserver. I want the result like in Table3 as can be seen in the image below.
This query gave me the answer finally:
SELECT COALESECE(t1.Label, t2.label) AS label,
t1.Value AS Table1_Value,
t2.Value AS Table2_Value
FROM Table_1 t1
FULL OUTER JOIN Table_2 t2
ON t1.Label = t2.Label
Try the below Query
SELECT COALESCE(T1.LABEL,T2.LABEL) AS LABEL,T1.VALUE,T2.VALUE
FROM TAB1 T1 FULL OUTER JOIN TAB2 T2
ON T1.LABEL=T2.LABEL
Using This query:
SELECT t1.Label,
t1.Value AS Table1_Value,
t2.Value AS Table2_Value
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.Label = t2.Label

Missing data from converting OR condtion in Join to Another JOIN

Few days back I asked how to change OR condtion (left join) into another left join. Here is the code below :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
OR (
t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t2.ID IS NULL
)
I converted to OR condition like below:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)
But i am not getting records for second left join in seperate row.
With OR condition result are like :
T1.ID T2.ID
1 a
2 b
3 c
4 d
But with second left join its lile below:
T1.ID T2.ID
1 a
2 b
3 c
4rth record is from second left join. How can i bring that in left join. I dont have option for using UNION ALl as per company standards.
Please help.
Thanks
First of all please notice that you're using t2 instead of t3 in the second left join :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t3.ID IS NULL
)
Anyhow, without understanding what you'r trying to do and how do you want to see your result, it's hard to answer you.
You're using left join which just gives you everything from left result set. Since, you want data from t3 which is in right, change second left join to be either right join or full join, depending on your requirements. One of the following query can give you correct result:
Editing 14-03-2015
SELECT *
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID
FULL OUTER JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)

Opposite Of An Inner Join Query

Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);

Change Sql "Not In" To "Left Outer Join"

I don't want to use "not in" this sql query. How can I do it? thanks
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T2.Sno not in (select Gid from T3 (nolock))
Assuming there is no row in T2 where Sno is null and in T3 where Gid is null:
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 WITH (nolock)
INNER JOIN T2 WITH (nolock)
LEFT JOIN T3 WITH (NOLOCK)
ON T2.Sno = T3.Gid
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and T3.Gid IS NULL
If you have multiple T3 rows per T2.Sno = T3.Gid, you'll need DISTINCT in a JOIN.
Without DISTINCT, it's a different query
With DISTINCT, it's an extra step.
I'd use NOT EXISTS which avoids this.
SELECT
T2.Sno,
T2.Name,
T1.description,
T2.UserCode
FROM
Table1 AS T1 (nolock)
INNER JOIN T2 (nolock)
ON T1.UserCode = T2.UserCode
WHERE
g.xid= #p_xid
and not exists (select * from T3 (nolock) where T3.Gid = T2.Sno)

Resources