Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple record counts in one sql query

Author  Topic 

asentilev
Starting Member

3 Posts

Posted - 2012-05-22 : 12:16:20
Hello,
I am trying to count few things in one query, but it is not working for me.
Here is the the idea of what i am trying to do:
select tbl1.col1, tbl1.col2,
count (distinct (tbl2.x) as "Nb" from tbl1 join tbl2 on tbl1.col3=tbl2.col3,
count (distinct (tbl3.x) as "Nb_ba" from tbl1 join tbl2 on col3.idntfr_person=tbl3.col3
group by ....

To count this: for col3 in tbl1 i need to see how many records i have for it in tbl2, tbl3 etc ...


If i have just the first count statement it is working, but i need more than one.



I try with select in front of count - i get some result, but it is not the one that i need.
select tbl1.col1, tbl1.col2,
(select count (distinct (tbl2.x)) as "Nb" from tbl1 join tbl2 on tbl1.col3=tbl2.col3),
(select count (distinct (tbl3.x)) as "Nb_ba" from tbl1 join tbl2 on col3.idntfr_person=tbl3.col3)
from tbl1
group by ....


Can you please help me. Thx :)

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-23 : 03:24:26
First, Welcome to SQLTeam.

Second, I don't think you can use the JOIN like that.

Third, people here could help you in building the query but you have to help them first by posting the DDL of the tables(create statements of the tables) so that they know what kinda tables and fields you are working with. You'll have to post some sample data to go with it, which could be used by everyone who might help you.

Please help us in helping you.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

asentilev
Starting Member

3 Posts

Posted - 2012-05-23 : 04:58:28
Hello vinu.vijayan.
Thank you for your answer.
Here are little more details on the subject.

DDL:
create table OWNER.TBL1
(
id char (13 byte) NOT NULL ,
name_id varchar2 (8 char)
)
create unique index OWNER.DI$TBL1 on OWNER.TBL1
(
id
)


create table OWNER.TBL2
(
id char (13 byte) NOT NULL ,
name_id varchar2 (8 char) ,
file1 varchar2 (8 char)
)
create unique index OWNER.DI$TBL2 on OWNER.TBL2
(
id
)


create table OWNER.TBL3
(
id char (13 byte) NOT NULL ,
name_id varchar2 (8 char) ,
file3 varchar2 (8 char)
)
create unique index OWNER.DI$TBL3 on OWNER.TBL3
(
id
)


EX:

TBL1
==========
id name_id
1 Name1
2 Name2


TBL2
==========
id name_id file1
1 Name1 File_name1
2 Name1 File_name2
3 Name2 File_name3
4 Name2 File_name4


TBL3
==========
id name_id file1
1 Name1 File_name5
2 Name1 File_name6
3 Name1 File_name7
4 Name2 File_name8


Expected result from the query:
Name Nb file1 Nb file2
Name1 2 2
Name2 3 1

Nb file1 - count Name1 from TBL1 and TBL2
Nb file2 - count Name2 from TBL1 and TBL2
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-23 : 05:23:01
Are you using SQL Server??....This DDL of your tables don't look like SQL Server code to me....I think you are in the wrong place. This is a MSSQL Server Forum.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

asentilev
Starting Member

3 Posts

Posted - 2012-05-23 : 07:51:42
I am not sure about the sql server, i can see with the hosting company.
Sorry, I didn't understand that sqlteam can help for sql queries only when Microsoft SQL Server is used.
Thank you Vinu
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-23 : 08:05:39
quote:
Originally posted by asentilev

I am not sure about the sql server, i can see with the hosting company.
Sorry, I didn't understand that sqlteam can help for sql queries only when Microsoft SQL Server is used.
Thank you Vinu



Yes you can check that with them.
But, till then I made a few changes and made it work in SQL Server. You can have a look at it and get some ideas maybe:


--Creating Tables

create table Tbl1
(
id int NOT NULL ,
name_id varchar (20)
)

ALTER TABLE Tbl1 ADD CONSTRAINT
id_NCUI UNIQUE NONCLUSTERED
(
id
)


create table TBL2
(
id int NOT NULL ,
name_id varchar (20) ,
file1 varchar (20)
)

ALTER TABLE TBL2 ADD CONSTRAINT
id_TBL2_NCUI UNIQUE NONCLUSTERED
(
id
)


create table TBL3
(
id int NOT NULL ,
name_id varchar (20) ,
file3 varchar (20)
)

ALTER TABLE TBL3 ADD CONSTRAINT
id_TBL3_NCUI UNIQUE NONCLUSTERED
(
id
)


--Inserting Sample Data

Insert Into Tbl1
Select 1, 'Name1'
Union ALL
Select 2, 'Name2'

Insert Into TBL2
Select 1, 'Name1', 'File_name1'
Union ALL
Select 2, 'Name1', 'File_name2'
Union ALL
Select 3, 'Name2', 'File_name3'
Union ALL
Select 4, 'Name2', 'File_name4'

Insert Into TBL3
Select 1, 'Name1', 'File_name5'
Union ALL
Select 2, 'Name1', 'File_name6'
Union ALL
Select 3, 'Name1', 'File_name7'
Union ALL
Select 4, 'Name2', 'File_name8'


--Query For Your requirement

Select a.name_id, b.Count_Tbl2, c.Count_Tbl3
From Tbl1 As a
JOIN
(Select name_id, COUNT(name_id) As Count_Tbl2 From TBL2 Group By name_id) As b ON a.name_id = b.name_id
JOIN
(Select name_id, COUNT(name_id) As Count_Tbl3 From TBL3 Group By name_id) As c ON a.name_id = c.name_id


Hope this helps.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
   

- Advertisement -