| Author |
Topic |
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 09:15:54
|
| Hello,How to write a query that counts the total number of occurrences for each value in colA (TableA), in TableB.Sample scenarioTableAC1 C2-----1 X2 YTableBC1 C2 C3------1 X a2 Y b3 X c4 X d5 Y eThe output should showC2 Count--------X 3Y 2Where Ci(i = 1 to 3), are columns.ThanksSrinivas |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 09:20:32
|
[code]select a.C2, Count = count(*)from tablea a inner join tableb b on a.C2 = b.C2group by a.C2[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-06-04 : 09:21:49
|
| select c2,count(c2) from tableb group by c2Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceled |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 09:50:37
|
quote: Originally posted by khtan
select a.C2, Count = count(*)from tablea a inner join tableb b on a.C2 = b.C2group by a.C2 KH[spoiler]Time is always against us[/spoiler]
Hi Khtan, Thanks for your reply. I have to extend my query to 3 tables. Here is the sample scenarioTable1C1 C2 C3 C4------------1 X A a2 Y B b3 Z C cTable2 C1 C2 C5----------1 X x2 Y yTable3C1 C2 C6---------1 X a2 Y b3 X c4 X d5 Y eI need to write a query that prints the total number of occurrences for each value of column C2 in Table2, in Table3The output should contain the following columnsT1.C2 T1.C3 T1.C4 T2.C5 count-----------------------------X A a x 3Y B b y 2Where Ci (i=1 to 5) are columns and T1 = Table1, T2 = Table2Thanks a lotSrinivas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 10:01:30
|
[code]select T1.C2, T1.C3, T1.C4, T2.C5, count = count(*)from Table2 T2 inner join Table1 T1 on T2.C2 = T1.C2 inner join Table3 T3 on T2.C2 = T3.C2group by T1.C2, T1.C3, T1.C4, T2.C5[/code]EDIT in red KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 10:02:06
|
is there a table 4 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 10:04:51
|
quote: Originally posted by khtan
select T1.C2 T1.C3 T1.C4 T2.C5, count = count(*)from Table2 T2 inner join Table1 T1 on T2.C2 = T1.C2 inner join Table3 T3 on T2.C2 = T3.C2group by T1.C2, T1.C3, T1.C4, T2.C5EDIT in red KH[spoiler]Time is always against us[/spoiler]
Hi Khtan, No, there is no Table4.But are you sure this gives the required number of rows and the right value of count ????T1.C2 T1.C3 T1.C4 T2.C5 count-----------------------------X A a x 3Y B b y 2If possible, can you explain your query.ThanksSrinivas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 10:07:53
|
missed out the group by statement KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 10:11:14
|
quote: Originally posted by khtan missed out the group by statement KH[spoiler]Time is always against us[/spoiler]
But are you sure this gives the required number of rows and the right value of count ????T1.C2 T1.C3 T1.C4 T2.C5 count-----------------------------X A a x 3Y B b y 2If possible, can you explain your query.ThanksSrinivas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 10:12:37
|
quote: Originally posted by seenuFour
quote: Originally posted by khtan missed out the group by statement KH[spoiler]Time is always against us[/spoiler]
But are you sure this gives the required number of rows and the right value of count ????T1.C2 T1.C3 T1.C4 T2.C5 count-----------------------------X A a x 3Y B b y 2If possible, can you explain your query.ThanksSrinivas
i don't know. why don't you try out and tell me ? I don't have your tables KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 10:20:05
|
quote: Originally posted by khtan
quote: Originally posted by seenuFour
quote: Originally posted by khtan missed out the group by statement KH[spoiler]Time is always against us[/spoiler]
But are you sure this gives the required number of rows and the right value of count ????T1.C2 T1.C3 T1.C4 T2.C5 count-----------------------------X A a x 3Y B b y 2If possible, can you explain your query.ThanksSrinivas
i don't know. why don't you try out and tell me ? I don't have your tables KH[spoiler]Time is always against us[/spoiler]
select T1.C2, T1.C3, T1.C4, T2.C5, count = count(*)from Table2 T2 where T1.C2 = T2.C2 and inner join Table3 T3 on T2.C2 = T3.C2group by T1.C2, T1.C3, T1.C4, T2.C5The count should only count all the occurrences in table T3.Please let me know if this query serves the purpose. I don't have the data in the tables yet. I am waiting on the DBA to populate the initial data.Thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-04 : 11:31:20
|
quote: Please let me know if this query serves the purpose.I don't have the data in the tables yet. I am waiting on the DBA to populate the initial data.
I don't have the data & table at all but . . .DECLARE @Table1 TABLE( C1 int, C2 CHAR, C3 CHAR, C4 CHAR)INSERT INTO @Table1SELECT 1, 'X', 'A', 'a' UNION ALLSELECT 2, 'Y', 'B', 'b' UNION ALLSELECT 3, 'Z', 'C', 'c'DECLARE @Table2 TABLE( C1 int, C2 CHAR, C5 CHAR)INSERT INTO @Table2SELECT 1, 'X', 'x' UNION ALLSELECT 2, 'Y', 'y'DECLARE @Table3 TABLE( C1 int, C2 CHAR, C6 CHAR)INSERT INTO @Table3SELECT 1, 'X', 'a' UNION ALLSELECT 2, 'Y', 'b' UNION ALLSELECT 3, 'X', 'c' UNION ALLSELECT 4, 'X', 'd' UNION ALLSELECT 5, 'Y', 'e'SELECT T1.C2, T1.C3, T1.C4, T2.C5, COUNT = COUNT(*)FROM @Table2 T2 INNER JOIN @Table1 T1 ON T2.C2 = T1.C2 INNER JOIN @Table3 T3 ON T2.C2 = T3.C2GROUP BY T1.C2, T1.C3, T1.C4, T2.C5/*C2 C3 C4 C5 COUNT ---- ---- ---- ---- ----------- X A a x 3Y B b y 2(2 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-04 : 11:46:08
|
quote: Originally posted by khtan
quote: Please let me know if this query serves the purpose.I don't have the data in the tables yet. I am waiting on the DBA to populate the initial data.
I don't have the data & table at all but . . .DECLARE @Table1 TABLE( C1 int, C2 CHAR, C3 CHAR, C4 CHAR)INSERT INTO @Table1SELECT 1, 'X', 'A', 'a' UNION ALLSELECT 2, 'Y', 'B', 'b' UNION ALLSELECT 3, 'Z', 'C', 'c'DECLARE @Table2 TABLE( C1 int, C2 CHAR, C5 CHAR)INSERT INTO @Table2SELECT 1, 'X', 'x' UNION ALLSELECT 2, 'Y', 'y'DECLARE @Table3 TABLE( C1 int, C2 CHAR, C6 CHAR)INSERT INTO @Table3SELECT 1, 'X', 'a' UNION ALLSELECT 2, 'Y', 'b' UNION ALLSELECT 3, 'X', 'c' UNION ALLSELECT 4, 'X', 'd' UNION ALLSELECT 5, 'Y', 'e'SELECT T1.C2, T1.C3, T1.C4, T2.C5, COUNT = COUNT(*)FROM @Table2 T2 INNER JOIN @Table1 T1 ON T2.C2 = T1.C2 INNER JOIN @Table3 T3 ON T2.C2 = T3.C2GROUP BY T1.C2, T1.C3, T1.C4, T2.C5/*C2 C3 C4 C5 COUNT ---- ---- ---- ---- ----------- X A a x 3Y B b y 2(2 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler]
Hi, I still get an error when I execute the query:SELECT T1.Competency, T1.CourseName, T2.StartDate, T1.Duration, COUNT = COUNT(*), T1.CapacityFROM @CourseSchedule T2 INNER JOIN @Courses T1 ON T2.CourseNo = T1.CourseNo INNER JOIN @Registrations T3 ON T2.CourseNo = T3.CourseNoGROUP BY T1.Competency, T1.CourseName, T2.StartDate, T1.Duration, T1.CapacityHere is the error:Msg 1087, Level 15, State 2, Line 2Must declare the table variable "@CourseSchedule".What is that I am doing wrong.ThanksSrinivas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-11 : 09:47:38
|
Do you have actual table ? or are you using table variable for the query ?replace the table variable with your actual table name KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
DavidDock
Starting Member
1 Post |
Posted - 2009-06-15 : 03:06:28
|
| This is the SQL select Query which are used to get all information in the database.SELECT * FROM table_nametemporary jobs London and the UK |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-18 : 12:17:50
|
quote: Originally posted by khtan
quote: Please let me know if this query serves the purpose.I don't have the data in the tables yet. I am waiting on the DBA to populate the initial data.
I don't have the data & table at all but . . .DECLARE @Table1 TABLE( C1 int, C2 CHAR, C3 CHAR, C4 CHAR)INSERT INTO @Table1SELECT 1, 'X', 'A', 'a' UNION ALLSELECT 2, 'Y', 'B', 'b' UNION ALLSELECT 3, 'Z', 'C', 'c'DECLARE @Table2 TABLE( C1 int, C2 CHAR, C5 CHAR)INSERT INTO @Table2SELECT 1, 'X', 'x' UNION ALLSELECT 2, 'Y', 'y'DECLARE @Table3 TABLE( C1 int, C2 CHAR, C6 CHAR)INSERT INTO @Table3SELECT 1, 'X', 'a' UNION ALLSELECT 2, 'Y', 'b' UNION ALLSELECT 3, 'X', 'c' UNION ALLSELECT 4, 'X', 'd' UNION ALLSELECT 5, 'Y', 'e'SELECT T1.C2, T1.C3, T1.C4, T2.C5, COUNT = COUNT(*)FROM @Table2 T2 INNER JOIN @Table1 T1 ON T2.C2 = T1.C2 INNER JOIN @Table3 T3 ON T2.C2 = T3.C2GROUP BY T1.C2, T1.C3, T1.C4, T2.C5/*C2 C3 C4 C5 COUNT ---- ---- ---- ---- ----------- X A a x 3Y B b y 2(2 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler]
Hi Khtan, I am having trouble with this query, If the table T2 has following entriesC1 C2 C5-----------1, X, x2, Y, y3, Z, zThen the out put should showC2 C3 C4 C5 COUNT ---- ---- ---- ---- ----------- X A a x 3Y B b y 2Z C c z 0I think, I have to do , outer Join ???I tried this:SELECT T1.C2, T1.C3, T1.C4, T2.C5, COUNT = COUNT(*)FROM @Table2 T2 INNER JOIN @Table1 T1 ON T2.C2 = T1.C2 LEFT JOIN @Table3 T3 ON T2.C2 = T3.C2GROUP BY T1.C2, T1.C3, T1.C4, T2.C5Gives me an WRONG output: C2 C3 C4 C5 COUNT ---- ---- ---- ---- ----------- X A a x 3Y B b y 2Z C c z 1The count should be 0, it prints 1How should I write the query using Left Join and print correct value for count.ThanksSrinivas |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-18 : 12:19:52
|
which outer join do you want ? LEFT OUTER JOIN ? RIGHT OUTER JOIN ? FULL OUTER JOIN ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-18 : 12:28:33
|
quote: How should I write the query using Left Join and print correct value for count.
You can make things easier for those who try to help you by providing the table structure in declare and sample data in insert into statements similar to what i have done in my post. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-18 : 12:31:35
|
quote: Originally posted by khtan which outer join do you want ? LEFT OUTER JOIN ? RIGHT OUTER JOIN ? FULL OUTER JOIN ? KH[spoiler]Time is always against us[/spoiler]
LEFT OUTER JOIN , I think, I'll give the scenario for your reference:Table1C1 C2 C3 C4------------1 X A a2 Y B b3 Z C cTable2C1 C2 C5----------1 X x2 Y y3 Z zTable3C1 C2 C6---------1 X a2 Y b3 X c4 X d5 Y eI should get output like this:C2 C3 C4 C5 COUNT---- ---- ---- ---- -----------X A a x 3Y B b y 2Z C c z 0I tried left, right and full joinsSELECT T1.C2, T1.C3, T1.C4, T2.C5, COUNT = COUNT(*)FROM @Table2 T2INNER JOIN @Table1 T1 ON T2.C2 = T1.C2LEFT JOIN @Table3 T3 ON T2.C2 = T3.C2GROUP BY T1.C2, T1.C3, T1.C4, T2.C5LEFT and FULL JOINs print C2 C3 C4 C5 COUNT---- ---- ---- ---- -----------X A a x 3Y B b y 2Z C c z 1RIGHT JOIN printsC2 C3 C4 C5 COUNT---- ---- ---- ---- -----------X A a x 3Y B b y 2Please tell me the correct way to use JoinThanksSrinivas |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-06-18 : 15:42:33
|
| declare @x table (c1 int, c2 char(1),c3 char(1),c4 char(1))insert @xselect 1,'X','A','a' union allselect 2,'Y','B','b' union allselect 3,'Z','C','c' declare @y table (c1 int, c2 char(1),c5 char(1))insert @yselect 1,'X','x' union allselect 2,'Y','y' union allselect 3,'Z','z'declare @z table (c1 int, c2 char(1), c6 char(1))insert @zselect 1, 'X' ,'a' union allselect 2, 'Y', 'b' union allselect 3, 'X', 'c' union allselect 4, 'X', 'd' union allselect 5, 'Y', 'e'select x.c2,x.c3,x.c4,y.c5, coalesce(z.[count] ,0)from @x x inner join @y y on x.c2 = y.c2left join (select count(*) as [count],c2 from @z group by c2) zon x.c2=z.c2 |
 |
|
|
seenuFour
Starting Member
16 Posts |
Posted - 2009-06-18 : 16:07:55
|
quote: Originally posted by vijayisonly declare @x table (c1 int, c2 char(1),c3 char(1),c4 char(1))insert @xselect 1,'X','A','a' union allselect 2,'Y','B','b' union allselect 3,'Z','C','c' declare @y table (c1 int, c2 char(1),c5 char(1))insert @yselect 1,'X','x' union allselect 2,'Y','y' union allselect 3,'Z','z'declare @z table (c1 int, c2 char(1), c6 char(1))insert @zselect 1, 'X' ,'a' union allselect 2, 'Y', 'b' union allselect 3, 'X', 'c' union allselect 4, 'X', 'd' union allselect 5, 'Y', 'e'select x.c2,x.c3,x.c4,y.c5, coalesce(z.[count] ,0)from @x x inner join @y y on x.c2 = y.c2left join (select count(*) as [count],c2 from @z group by c2) zon x.c2=z.c2
Hi Vijay, Thanks for your reply. Can you make sure that your query works fine ?I am finding it hard to understand. Lets say the table names here are Courses, and it has values:C1 C2 C3 C4------------1 X A a2 Y B b3 Z C cSecond table name is CourseSchedule and it has values:C1 C2 C5----------1 X x2 Y y3 Z zThird table name is Registrations and it has values:C1 C2 C6---------1 X a2 Y b3 X c4 X d5 Y eThe requirement is I have to count total number of occurrences for each value of column C2 in CourseSchedule, in Registrations.The output I need is C2 C3 C4 C5 COUNT---- ---- ---- ---- -----------X A a x 3Y B b y 2Z C c z 0Earlier I tried this query:SELECT Courses.C2, Courses.C3, Courses.C4, CourseSchedule.C5, COUNT = COUNT(*)FROM CourseSchedule INNER JOIN Courses ON CourseSchedule.C2 = Courses.C2 LEFT JOIN Registrations ON CourseSchedule.C2 = Registrations .C2GROUP BY Courses.C2, Courses.C3, Courses.C4, CourseSchedule.C5This prints wrong value for count in the third row as 1, instead fo 0:C2 C3 C4 C5 COUNT---- ---- ---- ---- -----------X A a x 3Y B b y 2Z C c z 1Can you please use above tables and tell me if your query works and gives the above output ?RegardsSrinivas |
 |
|
|
Next Page
|
|
|