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 2005 Forums
 Transact-SQL (2005)
 get number of records

Author  Topic 

aneeshmm
Starting Member

20 Posts

Posted - 2008-12-22 : 07:07:25
please help............

i have a table with company_name and number_of_employees as the fields.
i want to show a table having the following result:
criteria number
1-50 26
51-100 62
etc......
ie, i have to get count of companies having employee number between 1 and 50 and so on...
the criteria in the left are static...

thanx in advance

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-22 : 07:15:20
can u get the tables structure and data of ur tables
Go to Top of Page

aneeshmm
Starting Member

20 Posts

Posted - 2008-12-22 : 07:25:47
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Company_Data]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Company_Data](
[Company_Name] [nvarchar](200) NOT NULL,
[Registration_No] [nvarchar](10) NULL,
[Registration_Year] [nvarchar](50) NULL,
[Number_Of_Employees] [int] NULL,
[Hours_Of_Business] [nchar](100) NULL,
CONSTRAINT [PK_Company_Data] PRIMARY KEY CLUSTERED
(
[Serial] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-12-22 : 07:32:14
[code]
select '1 - 50' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 1 and 50
union all
select '51 - 100' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 51 and 100
etc...
[/code]

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-22 : 07:34:35
quote:
Originally posted by webfred


select '1 - 50' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 1 and 50
union all
select '51 - 100' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 51 and 100
etc...


Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.



he wants the count of companies where emp ranges for 1-50 like that
just check once
Go to Top of Page

aneeshmm
Starting Member

20 Posts

Posted - 2008-12-22 : 07:36:42
thnx buddy....
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-22 : 07:39:02
SELECT s.level,COUNT(t.companyname) AS 'NO.Of Companies' FROM
(SELECT 1 As lowLevel,50 AS highLevel,'1-50' AS Level UNION ALL
SELECT 51 As lowLevel,100 AS highLevel, '51-100' AS Level UNION ALL
SELECT 101 As lowLevel ,150 AS highLevel, '101-150' AS Level ) s
LEFT JOIN
company_data t ON t.noofemployees between s.lowlevel and s.highlevel
GROUP BY s.level
ORDER BY CAST(SUBSTRING(s.level,1,CHARINDEX('-',s.level,1)-1) AS INT)

Jai Krishna
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-12-22 : 07:55:53
SELECT s.level,COUNT( distinct t.companyname) AS 'NO.Of Companies' FROM
(SELECT 1 As lowLevel,50 AS highLevel,'1-50' AS Level UNION ALL
SELECT 51 As lowLevel,100 AS highLevel, '51-100' AS Level UNION ALL
SELECT 101 As lowLevel ,150 AS highLevel, '101-150' AS Level ) s
LEFT JOIN
company_data t ON t.noofemployees between s.lowlevel and s.highlevel
GROUP BY s.level
ORDER BY s.level

hi, keep distinct before companyname while getting count
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-12-22 : 08:07:59
[code]
select t.range as criteria, count(*) as number from
(select Company_Name,
case when Number_Of_Employees between 1 and 50 then '1-50'
when Number_Of_Employees between 51 and 100 then '51-100'
...etc...
else '> some_number' end as range
from Company_Data)t
group by t.range
[/code]

Webfred
thx bklr to point me what OP wants...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-22 : 23:05:21
quote:
Originally posted by webfred


select t.range as criteria, count(*) as number from
(select Company_Name,
case when Number_Of_Employees between 1 and 50 then '1-50'
when Number_Of_Employees between 51 and 100 then '51-100'
...etc...
else '> some_number' end as range
from Company_Data)t
group by t.range


Webfred
thx bklr to point me what OP wants...


No, you're never too old to Yak'n'Roll if you're too young to die.



welcome
I think ur query will works
he wants only the count of companies where employees ranges from 1-50,50-100....
just one modification webfred,
keep distinct for count of company name in place of * in count
Go to Top of Page

aneeshmm
Starting Member

20 Posts

Posted - 2008-12-22 : 23:50:18
thanks all for such a great response...
i could blend all these queries to my requirement...
jai krishna's post worked wonderfully by providing the correct order for the result...
thnx again...
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-22 : 23:52:44
Welcome

Jai Krishna
Go to Top of Page
   

- Advertisement -