473,378 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I get a count of each set of results?

My query returns a table of results, I would like to add a count column
that contains the number of each result type returned.

i.e.

Type Count
1 3
1 3
1 3
2 2
2 2
3 4
3 4
3 4
3 4
4 2
4 2

Because there are 3 of type 1, 2 of type 2, 4 of type 3 etc...

Is there straightforward way of doing this in SQL?

Thanks

Aug 22 '06 #1
1 1272
In SQL Server 2005:

select
[Type],
count() over (partition by [Type]) as [Count]
from T
In SQL Server 2000:

select
[Type],
(select count(*)
from T as Tcopy
where Tcopy.[Type] = T.[Type]
) as [Count]
from T

(both solutions untested - for a better chance at tested
solutions, include create table and insert statements that
can be cut and pasted into a query editor.)

Steve Kass
Drew University

ka*********@yahoo.com wrote:
My query returns a table of results, I would like to add a count column
that contains the number of each result type returned.

i.e.

Type Count
1 3
1 3
1 3
2 2
2 2
3 4
3 4
3 4
3 4
4 2
4 2

Because there are 3 of type 1, 2 of type 2, 4 of type 3 etc...

Is there straightforward way of doing this in SQL?

Thanks
Aug 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bryan | last post by:
I've been trying to work out a problem but I'm having no luck. I have field in my table for area and I need to count the number of matches and assign a variable for each one.. this is what I have...
5
by: Michael Krzepkowski | last post by:
All, I have a view that returns the following values: Item Vendor 70807 1234 70807 5678 If I am looking for items that have more...
6
by: Good Man | last post by:
Hi all Well, I didn't want to have to bring this question to someone else to answer, but I am truly flummoxed and could use some help. It all boils down to trying to find the number of jobs...
4
by: vacataire testeur de site web | last post by:
Bonjour je cherche à afficher de count de tables identiques dans la forme exemple num_cli | nombre de poste | nombre de voiture 1 1 3 2 2 ...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
3
by: ret4rt | last post by:
Hello. I have a database with movies similar with imdb and i want to find out which directors have directed both thriller and drama movies. The output i want is like this "DIR_NAME,amount of...
1
by: muld | last post by:
I've inherited a database system designed to calculate sickness absence statistics. There is a main table with an employee ID which also holds the number of days worked by that person in a year ....
1
by: jaxkookie | last post by:
I have been trying to count each answer for each question and roll this up to one line item per question with totals for each answer. what I am getting is staggered answers. example, question 1 has 9...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.