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 > Data Access, Manipulation & Batch Languages > ANSI SQL > can i make one query out of these two?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-03-03, 12:00
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
can i make one query out of these two?

i have two views. the first finds related images for products, but only if the image is the primary image. here it is:

CREATE VIEW images AS
SELECT
p.product_id,
pI.productImage_title as productImage_title
FROM
dbo.products p INNER JOIN
dbo.productImages pI ON p.product_id = pI.product_id
WHERE pI.productImage_primary = 1

the second view gets all the products, and uses the first view to find an image, if there is one:

SELECT
p.product_id as product_id,
p.product_partNumber as part_number,
p.product_name as product_name,
i.productImage_title as small_image,
FROM
dbo.products p LEFT OUTER JOIN
dbo.images i ON p.product_id = i.product_id

i would like to be able to do this with just one view. i'm not sure how to do it though while still limiting the images to only primary ones.

thanks for any help
Reply With Quote
  #2 (permalink)  
Old 06-03-03, 12:05
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Re: can i make one query out of these two?

If this in Oracle, you could use inline views as follows :

Code:
SELECT     
  p.product_id as product_id,
  p.product_partNumber as part_number,
  p.product_name as product_name,
  i.productImage_title as small_image,
FROM
  dbo.products p,
  (SELECT p.product_id,
               pI.productImage_title as productImage_title
    FROM dbo.products p INNER JOIN
             dbo.productImages pI ON p.product_id = pI.product_id
    WHERE pI.productImage_primary = 1) i
WHERE p.product_id = i.product_id(+)
Quote:
Originally posted by kfenstad
i have two views. the first finds related images for products, but only if the image is the primary image. here it is:

CREATE VIEW images AS
SELECT
p.product_id,
pI.productImage_title as productImage_title
FROM
dbo.products p INNER JOIN
dbo.productImages pI ON p.product_id = pI.product_id
WHERE pI.productImage_primary = 1

the second view gets all the products, and uses the first view to find an image, if there is one:

SELECT
p.product_id as product_id,
p.product_partNumber as part_number,
p.product_name as product_name,
i.productImage_title as small_image,
FROM
dbo.products p LEFT OUTER JOIN
dbo.images i ON p.product_id = i.product_id

i would like to be able to do this with just one view. i'm not sure how to do it though while still limiting the images to only primary ones.

thanks for any help
Reply With Quote
  #3 (permalink)  
Old 06-03-03, 12:07
kfenstad kfenstad is offline
Registered User
 
Join Date: May 2003
Posts: 25
i am working in SQL server.
Reply With Quote
  #4 (permalink)  
Old 06-03-03, 12:14
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Sorry, I cant help you with exact syntax but SQL Server also supports inline views -- dont know which version. May be, you can look into the documentation.

Quote:
Originally posted by kfenstad
i am working in SQL server.
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