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.
| 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.col3group 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 tbl1group 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" |
 |
|
|
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_id1 Name12 Name2TBL2 ==========id name_id file11 Name1 File_name12 Name1 File_name23 Name2 File_name34 Name2 File_name4TBL3 ==========id name_id file11 Name1 File_name52 Name1 File_name63 Name1 File_name74 Name2 File_name8Expected result from the query:Name Nb file1 Nb file2Name1 2 2Name2 3 1Nb file1 - count Name1 from TBL1 and TBL2Nb file2 - count Name2 from TBL1 and TBL2 |
 |
|
|
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" |
 |
|
|
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 |
 |
|
|
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 Tablescreate 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 Tbl1Select 1, 'Name1'Union ALLSelect 2, 'Name2'Insert Into TBL2Select 1, 'Name1', 'File_name1'Union ALLSelect 2, 'Name1', 'File_name2'Union ALLSelect 3, 'Name2', 'File_name3'Union ALLSelect 4, 'Name2', 'File_name4'Insert Into TBL3Select 1, 'Name1', 'File_name5'Union ALLSelect 2, 'Name1', 'File_name6'Union ALLSelect 3, 'Name1', 'File_name7'Union ALLSelect 4, 'Name2', 'File_name8'--Query For Your requirementSelect 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" |
 |
|
|
|
|
|
|
|