Solved

Recursive Query Help

Posted on 2008-10-07
10
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Suggested Courses

751 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