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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Count and group by

Author  Topic 

Kim
Starting Member

3 Posts

Posted - 2004-09-21 : 07:54:07
Hi,

The following query:

Select
h.HeaderID,
h.FromDat,
h.TomDat,
ta.Country
From SvHeader h
Join SvTillAdress ta on h.HeaderID = ta.HeaderID
Where h.AdressandTyp = 'TFS'
And ta.Country<> ''
And ta.Country<> 'Sverige'
And ta.AdressKalla = 'P'
And h.FromDat >= '2003-01-01'

gives the following resultset with some 5000 rows:

HeaderID FromDat TomDat Country
2514241 2003-01-07 00:00:00.000 2003-03-20 00:00:00.000 SPANIEN
2514450 2003-01-04 00:00:00.000 2003-04-15 00:00:00.000 FRANKRIKE
2811566 2003-07-09 00:00:00.000 2003-08-08 00:00:00.000 NORGE
2811698 2003-07-20 00:00:00.000 2003-08-08 00:00:00.000 NORGE
2811789 2003-07-07 00:00:00.000 2003-08-04 00:00:00.000 ÅLAND
2812068 2003-07-10 00:00:00.000 2003-09-04 00:00:00.000 ITALIEN
2812236 2003-07-07 00:00:00.000 2004-01-07 00:00:00.000 FRANKRIKE
2812361 2003-07-04 00:00:00.000 2003-07-27 00:00:00.000 DANMARK
2812436 2003-08-01 00:00:00.000 2003-10-31 00:00:00.000 FRANKRIKE
2812534 2003-07-08 00:00:00.000 2003-08-14 00:00:00.000 U.S.A.
2812834 2003-07-15 00:00:00.000 2004-01-15 00:00:00.000 FÖRENADE ARABEMIRATEN
2813563 2003-07-13 00:00:00.000 2003-08-06 00:00:00.000 ÅLAND
2814676 2003-07-18 00:00:00.000 2003-08-12 00:00:00.000 FINLAND
2815342 2003-07-08 00:00:00.000 2003-07-28 00:00:00.000 TYSKLAND
2816137 2003-07-10 00:00:00.000 2003-09-23 00:00:00.000 PORTUGAL
.
.
.


I´m am then trying to get at resultset that counts the number of records and
presents an output like:

[Country], [Datediff 1-14 days], [Datediff 15-30 days], [Datediff > 30 days]

I will have it groupt by Country. Can I do that in a single question or do I have to use temp tables?

>>>Kim

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-21 : 08:10:04
looks like a job for cross tabs:
http://www.sqlteam.com/item.asp?ItemID=2955

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -