Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

How to display following matrix?

This is a discussion on How to display following matrix? within the SQL forums, part of the Databases category; Need to create a query to display the total number of employees and the number of employees hired in 1995, 1996, 1997 and 1998. i have difficulty of creating following ...

Go Back   Geeks Talk > Databases > SQL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 03-07-2007
Junior Member
 
Join Date: Feb 2007
Posts: 8
Thanks: 2
Thanked 1 Time in 1 Post
orlando is on a distinguished road
How to display following matrix?

Need to create a query to display the total number of employees and the number of employees hired in 1995, 1996, 1997 and 1998. i have difficulty of creating following format. not sure how to use group by clause
Total | 1995 | 1996 | 1997 | 1998
20 | 1 | 2 | 2 | 3

Thank you,
Orlando

Last edited by orlando; 03-08-2007 at 05:30 PM.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2007
Expert Member
 
Join Date: Sep 2006
Location: India
Posts: 131
Thanks: 1
Thanked 21 Times in 20 Posts
Innila is on a distinguished road
Re: How to display following matrix?

Hi Orlando,

Group by clause will help you retrieving the query for total number of employees hired in each year.
Formatting the Query results will give you the overall total number of employees.
The below code will satisfy this,
Code:
break on report skip 2
compute sum label 'Total' of "NO OF EMP" on report
select '           ', to_char(hiredate,'yyyy') "YEAR", count(*) "NO OF EMP" from emp
group by to_char(hiredate,'yyyy')
/

''          YEAR  NO OF EMP
----------- ---- ----------
            1980          1
            1981         10
            1982          1
            1987          2
            1999          1
                 ----------
TOTAL                    19
If you want to know more about Group by, i suggest you to go through the following link,
http://www.techonthenet.com/sql/group_by.php

Guess i've got your problem right.
Hopefully the SQL query & the link helps you.
__________________
*** Innila ***

Last edited by Innila; 03-08-2007 at 07:21 AM.
Reply With Quote
  #3 (permalink)  
Old 03-08-2007
Junior Member
 
Join Date: Feb 2007
Posts: 8
Thanks: 2
Thanked 1 Time in 1 Post
orlando is on a distinguished road
Re: How to display following matrix?

Hi, Innila,
Thank you for your answer.
Your result data are what they want, but the problem ask to display the Total and years as first row, and the numbers are on the second row. I can not figure out how to display this format?
Orlando
Reply With Quote
  #4 (permalink)  
Old 03-12-2007
Junior Member
 
Join Date: Mar 2007
Location: australia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
bbkcdb is on a distinguished road
Re: How to display following matrix?

select AVG(tax) from invoice where type='sales tax'
what is the error n above query
Reply With Quote
  #5 (permalink)  
Old 03-13-2007
Junior Member
 
Join Date: Feb 2007
Posts: 8
Thanks: 2
Thanked 1 Time in 1 Post
orlando is on a distinguished road
Re: How to display following matrix?

Hi,
Here is my solution. I would like to share it.
select count(*) as Total,
count ( case when to_char(hire_date,'YYYY') = '1995' then hire_date else NULL end) as "1995",
count ( case when to_char(hire_date,'YYYY') = '1996' then hire_date else NULL end) as "1996",
count ( case when to_char(hire_date,'YYYY') = '1997' then hire_date else NULL end) as "1997",
count ( case when to_char(hire_date,'YYYY') = '1998' then hire_date else NULL end) as "1998"
from employees

June
Reply With Quote
Reply

  Geeks Talk > Databases > SQL

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Display n-1 rows from a table dbtester SQL Server 3 02-07-2008 07:11 AM
Display data in datagrid JobHelper VB.NET 2 06-05-2007 09:10 AM
Display record ordered by the day of the week orlando SQL 3 03-01-2007 02:04 AM
Need predefined Function or code for Data Display in PHP norman PHP 0 07-17-2006 04:17 PM


All times are GMT -4. The time now is 04:15 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved