Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Combining a few results while using a function

Author  Topic 

jose1lm
Yak Posting Veteran

70 Posts

Posted - 2003-07-25 : 18:01:31
I need help on combining the table/views into the results (view) I’m looking for.

Table: job

Job_id | status
133 | Complete
501 | Complete
502 | Complete
503 | Complete
504 | Complete
505 | Complete
etc etc

View: vw_ffd_form_102

Job_id | txtSection13Element3
501 | 0
503 | 0
504 | 90
506 | 288
etc etc

View: vw_ffd_form_47

Job_id | txtSection31Element3
133 | 1
502 | 154
505 | 192
etc etc


There is a function I use that will convert those text section numbers into something else we use. Here is that select statement and the results it returns when reading from just one table/view:

SELECT job_id, dbo.fn_dd_Product(txtSection13Element3) AS txtSection13Element3
FROM dbo.vw_ffd_form_102
ORDER BY job_id

Job_id | txtSection13Element3
501 | other
503 | other
504 | DR1000
506 | Vacuum_Breaker
etc etc.


Now the results I would like to see should look like this:

Job_id | status | Product
133 | Complete | unkown
501 | Complete | other
502 | Complete | L5000
503 | Complete | other
504 | Complete | DR1000
505 | Complete | NS1000E
506 | Open | Vacuum Breaker
etc etc


Here is the query I wrote but it doesn’t work correctly at all, not to mention that I couldn’t get the function to work in this statement so I had it just return the number instead of the conversion that I would like. I feel I should’ve used a UNION somewhere. The ‘product’ columns should’ve only be listed once, not twice:

SELECT a.job_id, a.status,
(SELECT txtSection13Element3 FROM vw_ffd_form_102 b WHERE b.job_id = a.job_id) AS Product,
(SELECT txtSection31Element3 FROM vw_ffd_form_47 b WHERE b.job_id = a.job_id) AS Product
FROM job a

Job_id | status | Product | Product
133 | Complete | Null | 1
501 | Complete | 0 | Null
502 | Complete | Null | 154
503 | Complete | 0 | Null
504 | Complete | 90 | Null
505 | Complete | Null | 192
506 | Open | 288 | Null


I didn’t past the function code here because it’s awfully long, but if it is required, let me know.

Thanks in advance for your help guys! This place is a great.


MichaelP
Jedi Yak

2489 Posts

Posted - 2003-07-25 : 18:53:55
I think that this will work:


SELECT j.job_id, j.Status, dbo.fn_dd_Product(COALESCE(v1.txtSection13Element3 , v2.txtSection31Element3)) AS Product
FROM Job j
LEFT JOIN dbo.vw_ffd_form_102 v1 ON v1.job_id = j.job_id
LEFT JOIN vw_ffd_form_47 v2 ON v2.job_id = j.job_id
ORDER BY j.job_id


Basically what it does is gives you an output similar to what your last query did, except it takes those two product fields, and puts them into one field. COALESCE gets the first non-null value in the list of things that you give it. It will then pass that value to your handy UDF, and you'll get the results you need.

Let us know if this works or not!

Michael



<Yoda>Use the Search page you must. Find the answer you will.</Yoda>

Edited by - michaelp on 07/25/2003 18:55:19
Go to Top of Page

jose1lm
Yak Posting Veteran

70 Posts

Posted - 2003-07-28 : 11:00:52
Thanks Michael, it works great!

I had this working by doing seperate views rather than just 1 view as you have shown me.

CREATE VIEW dbo.product_line
SELECT job_id, dbo.fn_dd_Product(txtSection13Element3) AS Product
FROM dbo.vw_ffd_form_102
UNION
SELECT job_id, dbo.fn_dd_Product(txtSection31Element3) AS Product
FROM dbo.vw_ffd_form_47

----------------

CREATE VIEW dbo.product_jobs
SELECT dbo.JOB.job_id, dbo.JOB.status, dbo.vw_product_line.Product
FROM dbo.JOB LEFT OUTER JOIN dbo.vw_product_line ON dbo.JOB.job_id = dbo.vw_product_line.job_id
ORDER BY dbo.JOB.job_id

Thanks again!

JLM
Go to Top of Page
   

- Advertisement -