Select count(*) from multiple tables with null values - sql-server

Table 1 Table 2
Class | VAL Class | VAL
------|----- ------|-----
A | 1 A | 1
A | 1 A | 1
A | 1 A | 1
B | 1 B | 1
A | 1
B | 1
C | 1
If I have two tables as above and I need the result as the following table
Result needed:
Class | T1 | T2
------|-----|-----
A | 4 | 3
B | 2 | 1
C | 1 | 0
I'm trying this query but it's not working.
SELECT [Class],COUNT(VAL) FROM dbo.T1 group by [Class])
UNION
SELECT [Class],COUNT(VAL) FROM dbo.T2 group by [Class])

SELECT COALESCE(a.Class, b.Class) AS [Class], COALESCE(a.T1,0) AS T1, COALESCE(b.T2,0) AS T2
FROM
(SELECT [Class],COUNT(VAL) AS T1 FROM dbo.T1 GROUP BY [Class]) a
FULL OUTER JOIN
(SELECT [Class],COUNT(VAL) AS T2 FROM dbo.T2 GROUP BY [Class]) b
ON a.Class = b.Class
Another possible solution
SELECT
a.[Class]
, COUNT(a.VAL1) AS T1
, COUNT(a.VAL2) AS T2
FROM (
SELECT [Class], VAL AS VAL1, NULL AS VAL2 FROM dbo.T1
UNION ALL
SELECT [Class], NULL AS VAL1, VAL AS VAL2 FROM dbo.T2
) a
GROUP BY
a.Class;

And another approach here
declare #table1 table (class varchar(1) null, val int)
declare #table2 table (class varchar(1) null, val int)
insert into #table1 values ('A', 1)
insert into #table1 values ('A', 1)
insert into #table1 values ('A', 1)
insert into #table1 values ('B', 1)
insert into #table1 values ('A', 1)
insert into #table1 values ('B', 1)
insert into #table1 values ('C', 1)
insert into #table2 values ('A', 1)
insert into #table2 values ('A', 1)
insert into #table2 values ('A', 1)
insert into #table2 values ('B', 1)
select t.Class,
(select count(val) from #table1 where class = t.class),
(select count(val) from #table2 where class = t.class)
from ( select class
from #table1 t1
union
select class
from #table2 t1
) t
this returns
Class T1 T2
A 4 3
B 2 1
C 1 0

Select Class, sum(T1) as T1, sum(T2) AS T2
From
(
Select Class, VAL AS T1, 0 as T2
From Table1
Union All
Select Class, 0 as T1, VAL AS T2
From Table2
) A
group by A.Class

SELECT t1.class,
COUNT(t1.class) AS T1,
X.T2Count AS T2
FROM #table1 t1
OUTER APPLY(
SELECT COUNT(val) AS T2Count
FROM #table2 t2
WHERE t2.class = t1.class
)X
GROUP BY t1.class,X.T2Count
ORDER BY t1.class

Related

Check duplicated value from temp table

I have a source table:
ID Value
1 a
2 b
3 c
4 d
And a temp table #ABCD:
ID Value
1 b
2 a
3 d
I want to update the original table from temp table based on Id match:
Original table:
ID Value
1 b
2 a
3 d (duplicate)
4 d
So now my original table has a duplicate value (d)
So how to get the duplicate value (d) from temp table #ABCD? Before updating.
Query:
CREATE TABLE [dbo].[Original](
[Id] [int] NOT NULL,
[Value] [nchar](10) NOT NULL
)
GO
INSERT INTO [dbo].[Original]
([Id]
,[Value])
VALUES
(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')
GO
CREATE TABLE #ABCD([Id] INT, [Value] nchar(10))
GO
INSERT INTO #ABCD([Id], [Value]) VALUES (1, 'b'), (2, 'a'), (3, 'd')
Result:
Duplicated value:
ID Value
3 d
EDIT
Maybe this is better:
select q2.* from (
select * from #ABCD
union
select * from original where id not in(select id from #ABCD)
)q1
inner join #ABCD q2
on q1.Value = q2.Value
and q1.id<>q2.id
Try this. I did the update in another temp table and evaluate the final result.
DROP TABLE IF EXISTS #TMP
select * into #TMP
from original
update t1
set t1.Value = t2.value
from #TMP t1
inner join #ABCD t2
on t1.id=t2.id
select a.* from #TMP t1
inner join #ABCD t2
on t1.Value=t2.Value
and t1.id<>t2.id

How to find group of rows of a table that match in another table in SQL?

I have two tables in SQL Server,
Declare #Table1 Table ( TID1 INT, TP1 INT)
Declare #Table2 Table ( TID2 INT, TP2 INT)
INSERT INTO #Table1 (TID1,TP1) VALUES (100,1)
INSERT INTO #Table1 (TID1,TP1) VALUES (100,2)
INSERT INTO #Table1 (TID1,TP1) VALUES (100,3)
INSERT INTO #Table2 (TID2,TP2) VALUES (101,1)
INSERT INTO #Table2 (TID2,TP2) VALUES (101,2)
INSERT INTO #Table2 (TID2,TP2) VALUES (101,3)
INSERT INTO #Table2 (TID2,TP2) VALUES (102,1)
INSERT INTO #Table2 (TID2,TP2) VALUES (102,2)
INSERT INTO #Table2 (TID2,TP2) VALUES (103,1)
INSERT INTO #Table2 (TID2,TP2) VALUES (103,2)
INSERT INTO #Table2 (TID2,TP2) VALUES (103,3)
INSERT INTO #Table2 (TID2,TP2) VALUES (103,4)
INSERT INTO #Table2 (TID2,TP2) VALUES (104,2)
INSERT INTO #Table2 (TID2,TP2) VALUES (105,3)
Having Data as :
TID1 TP1
----------- -----------
100 1
100 2
100 3
TID2 TP2
----------- -----------
101 1
101 2
101 3
102 1
102 2
103 1
103 2
103 3
103 4
104 2
105 3
I want to select those records which having exact matching of TP1 column in Table2 TP2 column. EX TID2 having ID 101 will be only in result set
SELECT t2.TID2
FROM #Table2 t2
LEFT JOIN #Table1 t1
ON t2.TP2 = t1.TP1
GROUP BY t2.TID2
HAVING SUM(CASE WHEN t1.TP1 IS NULL THEN 1 ELSE 0 END) = 0 AND
COUNT(*) = (SELECT COUNT(*) FROM #Table1)
Try Like below.
SELECT TID2
FROM #TABLE1 T
RIGHT JOIN #TABLE2 T2
ON T.TP1 = T2.TP2
GROUP BY TID2
HAVING COUNT(T2.TP2) = (SELECT COUNT(*) FROM #TABLE1)
-- you can calculated this in CTEW or sub-query if you do not like to be in variable
DECLARE #MaxRowsCount INT = (SELECT COUNT(*) FROM #Table1);
SELECT T2.[TID2]
FROM #Table2 T2
LEFT JOIN #Table1 T1
ON T2.[TP2] = T1.[TP1]
GROUP BY T2.[TID2]
HAVING
(
-- current count of rows should be the same as the row count from the first table
COUNT(T2.[TP2]) = #MaxRowsCount
)

How to make query to count values from two tables

I have three tables main 'maintable' table and two sub tables 'table1' and 'table2' the main table 'maintable' contains tow columns 'ID' and 'name' like that:
ID name
.... ......
1 Khalid
2 Jone
3 Steve
and the first sub table 'table1' contains 't1ID' and 'column' and 'ID' (foreign key) from 'maintable' like that:
t1ID column ID
...... ....... ....
1 Value 1
2 Value 1
3 Value 1
4 Value 2
and the second sub table 'table2' contains 't2ID' and 'column' and 'ID' (foreign key) from 'maintable' like that:
t2ID column ID
...... ....... ....
1 Value 2
2 Value 1
3 Value 1
4 Value 3
I want to make query to find count of (table1.ID) as A and count of (table2.ID) as B like that:
name A B
...... ... ...
khalid 3 2
Jone 1 1
Steve 0 1
Try this :
select name,
(select count(t1.ID) from table1 t1 where t1.ID = main.ID) as A,
(select count(t2.ID) from table2 t2 where t2.ID = main.ID) as B
from maintable main
Try this out:
;with cte1 as (
SELECT ID, COUNT(1) AS Cnt
FROM table1
GROUP BY ID
), cte2 as (
SELECT ID, COUNT(1) AS Cnt
FROM table2
GROUP BY ID
)
SELECT m.name, ISNULL(cte1.Cnt, 0) AS A, ISNULL(cte2.Cnt, 0) AS B
FROM maintable m
LEFT JOIN cte1 ON cte1.ID = m.ID
LEFT JOIN cte2 ON cte2.ID = m.ID
It can also be done with subqueries, but I like CTEs more (query is more readable).
Try this:
with t0_t1 as (
select
t.id,
t.nm,
count(t1.id) as A
from table0 t
left join table1 t1 on t.id = t1.id
group by t.id, t.nm
)
select t.nm, t.A, count(t2.id) as B
from t0_t1 t
left join table2 t2 on t.id = t2.id
group by t.nm, t.A
Example: http://sqlfiddle.com/#!6/341ff/10
create table table0 (id int, nm varchar(20));
insert into table0 values (1,'Khalid'),(2,'Jone'),(3,'Steve');
create table table1 (t1id int, col varchar(20), id int);
insert into table1 values
(1, 'v', 1), (2, 'v', 1), (3, 'v', 1), (4, 'v', 2);
create table table2 (t2id int, col varchar(20), id int);
insert into table2 values
(1, 'v', 2), (2, 'v', 1), (3, 'v', 1), (4, 'v', 3);
Result:
| nm | A | B |
|--------|---|---|
| Steve | 0 | 1 |
| Jone | 1 | 1 |
| Khalid | 3 | 2 |

Select record from table1 where value from table2

I have 2 tables here:
table1
id name idfrom idto
1 test 2 3
2 test3 1 9
table2
id branch status
2 a from
1 b from
9 c to
3 d to
How do I select branch from table2 and table1 based on status in table2?
I want the result to look like this:
id name branchfrom branchto
1 test a d
2 test3 b c
I answer it doesn't mean I like it.
SELECT id, name, bfrom.branch branchfrom, bto.branch branchto
FROM table1 t1
INNER JOIN (SELECT id, branch
FROM table2
WHERE status = 'from') bfrom
ON t1.idfrom = bfrom.id
INNER JOIN (SELECT id, branch
FROM table2
WHERE status = 'to') bto
ON t1.idto = bto.id;
I use INNER JOIN as sample only. You must adjust with your requirement (which you didn't clearly specify).
Something like the following should do (assuming you're wanting to join on id in both tables):
select t1.id, t1.name, f.branch as branchfrom, t.branch as branchto
from table1 as t1
join table2 as f
on f.id = t1.id
and f.status = 'from'
join table2 as t
on t.id = t1.id
and t.status = 'to'
This should work for you:
select t1.id, t1.name, f.branch as branchfrom, f1.branch as branchto
from table1 as t1
join table2 as f
on t1.idfrom = f.id
join table2 as f1
on t1.idto = f1.id
Please see here for demo: SQL Fiddle Demo
I don't know if this is better or worse than what the other two people have suggested but
select
t1.name,
(select
t2.branch
from
table2 t2
where
t1.idfrom = t2.id
) as branchfrom,
(select
t2.branch
from
table2 t2
where
t1.idto = t2.id
) as branchto
from
table1 t1
Here is a fiddle
Use this Code:
CREATE TABLE #table1 (
id int,
name varchar(10),
idfrom int,
idto int
)
CREATE TABLE #table2 (
id int,
branch char,
statuss varchar(10)
)
INSERT INTO #table1
VALUES (1, 'test', 2, 3)
INSERT INTO #table1
VALUES (2, 'test3', 1, 9)
INSERT INTO #table2
VALUES (2, 'a', 'From')
INSERT INTO #table2
VALUES (1, 'b', 'From')
INSERT INTO #table2
VALUES (9, 'c', 'to')
INSERT INTO #table2
VALUES (3, 'd', 'to')
SELECT
a.id,
a.name,
(SELECT
b.branch
FROM #table2 b
WHERE a.idfrom = b.id
AND b.statuss = 'FROM')
AS BranchFrom,
(SELECT
b.branch
FROM #table2 b
WHERE a.idto = b.id
AND b.statuss = 'to')
AS BranchTo
FROM #table1 a

Case-when w.r.to Count in sql server

I am getting error in the below sql query.
if count is >1 i need to execute when statement, if not else statement.
SELECT CASE
WHEN (COUNT(VALUE) FROM TABLE1 WHERE ID=111)>1 )
THEN
SELECT VALUE FROM TABLE2 WHERE ID=111
ELSE
SELECT 2
Please help
Try this:
if (select count(*) from table1 where id = 111 group by id) > 1
select value from table2 where id = 111
else
select 2
Demo
The same thing, written using case when...
select
case
when (select count(*) from table1 where id = 111 group by id) > 1 then value
else 2
end
from table2
where id = 111
Try this sample code
DECLARE #Table1 TABLE
(
id INT,
value INT
)
INSERT #Table1
VALUES(1,
10)
INSERT #Table1
VALUES(2,
20)
DECLARE #TABLE2 TABLE
(
id INT,
c2 INT
)
INSERT #TABLE2
VALUES(1,
11 )
INSERT #TABLE2
VALUES(2,
22 )
SELECT CASE
WHEN (SELECT count(value)
FROM #Table1) > 0 THEN (SELECT T2.c2
FROM #TABLE2 T2
WHERE T1.id = T2.id)
ELSE (SELECT T2.id
FROM #TABLE2 T2
WHERE T1.id = T2.id)
END
FROM #Table1 t1
IF EXISTS(SELECT TOP 1 1 FROM TABLE1 WHERE ID='111' HAVING COUNT(VALUE)>1)
BEGIN
SELECT VALUE FROM TABLE2 WHERE ID=111
END
ELSE
BEGIN
SELECT 2
END

Resources