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
 Help me to solve my problem in sql script

Author  Topic 

andimanro
Starting Member

2 Posts

Posted - 2010-08-31 : 23:40:22
Dear All..

I have a table named tProduk with data like this :
---------
Name |
---------
Monitor |
Monitor |
Mouse |
Mouse |
Keyboard|

what sql script should i use to make the result like this from the table tProduk :
-----------------
Product | Count |
-----------------
Monitor | 2 |
Mouse | 2 |
Keyboard| 1 |


thnx in advance

matty
Posting Yak Master

161 Posts

Posted - 2010-09-01 : 02:32:36
[code]

SELECT Name,COUNT(Name) AS Count
FROM tProduk
GROUP BY Name
[/code]
Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2010-09-01 : 02:35:27
Hi!
You should always try to give us more info about the table.

Solution:
select Name, COUNT(Name) as COUNT from tProduk
group by Name order by COUNT desc

Output:
Monitor 2
Mouse 2
Keyboard 1
Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2010-09-01 : 02:36:47
Sorry about that, Matty posted his script before me :-).
Go to Top of Page

andimanro
Starting Member

2 Posts

Posted - 2010-09-01 : 03:31:59
dear maevr and matty..

thanks for your reply... it works... thanks again :)
Go to Top of Page
   

- Advertisement -