473,406 Members | 2,369 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,406 software developers and data experts.

COUNT depending on data

Hello.

I have one serious problem with COUNT in TSQL.
I use MS SQL Server 2000.
I would like to count rows depending on data in it.
What I meat is:
I have fields like:
region | month | year | some_count |
-------|-------|------|-------------|
LA | 1 | 2003| 4 |
LA | 2 | 2003| 2 |
LA | 3 | 2003| 1 |
LA | 4 | 2003| 6 |
VV | 1 | 2003| 3 |
VV | 2 | 2003| 7 |
VV | 4 | 2003| 20 |
VV | 6 | 2003| 3 |
BB | 2 | 2002| 1 |
etc...

And what I would like to get from it is:

region | jan2003 | feb2003 | mar2003 | apr2003 | may2003 | june2003 | etc...
-------|----------|---------|-----------|----------|----------|-----------|-
-
LA | 4 | 2 | 1 | 6 |
0 | 0 | 0
VV | 3 | 7 | 0 | 20 | 0
| 3 | 0
BB | 0 | 1 | 0 | 0 |
0 | 0 | 0
etc...

Is it possible to do it in views? if yes then how?
I could use temp tables for each month but it is not solution when I have 3
years
and more later then it will be round 40 temporary tables.

Thanks for any response.

Regards,
Casper


Jul 20 '05 #1
3 2681
Casper,

What you need is a crosstab query. SQL Server do not have this functionality
as Access or Excel have. It's feasable but not very easy. Take a look here:
http://www.mssqlserver.com/faq/development-crosstab.asp

As you'll see, you'll have to hard code your month column name and manualy
add one in your query.

HTH

Yannick

"Kaczorek" <ka******@poczta.onet.pl> wrote in message
news:bj**********@news.onet.pl...
Hello.

I have one serious problem with COUNT in TSQL.
I use MS SQL Server 2000.
I would like to count rows depending on data in it.
What I meat is:
I have fields like:
region | month | year | some_count |
-------|-------|------|-------------|
LA | 1 | 2003| 4 |
LA | 2 | 2003| 2 |
LA | 3 | 2003| 1 |
LA | 4 | 2003| 6 |
VV | 1 | 2003| 3 |
VV | 2 | 2003| 7 |
VV | 4 | 2003| 20 |
VV | 6 | 2003| 3 |
BB | 2 | 2002| 1 |
etc...

And what I would like to get from it is:

region | jan2003 | feb2003 | mar2003 | apr2003 | may2003 | june2003 | etc... -------|----------|---------|-----------|----------|----------|----------- |- -
LA | 4 | 2 | 1 | 6 |
0 | 0 | 0
VV | 3 | 7 | 0 | 20 | 0 | 3 | 0
BB | 0 | 1 | 0 | 0 |
0 | 0 | 0
etc...

Is it possible to do it in views? if yes then how?
I could use temp tables for each month but it is not solution when I have 3 years
and more later then it will be round 40 temporary tables.

Thanks for any response.

Regards,
Casper


Jul 20 '05 #2

Thanks verry much. This link is verry helpfull. This is exactly what I
nead.

Regards,
Casper
Użytkownik "Yannick Turgeon" <no****@nowhere.com> napisał w wiadomo¶ci
news:9c******************@news20.bellglobal.com...
Casper,

What you need is a crosstab query. SQL Server do not have this functionality as Access or Excel have. It's feasable but not very easy. Take a look here: http://www.mssqlserver.com/faq/development-crosstab.asp

As you'll see, you'll have to hard code your month column name and manualy
add one in your query.

HTH

Yannick

"Kaczorek" <ka******@poczta.onet.pl> wrote in message
news:bj**********@news.onet.pl...
Hello.

I have one serious problem with COUNT in TSQL.
I use MS SQL Server 2000.
I would like to count rows depending on data in it.
What I meat is:
I have fields like:
region | month | year | some_count |
-------|-------|------|-------------|
LA | 1 | 2003| 4 |
LA | 2 | 2003| 2 |
LA | 3 | 2003| 1 |
LA | 4 | 2003| 6 |
VV | 1 | 2003| 3 |
VV | 2 | 2003| 7 |
VV | 4 | 2003| 20 |
VV | 6 | 2003| 3 |
BB | 2 | 2002| 1 |
etc...

And what I would like to get from it is:

region | jan2003 | feb2003 | mar2003 | apr2003 | may2003 | june2003 | etc...

-------|----------|---------|-----------|----------|----------|-----------
|-
-
LA | 4 | 2 | 1 | 6 |
0 | 0 | 0
VV | 3 | 7 | 0 | 20 |

0
| 3 | 0
BB | 0 | 1 | 0 | 0 |
0 | 0 | 0
etc...

Is it possible to do it in views? if yes then how?
I could use temp tables for each month but it is not solution when I

have 3
years
and more later then it will be round 40 temporary tables.

Thanks for any response.

Regards,
Casper



Jul 20 '05 #3

Thanks verry much. This link is verry helpfull. This is exactly what I
nead.

Regards,
Casper
Użytkownik "Yannick Turgeon" <no****@nowhere.com> napisał w wiadomo¶ci
news:9c******************@news20.bellglobal.com...
Casper,

What you need is a crosstab query. SQL Server do not have this functionality as Access or Excel have. It's feasable but not very easy. Take a look here: http://www.mssqlserver.com/faq/development-crosstab.asp

As you'll see, you'll have to hard code your month column name and manualy
add one in your query.

HTH

Yannick

"Kaczorek" <ka******@poczta.onet.pl> wrote in message
news:bj**********@news.onet.pl...
Hello.

I have one serious problem with COUNT in TSQL.
I use MS SQL Server 2000.
I would like to count rows depending on data in it.
What I meat is:
I have fields like:
region | month | year | some_count |
-------|-------|------|-------------|
LA | 1 | 2003| 4 |
LA | 2 | 2003| 2 |
LA | 3 | 2003| 1 |
LA | 4 | 2003| 6 |
VV | 1 | 2003| 3 |
VV | 2 | 2003| 7 |
VV | 4 | 2003| 20 |
VV | 6 | 2003| 3 |
BB | 2 | 2002| 1 |
etc...

And what I would like to get from it is:

region | jan2003 | feb2003 | mar2003 | apr2003 | may2003 | june2003 | etc...

-------|----------|---------|-----------|----------|----------|-----------
|-
-
LA | 4 | 2 | 1 | 6 |
0 | 0 | 0
VV | 3 | 7 | 0 | 20 |

0
| 3 | 0
BB | 0 | 1 | 0 | 0 |
0 | 0 | 0
etc...

Is it possible to do it in views? if yes then how?
I could use temp tables for each month but it is not solution when I

have 3
years
and more later then it will be round 40 temporary tables.

Thanks for any response.

Regards,
Casper





Jul 20 '05 #4

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

Similar topics

2
by: Alex | last post by:
Hi all, I'm running into a road block, and I know I've done this before. I'm getting fields from two tables, and I need to do a count of similar items with it showing some extra info. Here's...
2
by: Niklas E | last post by:
Does anyone know how to get all nodes in a xml-document that start with a certain text? In my case I want to count all suppliers ( <id>Supplier*</id> ) <root> <customer> <name>ABC</name>...
5
by: Stefan | last post by:
Okay, I'm confused. The code below produces no errors and prints -1 for the RecordCount. I run it on a local Access DB. I ran the query in Access and it returns correctly one field with one row...
1
by: Simon Matthews | last post by:
Hope someone can help an Access beginner! I've just started keeping my surgical logbook on access and it's a simple flat-file affair. I have created several queries that will list cases...
3
by: Rabun | last post by:
Heres one that is giving me fits ( = = Access newbie), more than likely something simple that I blew right over . . . any help is appreciated - I have a report based on a query, with several...
4
by: Krish | last post by:
I donot understand why it is this way. I have an arraylist. I understand I can get the count by arraylist.count in .net studio, intellisense also gives me arraylist.count.tostring()
9
by: NvrBst | last post by:
Whats the best way to count the lines? I'm using the following code at the moment: public long GetNumberOfLines(string fileName) { int buffSize = 65536; int streamSize = 65536; long...
5
by: jehugaleahsa | last post by:
Hello: I am sure this question comes up a lot. I need to disable the controls on my Windows forms so that when the BindingSource is empty some the controls bound to it will be disabled. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.