| 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 Date11 Name1 Date11 Name1 Date12 Name1 Date22 Name1 Date22 Name1 Date22 Name1 Date2How can I turn it to this:1 Name1 Date1 Count32 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 countfrom (-- your existing query here)group by col1, col2, col3[/code]Note: Replace col1, col2, col3 with appropriate column names respectively.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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 countfrom (-- your existing query here)group by col1, col2, col3 Note: Replace col1, col2, col3 with appropriate column names respectively.Harsh AthalyeIndia."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 Count11 Name1 Date1 Count11 Name1 Date1 Count12 Name1 Date2 Count12 Name1 Date2 Count12 Name1 Date2 Count12 Name1 Date2 Count1I 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? |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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 countfrom (-- your existing query here) as dgroup by col1, col2, col3 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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 |
 |
|
|
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 dGROUP BY Col1, Col2ORDER BY Col1, Col2[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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 2001 2011 2021 2032 2002 205I tried this query:SELECT Col1 , Col2, 'Count' + CAST(COUNT(*) AS VARCHAR(12))FROM ( select Col1 , Col2from Table ) AS dGROUP BY Col1 , Col2ORDER BY Col1 , Col2and got this result:1 200 Count11 201 Count11 202 Count11 203 Count12 200 Count12 205 Count1Any ideas? |
 |
|
|
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 dGROUP BY Col1, Col2ORDER BY Col1, Col2 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
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? |
 |
|
|
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" |
 |
|
|
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 dGROUP BY Col1, Col2ORDER 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.. |
 |
|
|
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 dGROUP BY Col1, Col2ORDER 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. |
 |
|
|
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 @SampleSELECT 1, 'Name1', 'Date1' UNION ALLSELECT 1, 'Name1', 'Date1' UNION ALLSELECT 1, 'Name1', 'Date1' UNION ALLSELECT 2, 'Name1', 'Date2' UNION ALLSELECT 2, 'Name1', 'Date2' UNION ALLSELECT 2, 'Name1', 'Date2' UNION ALLSELECT 2, 'Name1', 'Date2'SELECT Col1, Col2, COUNT(*) AS [Count], 'COUNT' + CAST(COUNT(*) AS VARCHAR(12)) AS YakFROM @SampleGROUP BY Col1, Col2ORDER BY Col1, Col2 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
kensai
Posting Yak Master
172 Posts |
Posted - 2008-06-20 : 03:38:59
|
quote: Originally posted by visakh16You 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 252 25[TableQuestions]TestID QuestionID------------------1 2001 2011 2021 2032 2002 205What I want to display to user with a query is this:"User 25, you have following tests:Test number 1, has 4 questionsTest 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.. |
 |
|
|
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 visakh16You 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 252 25[TableQuestions]TestID QuestionID------------------1 2001 2011 2021 2032 2002 205What I want to display to user with a query is this:"User 25, you have following tests:Test number 1, has 4 questionsTest 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. |
 |
|
|
|