Solved

Recursive Query Help

Posted on 2008-10-07
10
191 Views
Last Modified: 2012-05-05
I have this query that gives me the postingFacilityID and total openings.

SELECT DISTINCT j.postingFacilityID, SUM(j.true_job_cnt) AS tot_openings, ff.facilityName
FROM jobInfo j, facilities f
WHERE j.display = 'Y'
            AND j.dateClose >= GetDate()
            AND j.facilityID = f.facilityID
            and f.statusid = 1
            and f.deleted = 0
GROUP BY j.postingFacilityID
ORDER BY SUM(j.true_job_cnt) DESC

What I want is to be able to use the resultant postingFacilityID and get the facility name which would be in the facilities table as "facilityName".
0
Comment
Question by:coldchillin
  • 4
  • 4
  • 2
10 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22661552
You've got a FacilityName in your list of fields ... is it returning the wrong thing?
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22661561
Try:


SELECT j.postingFacilityID, f.facilityName, SUM(j.true_job_cnt) AS tot_openings

FROM jobInfo j

inner join facilities f

on j.facilityID = f.facilityID

WHERE j.display = 'Y'

  AND j.dateClose >= GetDate()

  and f.statusid = 1

  and f.deleted = 0

GROUP BY j.postingFacilityID , f.facilityName

ORDER BY SUM(j.true_job_cnt) DESC

Open in new window

0
 
LVL 1

Author Comment

by:coldchillin
ID: 22662314
I tried that as well, but with no luck. The reason why I can't just include it is because I need the DISTINCT item.

The records are grouped by "corporation", where all members belong to the corporate record will be aggregated and displayed under the "corporate" name.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22662495
Can you post a few rows of sample data?  My code would have looked a lot like Brandon's from what I understand ... so I'm missing something.
0
 
LVL 1

Author Comment

by:coldchillin
ID: 22663218
Ok, initial query I posted returns this:
postingFacilityID        tot_openings
11                  559
158                  48
211                   30

The suggested query returns this:
postingFacilityID
11              name1      117
11              name2      102
11              name3      69
11              name4      68
11              name5      50

The problem with the second query is that all those records with the postingFacilityID of 11 should be aggregated. The actual entity with the ID 11 will have the name "Name0"
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22663817
Can you post the definitions of jobInfo and facilities?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22677735
Is there more than one FacilityName for a FacilityID?
0
 
LVL 1

Author Comment

by:coldchillin
ID: 22683164
The facility table has the following relevant fields:
facilityID, corporateID, facilityName

The jobInfo table has the following relevant field:
postingFacilityID

The posted query works fine, but what I then need to do is go back and using the resultant postingFacilityID, match it back to the facilities table to the facilityID, and get the facilityName
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22686556
The posted query works fine, but what I then need to do is go back and using the resultant postingFacilityID, match it back to the facilities table to the facilityID, and get the facilityName
Understood.  It's that matching up that is missing some criterion.


The suggested query returns this:
postingFacilityID
11              name1      117
11              name2      102
11              name3      69
11              name4      68
11              name5      50

The problem with the second query is that all those records with the postingFacilityID of 11 should be aggregated. The actual entity with the ID 11 will have the name "Name0"
Why?  Where does "Name0" come from?

Are you using Brandon's query exactly as he posted it?  Or are you modifying it?  If modifying, please post the modification.  

Perhaps you've simplified the problem for us ... a little too much.
0
 
LVL 1

Author Comment

by:coldchillin
ID: 22689642
Ahhh...I see what happened!

Brandon, the query you posted uses j.facilityid = f.facilityid, but what it needed to use was j.postingFacilityID = f.facilityID
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now