Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

how to count in sql? Expand / Collapse
Author
Message
Posted Thursday, February 7, 2013 2:32 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 5, 2013 10:10 AM
Points: 17, Visits: 30
hi i would like to understand how to count in sql with different parameters?
for example i have

Date, firstname, name, nationality , mode, origine, id
1/1/2010 teste teste1 fr in p 01
15/07/2005 toto tata lb out L 02
01/03/2012 teste teste1 fr in P 01



i would like to know how many poeple have the most entry in my database
for example here teste teste1 with Id 01 have the most entry it's 2 record
so i need to know the top 100 of poeple who have the most entry

result i need is for example

id 01 = 2 entry
id 02= 1 entry

thanks to help
Post #1417359
Posted Thursday, February 7, 2013 2:35 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:35 PM
Points: 13,253, Visits: 12,087
asco5 (2/7/2013)
hi i would like to understand how to count in sql with different parameters?
for example i have

Date, firstname, name, nationality , mode, origine, id
1/1/2010 teste teste1 fr in p 01
15/07/2005 toto tata lb out L 02
01/03/2012 teste teste1 fr in P 01



i would like to know how many poeple have the most entry in my database
for example here teste teste1 with Id 01 have the most entry it's 2 record
so i need to know the top 100 of poeple who have the most entry

result i need is for example

id 01 = 2 entry
id 02= 1 entry

thanks to help


select top 100 ID, count(*)
from YourTable
group by ID
Order by count(*)

I think that should be close to what you are looking for.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1417362
Posted Thursday, February 7, 2013 6:16 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:14 AM
Points: 3,618, Visits: 5,254
asco5 (2/7/2013)

so i need to know the top 100 of poeple who have the most entry



How do you want to handle tied counts?

I'm thinking a RANK() or DENSE_RANK() might be more appropriate than TOP 100.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1417418
Posted Thursday, February 7, 2013 9:30 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 31, 2014 7:15 PM
Points: 1,129, Visits: 1,163
dwain.c (2/7/2013)
asco5 (2/7/2013)

so i need to know the top 100 of poeple who have the most entry



How do you want to handle tied counts?

I'm thinking a RANK() or DENSE_RANK() might be more appropriate than TOP 100.


I think it should be the Rank() or Dense_Rank()

as asked

i would like to know how many poeple have the most entry in my database
for example here teste teste1 with Id 01 have the most entry it's 2 record
so i need to know the top 100 of poeple who have the most entry



It's for the most entry ; It could be 100 or less than 100 ; by ranking function it could be determined appropriately..


~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one
Post #1417456
Posted Friday, February 8, 2013 5:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 5, 2013 10:10 AM
Points: 17, Visits: 30
hi i tried first one

select top 100 cause i need only the 100 first people who have the most entry

so i did

select top 100 ID,
count (*)
from [database].[table].[table]
group by id
order by count (*);

i received an error
column [database].[table].[table] is invalid in the select list because it is not contained
in either aggregate function or the group by clause

thanks for heping
Post #1417611
Posted Friday, February 8, 2013 5:16 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 18, 2014 9:09 AM
Points: 870, Visits: 2,385

this

select top 100 ID, 
count (*)
from [database].[table].[table]
group by id
order by count (*);

should be

select top 100 ID, 
count (*) myCount
from [database].[table].[table]
group by id
order by myCount;

to order by the Count you need to alias it and then reference that in the orderby, dont use Ordinal positions as its a pain to workout column 42 of a 100 column select, even worse when someone adds a 2 or more new columns a head of it.

Besides I understand that sorting by the ordinal is going to be depreciated in a later version.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1417617
Posted Friday, February 8, 2013 5:35 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 5, 2013 10:10 AM
Points: 17, Visits: 30
still same probleme not working same error
Post #1417628
Posted Friday, February 8, 2013 5:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 5,216, Visits: 5,064
select top 100 ID, 
count (ID) myCount
from [database].[table].[table]
group by id
order by myCount;

You cant use a * in an aggregate query unless you group by every column in the table

select top 100 ID, 
count (*) myCount
from [database].[table].[table]
group by id, col2, col3, col4, col5......................................
order by myCount;





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1417635
Posted Friday, February 8, 2013 5:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, August 5, 2013 10:10 AM
Points: 17, Visits: 30
still not working
Post #1417637
Posted Friday, February 8, 2013 5:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:55 AM
Points: 5,216, Visits: 5,064
what is not working? error message? incorrect data outputted? more information please?




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1417639
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse