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
 General SQL Server Forums
 New to SQL Server Programming
 How to query 3 table to calculate number of hits ?

Author  Topic 

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-10-22 : 11:19:33
Hi
Below is my scenario..

Columns-------------------------------Tables
BProdID, URL, SearchTerms,BehaveID - TblBrowse
PProdID,SKU - TblProduct
OrdID,OProdID,QTY,Price - TblOrder

TblBrowse -This table log all the hits for the products
TblProduct - This table has all the products
TblOrder - This table has all the order placed online

BProdID & OProdID contains the product ID from PProdID of TblProduct.

I need to calculate How many hits per product, How many times sold that product, if sold what is the total amount of SALES for that product.

Exmple: -


Hits --Code------NumOfSales-----Amount
433 ---AB56TF------20------------£3450
322----GH78-OP-----24------------£1960
13----RR5t6Y------32------------£2980

I am using the below code without success, please help :

SELECT COUNT(B.BProdID) AS Hits, B.URL, B.Searchterms,P.Sku as Code, Count(O.OrdID) As NumOfSales ,Sum(O.Price) AS Amount , Sum(O.QTY) As QTY
FROM TblOrder O RIGHT OUTER JOIN
TblBrowse B RIGHT OUTER JOIN
TblProduct P ON COALESCE(B.BProdID,0) = P.PProdID
ON COALESCE(B.BProdID,0) = COALESCE(O.OProdID,0)

WHERE B.BehaveID = 2
GROUP BY O.Price,B.BProdID,P.Sku,O.QTY
ORDER BY Hits DESC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-22 : 11:32:31
[code]SELECT b.NoOfHits,p.SKU AS Code,o.NoOfSales,o.TotalPrice
FROM tblProduct p
INNER JOIN(SELECT OProdID,SUM(Price) AS TotalPrice,SUM(Qty) AS TotalQty,COUNT(OrdID) AS NoOfSales
FROM tblOrder
GROUP BY OProdID)o
ON o.OProdID=p.PProdID
INNER JOIN (SELECT BProdID,COUNT(*) AS NoOfHits
FROM tblOrder
GROUP BY BProdID)b
ON b.BProdID=p.PProdID[/code]
Go to Top of Page

Vaishu
Posting Yak Master

178 Posts

Posted - 2008-10-23 : 04:29:37
Hi

I changed little bit and works fine. THANKS A LOT

quote:
Originally posted by visakh16

SELECT b.NoOfHits,p.SKU AS Code,o.NoOfSales,o.TotalPrice
FROM tblProduct p
INNER JOIN(SELECT OProdID,SUM(Price) AS TotalPrice,SUM(Qty) AS TotalQty,COUNT(OrdID) AS NoOfSales
FROM tblOrder
GROUP BY OProdID)o
ON o.OProdID=p.PProdID
INNER JOIN (SELECT BProdID,COUNT(*) AS NoOfHits
FROM tblOrder
GROUP BY BProdID)b
ON b.BProdID=p.PProdID


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-23 : 04:34:27
cheers
Go to Top of Page
   

- Advertisement -