Unix Technical Forum

SQL select statement needed

This is a discussion on SQL select statement needed within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a table consisting of two fields, OStype and OSversion, with entries like: OStype OSversion solaris 2.5 solaris ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 09:10 PM
ian.l.guthrie@gmail.com
 
Posts: n/a
Default SQL select statement needed

I have a table consisting of two fields, OStype and OSversion, with
entries like:

OStype OSversion
solaris 2.5
solaris 2.6
redhat 6.2
redhat 6.2
solaris 8
redhat AS4
solaris 10
solaris 10
redhat AS2.1
redhat AS3
redhat AS4

I want to create a select statement that returns for each OS type, the
total number of entries and for each version the total number of
entries.

In the example the result would be:

OStype OStype Count OSversion OSversion Count
solaris 5
2.5 1
2.6 1
8 1
10 2
redhat 6
6.2 2
AS2.1 1
AS3 1
AS4 2


Thanks in advance for your help.

Ian

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 09:10 PM
David Portas
 
Posts: n/a
Default Re: SQL select statement needed

ian.l.guthrie@gmail.com wrote:
> I have a table consisting of two fields, OStype and OSversion, with
> entries like:
>
> OStype OSversion
> solaris 2.5
> solaris 2.6
> redhat 6.2
> redhat 6.2
> solaris 8
> redhat AS4
> solaris 10
> solaris 10
> redhat AS2.1
> redhat AS3
> redhat AS4
>
> I want to create a select statement that returns for each OS type, the
> total number of entries and for each version the total number of
> entries.
>
> In the example the result would be:
>
> OStype OStype Count OSversion OSversion Count
> solaris 5
> 2.5 1
> 2.6 1
> 8 1
> 10 2
> redhat 6
> 6.2 2
> AS2.1 1
> AS3 1
> AS4 2
>
>
> Thanks in advance for your help.
>
> Ian


Try CUBE/ROLLUP:

SELECT ostype, osversion, COUNT(*) AS cnt
FROM tbl
GROUP BY ostype, osversion
WITH ROLLUP ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 09:11 PM
Madhivanan
 
Posts: n/a
Default Re: SQL select statement needed


Also if you want to suppress the result, use front end application

Madhivanan


ian.l.guthrie@gmail.com wrote:
> I have a table consisting of two fields, OStype and OSversion, with
> entries like:
>
> OStype OSversion
> solaris 2.5
> solaris 2.6
> redhat 6.2
> redhat 6.2
> solaris 8
> redhat AS4
> solaris 10
> solaris 10
> redhat AS2.1
> redhat AS3
> redhat AS4
>
> I want to create a select statement that returns for each OS type, the
> total number of entries and for each version the total number of
> entries.
>
> In the example the result would be:
>
> OStype OStype Count OSversion OSversion Count
> solaris 5
> 2.5 1
> 2.6 1
> 8 1
> 10 2
> redhat 6
> 6.2 2
> AS2.1 1
> AS3 1
> AS4 2
>
>
> Thanks in advance for your help.
>
> Ian


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 09:11 PM
ian.l.guthrie@gmail.com
 
Posts: n/a
Default Re: SQL select statement needed

Thanks David (and all)...that worked!

-Ian
David Portas wrote:
> ian.l.guthrie@gmail.com wrote:
> > I have a table consisting of two fields, OStype and OSversion, with
> > entries like:
> >
> > OStype OSversion
> > solaris 2.5
> > solaris 2.6
> > redhat 6.2
> > redhat 6.2
> > solaris 8
> > redhat AS4
> > solaris 10
> > solaris 10
> > redhat AS2.1
> > redhat AS3
> > redhat AS4
> >
> > I want to create a select statement that returns for each OS type, the
> > total number of entries and for each version the total number of
> > entries.
> >
> > In the example the result would be:
> >
> > OStype OStype Count OSversion OSversion Count
> > solaris 5
> > 2.5 1
> > 2.6 1
> > 8 1
> > 10 2
> > redhat 6
> > 6.2 2
> > AS2.1 1
> > AS3 1
> > AS4 2
> >
> >
> > Thanks in advance for your help.
> >
> > Ian

>
> Try CUBE/ROLLUP:
>
> SELECT ostype, osversion, COUNT(*) AS cnt
> FROM tbl
> GROUP BY ostype, osversion
> WITH ROLLUP ;
>
> --
> David Portas, SQL Server MVP
>
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
>
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
> --


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:12 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com