0

I have Product Table & priceDeatil table.

 Prouct Table
 ProductCode  BusinessUnit 
 10001        ORB          
 10002        ORB          

 StockRecord Table
 ProductCode    Name       StcokQuantity 
  10001         SUnSilk    1000
  10002         Pen         500

priceDeatil 
ProductCode  BusinessUnit  price   DateFrom     DateTo
10001         ORB          12.00   12-08-2011   31-09-2015
10001         ORB          21.00   01.08-2011   15-09-2011
10002         ORB          54.00   21.08-2011   15-09-2011

I want to get the product table's record,StockRecords's table & price with that product latest price

Here productCode 10001 contain 2 records when I run query it return 2 reords.I want to get only one records.

 SELECT     WMProduct.BusinessUnit, WMProduct.ProductCode, StockRecord.Name, WMPriceDetail.Price
 FROM       WMProduct INNER JOIN StockRecord
            ON WMProduct.ProductCode = StockRecord.ProductCode
           INNER JOIN WMPriceDetail
            WMPriceDetail ON WMProduct.BusinessUnit = WMPriceDetail.BusinessUnit AND WMPriceDetail.ProductCode = WMProduct.ProductCode

 WHERE     (WMPriceDetail.DateFrom < GETDATE()) AND (WMPriceDetail.DateTo > GETDATE() OR
            WMPriceDetail.DateTo = NULL)

This query returns

   BusinessUnit ProductCode     Name   Price
     ORB         10001         SunSilk    12.00
     ORB         10001         SunSilk    21.00

But I need only one records.... BusinessUnit ProductCode Name Price ORB 10001 SunSilk 21.00 ORB 10002 Pen 54.00

From that query i can't put TOP 1 because there are more table combine.. join

If the price is not define in the price detail table, that records should not show into the list.

Please help me....

Thanks in advance

3 Answers 3

1

how i uderstood you need only max price from priceDetail:

SELECT WMProduct.BusinessUnit,
       WMProduct.ProductCode ,
       WMProduct.Description ,
       price_detail.price
FROM   WMProduct
       INNER JOIN
              ( SELECT  MAX(Price) AS price,
                       BusinessUnit        ,
                       ProductCode
              FROM     WMPriceDetail
              GROUP BY BusinessUnit,
                       ProductCode
              )
              price_detail
       ON     price_detail.BusinessUnit = WMProduct.BusinessUnit
       AND    price_detail.ProductCode  = WMProduct.ProductCode
WHERE  (
              WMPriceDetail.DateFrom < GETDATE()
       )
AND
       (
              WMPriceDetail.DateTo > GETDATE()
       OR     WMPriceDetail.DateTo = NULL
       )
Sign up to request clarification or add additional context in comments.

3 Comments

It say The multi-part identifier "WMPriceDetail.DateFrom" could not be bound.. And anotherthing not 'MAX(Price)' . It should be 'MAX(DateFrom)'
a forgot about where =) add in subselect fields which you need and not forget about groupping
I think you assumption is wrong.I need latest define price(DateFrom) and that date rage should not be finish date(compare with GETDATE()).Not working.I got error msg
0

You can use a top 1 with a join...

Just do select top 1 etc. as you've already done. so

select top 1 a.name, b.name from table1 a join table2 b on a.id=b.id

Your second problem is that you're not necessarily going to get the correct row returned as you aren't ordering the results before doing the top 1.

--Update

Seems sqlite doesn't understand top but does understand LIMIT. See http://www.sqlite.org/lang_select.html

Note that you will still need to use an order by clause

Comments

0
+50

Try this friend:

 select p.businessunit, p.productcode, pr.price from
 WMProduct p ,WMStockRecord s, WMPriceDetail pr
 where p.productcode = s.productcode and
    s.productcode = pr.productcode and p.productcode=pr.productcode and
    pr.datefrom = (select max(datefrom) from WMPriceDetail where productcode = p.productcode  and
  (
    ( DateFrom < 'Sep 06 2011') AND (DateTo > 'Sep 06 2011' OR DateTo IS NULL) 
  ) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.