If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > query help please!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-04, 10:04
manth manth is offline
Registered User
 
Join Date: Mar 2004
Posts: 54
query help please!!

Hi,
I have 2 tables for example EMP and DEPT

EMP DEPT
------- --------
empno deptno
empname dept_name
deptno
job

i want result as all the department names and corresponding
employees count for the given job title.

deptname count
-------- --------
sales 100
accounting 20
research 10

I am not good at SQL, please help me!

Thanks
Reply With Quote
  #2 (permalink)  
Old 03-31-04, 10:15
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: query help please!!

If you have tried a query and found it throwing errors, please post the same ...

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 03-31-04, 10:33
manth manth is offline
Registered User
 
Join Date: Mar 2004
Posts: 54
I tried

O.K. let me give my actual problem where i am working on.

i have the following tables

CFGHDRLOG CFGSERVICE
-------------- ------------------
MSGID SERVICEID
LOGONID SERVICE_NAME
CHANNELID
SERVICEID


I tried the following query
SELECT SERVICEID, COUNT(*) FROM DB2A.CFGHDRLOG WHERE CHANNELID=3482 GROUP BY SERVICEID

i got the follwing result

SERVICEID 2
--------------- -------
34 2
27 6
20 8

now i want display SERVICE_NAME instead of SERVICEID, which is in CFGSERVICE table?

i coud not figure it out!!!
Reply With Quote
  #4 (permalink)  
Old 03-31-04, 10:43
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by manth
I tried

O.K. let me give my actual problem where i am working on.

i have the following tables

CFGHDRLOG CFGSERVICE
-------------- ------------------
MSGID SERVICEID
LOGONID SERVICE_NAME
CHANNELID
SERVICEID


I tried the following query
SELECT SERVICEID, COUNT(*) FROM DB2A.CFGHDRLOG WHERE CHANNELID=3482 GROUP BY SERVICEID

i got the follwing result

SERVICEID 2
--------------- -------
34 2
27 6
20 8

now i want display SERVICE_NAME instead of SERVICEID, which is in CFGSERVICE table?

i coud not figure it out!!!

SELECT LOG.SERVICEID, SVC.SERVICE_NAME, COUNT(*) FROM DB2A.CFGHDRLOG LOG, DB2A.CFGSERVICE SVC WHERE LOG.CHANNELID=3482 AND LOG.SERVICEID = SVC.SERVICEID GROUP BY LOG.SERVICEID
Reply With Quote
  #5 (permalink)  
Old 03-31-04, 10:52
manth manth is offline
Registered User
 
Join Date: Mar 2004
Posts: 54
I tried it i am getting the following error

SQL0119N An expression starting with "SERVICE_NAME" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified. SQLSTATE=42803
Reply With Quote
  #6 (permalink)  
Old 03-31-04, 11:15
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by manth
I tried it i am getting the following error

SQL0119N An expression starting with "SERVICE_NAME" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified. SQLSTATE=42803
Yes, that's my mistake. The error message is sort of self-explanatory, isn't it? Add "svc.service_name" to the GROUP BY clause.
Reply With Quote
  #7 (permalink)  
Old 03-31-04, 11:29
manth manth is offline
Registered User
 
Join Date: Mar 2004
Posts: 54
i did this way as u said

db2 => SELECT LOG.SERVICEID, SVC.SERVICE_NAME, COUNT(*) FROM DB2A.CFGHDRLOG LOG, DB2A.CFGSERVICE SVC WHERE LOG.CHANNELID=3482 AND LOG.SERVICEID = SVC.SERVICEID GROUP BY SVC.SERVICE_NAME

SQL0119N An expression starting with "SERVICEID" specified in a SELECT clause,HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified. SQLSTATE=42803

i got the same kind of error!!
Reply With Quote
  #8 (permalink)  
Old 03-31-04, 11:34
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
Quote:
Originally posted by manth
i did this way as u said

db2 => SELECT LOG.SERVICEID, SVC.SERVICE_NAME, COUNT(*) FROM DB2A.CFGHDRLOG LOG, DB2A.CFGSERVICE SVC WHERE LOG.CHANNELID=3482 AND LOG.SERVICEID = SVC.SERVICEID GROUP BY SVC.SERVICE_NAME

SQL0119N An expression starting with "SERVICEID" specified in a SELECT clause,HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified. SQLSTATE=42803

i got the same kind of error!!
He said "add", you replaced... use both in the GROUP BY.
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #9 (permalink)  
Old 03-31-04, 11:34
beepyata beepyata is offline
Registered User
 
Join Date: Feb 2004
Posts: 9
Try this

SELECT SVC.SERVICE_NAME, LOG.SERVICEID,COUNT(*) FROM DB2A.CFGHDRLOG LOG, DB2A.CFGSERVICE SVC WHERE LOG.CHANNELID=3482 AND LOG.SERVICEID = SVC.SERVICEID GROUP BY SVC.SERVICE_NAME, LOG.SERVICEID

Cheers
Reply With Quote
  #10 (permalink)  
Old 03-31-04, 13:29
manth manth is offline
Registered User
 
Join Date: Mar 2004
Posts: 54
Thumbs up

It's working fine.

Thank you very much!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On