| 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 number1-50 2651-100 62etc......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 |
 |
|
|
aneeshmm
Starting Member
20 Posts |
Posted - 2008-12-22 : 07:25:47
|
| SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOIF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Company_Data]') AND type in (N'U'))BEGINCREATE 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 |
 |
|
|
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 50union allselect '51 - 100' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 51 and 100etc...[/code]Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
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 50union allselect '51 - 100' as criteria, Number_Of_Employees as number from Company_Data where Number_Of_Employees between 51 and 100etc... 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 |
 |
|
|
aneeshmm
Starting Member
20 Posts |
Posted - 2008-12-22 : 07:36:42
|
| thnx buddy.... |
 |
|
|
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 ALLSELECT 51 As lowLevel,100 AS highLevel, '51-100' AS Level UNION ALLSELECT 101 As lowLevel ,150 AS highLevel, '101-150' AS Level ) sLEFT JOIN company_data t ON t.noofemployees between s.lowlevel and s.highlevelGROUP BY s.levelORDER BY CAST(SUBSTRING(s.level,1,CHARINDEX('-',s.level,1)-1) AS INT)Jai Krishna |
 |
|
|
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 ALLSELECT 51 As lowLevel,100 AS highLevel, '51-100' AS Level UNION ALLSELECT 101 As lowLevel ,150 AS highLevel, '101-150' AS Level ) sLEFT JOINcompany_data t ON t.noofemployees between s.lowlevel and s.highlevelGROUP BY s.levelORDER BY s.levelhi, keep distinct before companyname while getting count |
 |
|
|
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)tgroup by t.range[/code]Webfredthx bklr to point me what OP wants... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
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)tgroup by t.range Webfredthx bklr to point me what OP wants... No, you're never too old to Yak'n'Roll if you're too young to die.
welcomeI 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 |
 |
|
|
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... |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-22 : 23:52:44
|
Welcome Jai Krishna |
 |
|
|
|