Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Complex SQL Query - Expert needed.

Hi,

I need some help with a pretty complex query.  I’m using SQL 2008 R2
My starting point is this SELECT query  ( I’ll call the results returned by this DS1)

SELECT component_id, control_id, item_number, description FROM product_structures INNER JOIN items ON product_structures.component_id = items.item_id WHERE control_id = 112

Open in new window


Now the tricky part.  For every row of DS1 I need to somehow complete this logic:
1.      Use the component_id to derive another set of results (DS2).
2.      Analyse DS2 and return a single value back to DS1

I figure that one way to accomplish this would be for the first select statement to call a function.  That function would do all the processing and return the value back.
I tried working on the function but I’m struggling with analysis stage.

So far I have this:

SELECT a.title, a.status as doc_status, i.status as part_status, component_id, a.revision,
COUNT(item_serial_nos.item_id) AS serial_count,
CASE
WHEN a.status = 'A' AND i.status = 'R' AND COUNT(item_serial_nos.item_id) > 0 THEN 'G1'
WHEN a.status = 'A' AND i.status = 'R' AND COUNT(item_serial_nos.item_id) = 0 THEN 'G2'
WHEN a.status = 'N' AND i.status = 'N' THEN 'R'
ELSE 'A' END AS Cat
FROM product_structures INNER JOIN documents AS a ON product_structures.control_id = a.document_id
INNER JOIN items AS i ON product_structures.parent_id = i.item_id
LEFT OUTER JOIN item_serial_nos ON i.item_id = item_serial_nos.item_id
WHERE component_id = @component_id AND control_id != '112'
GROUP BY a.title, a.status, i.status, component_id, a.revision, i.item_number
ORDER BY i.item_number

Open in new window


Within the function I need to process the results using this logic:

If there is at least one row in DS2 where Cat = G1 then return 0 and exit
Else if there are no rows in DS2 where Cat = G1 but at least one row where Cat = G2 return 10 exit
Else if there are no G1 or G2 count the number of rows where cat = R, then divide the total number of rows where Cat = A by 2, add these values together and then divide by the total number of rows in DS2, then multiple this by 100 to return a percentage value

The end results would a number returned to the original DS1.

Any pointers or suggestions appreciated as this is well out of my depth and requires a SQL expert.
Thanks.
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

You did a good job of describing what you are trying to accomplish; however, a complex query like this is not going to be solved by "air" code.  If you want a truly helpful response you need to provide table creation scripts and data inserts for those table so that the query can be written against "real" data.  There a lot of talented query writers in here but it is in your best interest to lower the barrier of entry for them to dig in.

Brian
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The 2nd SELECT is your DS2, right?
With control_id <> 112? And you need to check it for each DS1.component_id?

Can you provide sample data for both queries (DS1 and DS2)?
And I'm afraid that I didn't get what you want to do with the function. And where to put it.