views:

51

answers:

2

Hi,

I have query which provide wrong result, am I doing any thig wrong in this query

SELECT   b.nBoutiqueID                       ,
         b.sBoutiqueName                     ,
         b.Status                            ,
         SUM(bs.nViewCount)      nViewCount       ,
         SUM(ps.nViewCount)      nProductViewCount,
         SUM(ps.nLinkClickCount) nLinkClickCount  ,
         SUM(ps.nWishListCount)  nWishListCount   ,
         SUM(ps.nReferredCount)  nReferredCount
FROM     boutique b
         LEFT JOIN boutique_stats bs
         ON       b.nBoutiqueID=bs.nBoutiqueID
         LEFT JOIN product_stats ps
         ON       ps.nBoutiqueID=b.nBoutiqueID
WHERE    b.bDeleted             =0
GROUP BY b.nBoutiqueID
ORDER BY ps.nProductID DESC

Query giving not any error, but producing wrong result. I'm Using Mysql.

For particular Instance for nBoutiqueID=1 the max sum of nViewCount should be 455, but it gives 95124. that is huge difference. any one know why?

+3  A: 

It appears you are getting a sort-of Cartesian product of the query... Try getting your SUM() values from your sub-queries...

SELECT
      b.nBoutiqueID, 
      b.sBoutiqueName, 
      b.Status, 
      bs.StatsViewCount,
      ps.ProductViewCount, 
      ps.ProductLinkClickCount, 
      ps.ProductWishListCount, 
      ps.ProductReferredCount 
   FROM     
      boutique b 
         LEFT JOIN ( select nBoutiqueID, sum( nViewCount ) as StatsViewCount
                        from boutique_stats 
                        group by nBoutiqueID ) bs 
            ON b.nBoutiqueID = bs.nBoutiqueID 
         LEFT JOIN ( select SUM(nViewCount) ProductViewCount, 
                             SUM(nLinkClickCount) ProductLinkClickCount, 
                             SUM(nWishListCount) ProductWishListCount, 
                             SUM(nReferredCount)  ProductReferredCount 
                        from product_stats 
                        group by nBoutiqueID ) ps 
            ON ps.nBoutiqueID=b.nBoutiqueID 
   WHERE    
      b.bDeleted = 0 
   ORDER BY 
      ps.nProductID DESC 
DRapp
Thanks Drapp, That query is awesome. I see that type of query first time. Can you pass any link so that i can go more with sub-query in Joins? Any ways thanks.
Naresh
No direct links, just personal experience as with all the others out here offering solutions. However, I've found a few from my historical posts to offer.
DRapp
http://stackoverflow.com/questions/3771179 shows a way of doing a cross-tab from rows to columns http://stackoverflow.com/questions/2312608 for query optimizing technique http://stackoverflow.com/questions/2267791 for index timing, http://stackoverflow.com/questions/3352882 for non-standard ordering clause... can also be used as selecting value as columns,.. Just keep an eye out for others looking for answers and those that are offered.
DRapp
+1  A: 

You say "max nViewCount should be 455, but it gives 95124".

But in your query you have SUM(bs.nViewCount) nViewCount,.

Shouldn't that be MAX(bs.nViewCount) nViewCount, ?

ceteras