Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to count in sql?


how to count in sql?

Author
Message
asco5
asco5
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16510 Visits: 16985
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)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4231 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
demonfox
demonfox
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1201 Visits: 1192
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 Ermm
asco5
asco5
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
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
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
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
asco5
asco5
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 30
still same probleme not working same error
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6085 Visits: 6069
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
When a question, really isn't a question - Jeff Smith
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


asco5
asco5
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 30
still not working
anthony.green
anthony.green
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6085 Visits: 6069
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
When a question, really isn't a question - Jeff Smith
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search