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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Finding count in a resultset

Author  Topic 

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-19 : 08:28:02
I have a select with a few joins producing this results table:

1 Name1 Date1
1 Name1 Date1
1 Name1 Date1
2 Name1 Date2
2 Name1 Date2
2 Name1 Date2
2 Name1 Date2

How can I turn it to this:

1 Name1 Date1 Count3
2 Name1 Date2 Count4

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-19 : 08:30:15
[code]select col1, col2, col3, count(*) as count
from
(
-- your existing query here
)
group by col1, col2, col3[/code]

Note: Replace col1, col2, col3 with appropriate column names respectively.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-19 : 08:58:43
quote:
Originally posted by harsh_athalye

select col1, col2, col3, count(*) as count
from
(
-- your existing query here
)
group by col1, col2, col3


Note: Replace col1, col2, col3 with appropriate column names respectively.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



I tried this with a simple select with no joins, it first gave me "syntax error near ')'" error. I managed it to run with giving a table name to the subquery. But it gave me this result:

1 Name1 Date1 Count1
1 Name1 Date1 Count1
1 Name1 Date1 Count1
2 Name1 Date2 Count1
2 Name1 Date2 Count1
2 Name1 Date2 Count1
2 Name1 Date2 Count1

I also tried it with the original query and I got a lot of "The multi-part identifier "Table.Column" could not be bound." errors.

Am I doing something wrong?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-19 : 09:01:40
Then post your sample data so we can have more idea about the problem.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 09:05:05
You have to name the derived table. Harsh is on the right track.
I wonder what the local time in India is now? 20:00? 21:00?
select col1, col2, col3, count(*) as count
from
(
-- your existing query here
) as d
group by col1, col2, col3



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-06-19 : 09:05:33
Harsh, you didn't name the derived table.

from
(
-- your existing query here
) x
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 09:07:54
[code]SELECT Col1,
Col2,
'Count' + CAST(COUNT(*) AS VARCHAR(12))
FROM (
{Your query here}
) AS d
GROUP BY Col1,
Col2
ORDER BY Col1,
Col2[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-19 : 09:19:27
Okay I'm trying to get it done with a very simple data first. This is the table:

Col1 Col2
---------
1 200
1 201
1 202
1 203
2 200
2 205

I tried this query:

SELECT Col1 ,
Col2,
'Count' + CAST(COUNT(*) AS VARCHAR(12))
FROM (
select Col1 , Col2
from Table
) AS d
GROUP BY Col1 ,
Col2
ORDER BY Col1 ,
Col2

and got this result:

1 200 Count1
1 201 Count1
1 202 Count1
1 203 Count1
2 200 Count1
2 205 Count1

Any ideas?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 09:21:16
About what?
It is exactly as you posted in your first post.

Maybe you want this?
SELECT		Col1,
Col2,
COUNT(*) AS [Count]
FROM (
SELECT Col1,
Col2
FROM Table
) AS d
GROUP BY Col1,
Col2
ORDER BY Col1,
Col2




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-06-19 : 09:29:43
Peso what is the use of the derived table in your last suggestion?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 09:31:16
I am a little anxious to change too many things at once for OP.
He seems not that familiar with syntax and use of derived tables.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-19 : 09:58:44
quote:
Originally posted by Peso

About what?
It is exactly as you posted in your first post.

Maybe you want this?
SELECT		Col1,
Col2,
COUNT(*) AS [Count]
FROM (
SELECT Col1,
Col2
FROM Table
) AS d
GROUP BY Col1,
Col2
ORDER BY Col1,
Col2




E 12°55'05.25"
N 56°04'39.16"




Peso I'm truly grateful for the help but it just not what I'm looking for. This just adds a column with a value of "1" to every row. I just wonder if it's not possible to do it or if I wasn't clear enough with my explanation..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 10:07:25
quote:
Originally posted by kensai

quote:
Originally posted by Peso

About what?
It is exactly as you posted in your first post.

Maybe you want this?
SELECT		Col1,
Col2,
COUNT(*) AS [Count]
FROM (
SELECT Col1,
Col2
FROM Table
) AS d
GROUP BY Col1,
Col2
ORDER BY Col1,
Col2




E 12°55'05.25"
N 56°04'39.16"




Peso I'm truly grateful for the help but it just not what I'm looking for. This just adds a column with a value of "1" to every row. I just wonder if it's not possible to do it or if I wasn't clear enough with my explanation..


You were not clear. You would sound more clear if you show some sample data together with table columns and also output you wish to get from the data.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-06-19 : 10:08:30
The only reason for the query to return 1 is that there are no duplicate values.
DECLARE	@Sample TABLE
(
ID INT,
Col1 VARCHAR(20),
Col2 VARCHAR(20)
)

INSERT @Sample
SELECT 1, 'Name1', 'Date1' UNION ALL
SELECT 1, 'Name1', 'Date1' UNION ALL
SELECT 1, 'Name1', 'Date1' UNION ALL
SELECT 2, 'Name1', 'Date2' UNION ALL
SELECT 2, 'Name1', 'Date2' UNION ALL
SELECT 2, 'Name1', 'Date2' UNION ALL
SELECT 2, 'Name1', 'Date2'

SELECT Col1,
Col2,
COUNT(*) AS [Count],
'COUNT' + CAST(COUNT(*) AS VARCHAR(12)) AS Yak
FROM @Sample
GROUP BY Col1,
Col2
ORDER BY Col1,
Col2



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

kensai
Posting Yak Master

172 Posts

Posted - 2008-06-20 : 03:38:59
quote:
Originally posted by visakh16
You were not clear. You would sound more clear if you show some sample data together with table columns and also output you wish to get from the data.



Ok I'm sorry about that. Here's a more complete data about what I'd like to achieve:

There're two tables. It's about tests created by users and the questions added by them for each tests. The main tests table:

[TableTests]
TestID UserID
--------------
1 25
2 25

[TableQuestions]
TestID QuestionID
------------------
1 200
1 201
1 202
1 203
2 200
2 205

What I want to display to user with a query is this:

"User 25, you have following tests:
Test number 1, has 4 questions
Test number 2, has 2 questions"

I joined tables with inner join and had the results in my original post and thought if there's a way to achieve this modifying my query in any way. I'm not so good with joins so I can't think any other way to do this with a single query or in a single stored procedure. I appreciate any help..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-20 : 03:43:54
quote:
Originally posted by kensai

quote:
Originally posted by visakh16
You were not clear. You would sound more clear if you show some sample data together with table columns and also output you wish to get from the data.



Ok I'm sorry about that. Here's a more complete data about what I'd like to achieve:

There're two tables. It's about tests created by users and the questions added by them for each tests. The main tests table:

[TableTests]
TestID UserID
--------------
1 25
2 25

[TableQuestions]
TestID QuestionID
------------------
1 200
1 201
1 202
1 203
2 200
2 205

What I want to display to user with a query is this:

"User 25, you have following tests:
Test number 1, has 4 questions
Test number 2, has 2 questions"

I joined tables with inner join and had the results in my original post and thought if there's a way to achieve this modifying my query in any way. I'm not so good with joins so I can't think any other way to do this with a single query or in a single stored procedure. I appreciate any help..


this display formatting needs to be done at your front end. you could use the query posted by Peso to get count and then do this formatting on it.
Go to Top of Page
   

- Advertisement -