How to make query to count values from two tables - sql-server

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 |

Related

Suggestions on T-SQL query [duplicate]

Assuming I have a table containing the following information:
FK | Field1 | Field2
=====================
3 | ABC | *NULL*
3 | *NULL* | DEF
is there a way I can perform a select on the table to get the following
FK | Field1 | Field2
=====================
3 | ABC | DEF
Thanks
Edit: Fix field2 name for clarity
Aggregate functions may help you out here. Aggregate functions ignore NULLs (at least that's true on SQL Server, Oracle, and Jet/Access), so you could use a query like this (tested on SQL Server Express 2008 R2):
SELECT
FK,
MAX(Field1) AS Field1,
MAX(Field2) AS Field2
FROM
table1
GROUP BY
FK;
I used MAX, but any aggregate which picks one value from among the GROUP BY rows should work.
Test data:
CREATE TABLE table1 (FK int, Field1 varchar(10), Field2 varchar(10));
INSERT INTO table1 VALUES (3, 'ABC', NULL);
INSERT INTO table1 VALUES (3, NULL, 'DEF');
INSERT INTO table1 VALUES (4, 'GHI', NULL);
INSERT INTO table1 VALUES (4, 'JKL', 'MNO');
INSERT INTO table1 VALUES (4, NULL, 'PQR');
Results:
FK Field1 Field2
-- ------ ------
3 ABC DEF
4 JKL PQR
There are a few ways depending on some data rules that you have not included, but here is one way using what you gave.
SELECT
t1.Field1,
t2.Field2
FROM Table1 t1
LEFT JOIN Table1 t2 ON t1.FK = t2.FK AND t2.Field1 IS NULL
Another way:
SELECT
t1.Field1,
(SELECT Field2 FROM Table2 t2 WHERE t2.FK = t1.FK AND Field1 IS NULL) AS Field2
FROM Table1 t1
There might be neater methods, but the following could be one approach:
SELECT t.fk,
(
SELECT t1.Field1
FROM `table` t1
WHERE t1.fk = t.fk AND t1.Field1 IS NOT NULL
LIMIT 1
) Field1,
(
SELECT t2.Field2
FROM `table` t2
WHERE t2.fk = t.fk AND t2.Field2 IS NOT NULL
LIMIT 1
) Field2
FROM `table` t
WHERE t.fk = 3
GROUP BY t.fk;
Test Case:
CREATE TABLE `table` (fk int, Field1 varchar(10), Field2 varchar(10));
INSERT INTO `table` VALUES (3, 'ABC', NULL);
INSERT INTO `table` VALUES (3, NULL, 'DEF');
INSERT INTO `table` VALUES (4, 'GHI', NULL);
INSERT INTO `table` VALUES (4, NULL, 'JKL');
INSERT INTO `table` VALUES (5, NULL, 'MNO');
Result:
+------+--------+--------+
| fk | Field1 | Field2 |
+------+--------+--------+
| 3 | ABC | DEF |
+------+--------+--------+
1 row in set (0.01 sec)
Running the same query without the WHERE t.fk = 3 clause, it would return the following result-set:
+------+--------+--------+
| fk | Field1 | Field2 |
+------+--------+--------+
| 3 | ABC | DEF |
| 4 | GHI | JKL |
| 5 | NULL | MNO |
+------+--------+--------+
3 rows in set (0.01 sec)
I had a similar problem. The difference was that I needed far more control over what I was returning so I ended up with an simple clear but rather long query. Here is a simplified version of it based on your example.
select main.id, Field1_Q.Field1, Field2_Q.Field2
from
(
select distinct id
from Table1
)as main
left outer join (
select id, max(Field1)
from Table1
where Field1 is not null
group by id
) as Field1_Q on main.id = Field1_Q.id
left outer join (
select id, max(Field2)
from Table1
where Field2 is not null
group by id
) as Field2_Q on main.id = Field2_Q.id
;
The trick here is that the first select 'main' selects the rows to display. Then you have one select per field. What is being joined on should be all of the same values returned by the 'main' query.
Be warned, those other queries need to return only one row per id or you will be ignoring data
if one row has value in field1 column and other rows have null value then this Query might work.
SELECT
FK,
MAX(Field1) as Field1,
MAX(Field2) as Field2
FROM
(
select FK,ISNULL(Field1,'') as Field1,ISNULL(Field2,'') as Field2 from table1
)
tbl
GROUP BY FK
My case is I have a table like this
---------------------------------------------
|company_name|company_ID|CA | WA |
---------------------------------------------
|Costco | 1 |NULL | 2 |
---------------------------------------------
|Costco | 1 |3 |Null |
---------------------------------------------
And I want it to be like below:
---------------------------------------------
|company_name|company_ID|CA | WA |
---------------------------------------------
|Costco | 1 |3 | 2 |
---------------------------------------------
Most code is almost the same:
SELECT
FK,
MAX(CA) AS CA,
MAX(WA) AS WA
FROM
table1
GROUP BY company_name,company_ID
The only difference is the group by, if you put two column names into it, you can group them in pairs.
SELECT Q.FK
,ISNULL(T1.Field1, T2.Field2) AS Field
FROM (SELECT FK FROM Table1
UNION
SELECT FK FROM Table2) AS Q
LEFT JOIN Table1 AS T1 ON T1.FK = Q.FK
LEFT JOIN Table2 AS T2 ON T2.FK = Q.FK
If there is one table, write Table1 instead of Table2

MSSQL: How to remove duplicate column values from different tables?

How to remove duplicate column values from different tables?
Given the following tables without foreign key reference:
Table A
Id IdentifierString
-------------------
1 String A
2 String B
3 String C
Table B
Id IdentifierString
-------------------
1 String A
2 String C
3 String D
I want to remove all duplicate column values:
Table A
Id IdentifierString
-------------------
1 NULL
2 String B
3 NULL
Table B
Id IdentifierString
-------------------
1 NULL
2 NULL
3 String D
How is this doable?
This could be done using UNION ALL, HAVING(COUNT ) and UPDATE statement as below:
CREATE TABLE Table1 (Id INT, IdentifierString VARCHAR (20));
INSERT INTO Table1 (Id, IdentifierString) VALUES
(1, 'String A'),
(2, 'String B'),
(3, 'String C');
CREATE TABLE Table2 (Id INT, IdentifierString VARCHAR (20));
INSERT INTO Table2 (Id, IdentifierString) VALUES
(1, 'String A'),
(2, 'String C'),
(3, 'String D');
DECLARE #DuplicateEntries TABLE (IdentifierString VARCHAR(20));
INSERT INTO #DuplicateEntries (IdentifierString)
SELECT IdentifierString FROM (
SELECT IdentifierString FROM Table1
UNION ALL
SELECT IdentifierString FROM Table2
) A
GROUP BY IdentifierString
HAVING COUNT(IdentifierString) > 1;
UPDATE T1
SET T1.IdentifierString = NULL
FROM Table1 T1
JOIN #DuplicateEntries D ON D.IdentifierString = T1.IdentifierString;
UPDATE T2
SET T2.IdentifierString = NULL
FROM Table2 T2
JOIN #DuplicateEntries D ON D.IdentifierString = T2.IdentifierString;
SELECT * FROM Table1;
SELECT * FROM Table2;
Please find the working demo on db<>fiddle
Here's your dml script to remove duplicates.
delete from table1 where id in (select id from (
select id, t1.IdentifierString, t2.IdentifierStringfrom table1 t1
left join table2 t2 on t2.IdentifierString= t1.IdentifierString) as t3
where isnull(t1.IdentifierString, '') = '' or isnull(t2.IdentifierString, '') = '')
delete from table2 where id in (select id from (
select id, t1.IdentifierString, t2.IdentifierStringfrom table2 t1
left join table1 t2 on t2.IdentifierString= t1.IdentifierString) as t3
where isnull(t1.IdentifierString, '') = '' or isnull(t2.IdentifierString, '') = '')

Select count(*) from multiple tables with null values

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

Append parent table column value to child table column in sql

Assume I have two tables, as below:
Table 1
ID Name
------------------
1 Adam
2 Eve
Table 2
ID FK_ID (Table 1) Name
--------------------------------------------
1 1 Chris
2 1 Austin
3 1 Steve
4 2 Charles
5 2 Erik
6 2 Austin
Required table as Result
ID Name
-----------------
1 Chris
2 Austin Adam
3 Steve
4 Charles
5 Erik
6 Austin Eve
Notice here, in the resulting table I want repeated "Austin" appended with "Adam/Eve" from parent table (i.e. Table 1), depending on "FK_ID". I want to do this in SQL. Any idea/help would really be appreciated.
You can use GROUP BY Name to check names with count(*) > 1 and then do a LEFT JOIN to append T1 names appropriately
Sample Data
CREATE TABLE Table1
([ID] int, [Name] varchar(4));
INSERT INTO Table1
([ID], [Name])
VALUES
(1, 'Adam'),
(2, 'Eve');
CREATE TABLE Table2
([ID] int, [FK_ID] int, [Name] varchar(7));
INSERT INTO Table2
([ID], [FK_ID], [Name])
VALUES
(1, 1, 'Chris'),
(2, 1, 'Austin'),
(3, 1, 'Steve'),
(4, 2, 'Charles'),
(5, 2, 'Erik'),
(6, 2, 'Austin');
Query
SELECT
T2.ID,
T2.Name + CASE WHEN T3.Name IS NOT NULL THEN ' ' + T1.Name ELSE '' END as Name
FROM Table2 T2
INNER JOIN Table1 T1 ON T2.[FK_ID] = T1.id
LEFT JOIN (SELECT Name FROM Table2 GROUP BY Name HAVING COUNT(*) > 1) T3 ON T2.Name = T3.Name
Output
ID Name
1 Chris
2 Austin Adam
3 Steve
4 Charles
5 Erik
6 Austin Eve
You can use a window count to determine whether a name is repeated or not:
SELECT t2.ID,
CONCAT(t2.Name,
IIF(COUNT(t2.Name) OVER(PARTITION BY t2.Name) > 1,
COALESCE(' ' + t1.Name, ''),
''))
FROM TABLE2 AS t2
LEFT JOIN TABLE1 AS t1 ON t2.FK_ID = t1.ID
SELECT ID,NAME
FROM TABLE2
WHERE NAME NOT IN (
SELECT NAME
FROM TABLE2
GROUP BY NAME
HAVING COUNT(*) >1 )
UNION
SELECT A.ID AS ID,A.NAME+' '+B.NAME AS NAME
FROM TABLE2 AS A INNER JOIN TABLE1 AS B
ON A.FK_ID = B.ID
WHERE NAME IN (
SELECT NAME
FROM TABLE2
GROUP BY NAME
HAVING COUNT(*) >1 )
ORDER BY ID
Using union to process the logic by two group.

sql - how to query member of group with recursion (1 table with user and groups)

Following structure exists:
CREATE TABLE rel(
entry_id int,
parent_id int
)
CREATE TABLE entries(
entry_id int,
name varchar(44)
)
Following data exists:
INSERT INTO entries VALUES (1,'user 1');
INSERT INTO entries VALUES (2,'group 2');
INSERT INTO entries VALUES (3,'group 3');
INSERT INTO entries VALUES (4,'user 4');
INSERT INTO entries VALUES (5,'user 5');
INSERT INTO rel VALUES (3,2);
INSERT INTO rel VALUES (4,2);
INSERT INTO rel VALUES (1,3);
INSERT INTO rel VALUES (5,3);
INSERT INTO rel VALUES (2,NULL);
Result should look like:
group_id| group_name | member_id | member_name | level
2 | group 2 | 4 | user 4 | 0
2 | group 2 | 1 | user 1 | 1
2 | group 2 | 5 | user 5 | 1
3 | group 3 | 1 | user 1 | 0
3 | group 3 | 5 | user 5 | 0
I already tried stuff like the following but it's not returning the results I need:
SELECT
entries.entry_id,
entries.name,
rel.parent_id,
(SELECT name FROM entries WHERE entry_id=parent_id) AS parent_name
INTO
#tmpEntries
FROM
entries, rel
WHERE
rel.entry_id = entries.entry_id
;
SELECT * FROM #tmpEntries;
WITH MyCTE
AS (
SELECT
entry_id,
name,
parent_id,
--CAST('' AS VARCHAR(44)) AS
parent_name
FROM #tmpEntries
--WHERE parent_id IS NULL
UNION ALL
SELECT
#tmpEntries.entry_id,
#tmpEntries.name,
#tmpEntries.parent_id,
--MyCTE.name AS
#tmpEntries.parent_name
FROM #tmpEntries
INNER JOIN MyCTE ON #tmpEntries.parent_id = MyCTE.entry_id
--WHERE #tmpEntries.parent_id IS NOT NULL
-- WHERE NOT EXISTS (SELECT entry_id FROM rel WHERE parent_id=#tmpEntries.entry_id)
)
SELECT DISTINCT *
FROM MyCTE
ORDER BY parent_id
;
WITH MyCTE2
AS (
SELECT
entry_id,
name,
parent_id,
--CAST('' AS VARCHAR(44)) AS
parent_name
FROM #tmpEntries
--WHERE parent_id IS NULL
UNION ALL
SELECT
#tmpEntries.entry_id,
#tmpEntries.name,
#tmpEntries.parent_id,
--MyCTE.name AS
#tmpEntries.parent_name
FROM #tmpEntries
INNER JOIN MyCTE2 ON #tmpEntries.parent_id = MyCTE2.entry_id
--WHERE #tmpEntries.parent_id IS NOT NULL
WHERE NOT EXISTS (SELECT entry_id FROM rel WHERE parent_id=#tmpEntries.entry_id)
)
SELECT DISTINCT *
FROM MyCTE2
ORDER BY parent_id
This will work under assumption that group is anything that contains one or more members i.e. empty group will be considered a simple member.
with cte_hierarchy as
(
select entry_id, parent_id, 0 as level
from rel
where parent_id is not null
union all
select h.entry_id, rel.parent_id, h.level + 1 as level
from cte_hierarchy h
inner join rel on h.parent_id = rel.entry_id
)
select
g.entry_id as group_id,
g.name as group_name,
e.entry_id as member_id,
e.name as member_name,
h.level
from cte_hierarchy h
inner join entries e on e.entry_id = h.entry_id
inner join entries g on g.entry_id = h.parent_id
where not exists (select * from rel where parent_id = h.entry_id)
order by g.entry_id, h.level, e.entry_id
The where clause excludes nested groups that would otherwise appear as members.

Resources