Domanda

Hi helpful clever people, I am running into an issue when trying to prepare a query. I am trying to join a count of sales and a running total of sales to a prebuilt temp table.

The temp table (TMP_WEEK_SHOP) just has 2 rows, a list of week codes(WEEK_ID) and then an entry with that code for every location(SHOP_ID).

Now my issue is that no matter what i do, my queries will always omit results from the temp table if there were no sales for that location during that week. I need all entries for every week to be entered, even the 0 sales shops.

From what i can gather a left outer join should give me this, but no matter what i have tried it keeps omitting any shops without sales. I should say I am running on an SQL Server 2005 environment with Server Management Studio.

Any help at all would be fantastic!

USE CX
SELECT tws.WEEK_ID as Week, isnull(tws.SHOP_ID,0)as Shop, isnull(count(sal.SALES_ITEMS_ID),0)Sales, isnull(sum(sal.LINEVALUE),0)Sales_Value
FROM TMP_WEEK_SHOP tws
JOIN CX_DATES dat
on dat.WEEK_ID=tws.WEEK_ID
LEFT OUTER JOIN CX_SALES_ITEMS sal
on sal.DATE_ID=dat.DATE_ID
and sal.SHOP_NUM=tws.SHOP_ID
JOIN CX_STYLES sty 
on sal.STY_QUAL = sty.STY_QUAL
WHERE sty.STY_RET_TYPE='BIKES'
and (sal.SHOP_NUM='70006' or sal.SHOP_NUM='70008' or sal.SHOP_NUM='70010' or sal.SHOP_NUM='70018' or sal.SHOP_NUM='70028' or sal.SHOP_NUM='70029' or sal.SHOP_NUM='70012' or sal.SHOP_NUM='70016' or sal.SHOP_NUM='70026')
GROUP BY tws.WEEK_ID, tws.SHOP_ID
ORDER BY tws.WEEK_ID, tws.SHOP_ID
È stato utile?

Soluzione

This is your query, formatted a bit better so I can read it:

SELECT tws.WEEK_ID as Week, isnull(tws.SHOP_ID,0)as Shop,
       isnull(count(sal.SALES_ITEMS_ID),0)Sales, isnull(sum(sal.LINEVALUE),0)Sales_Value
FROM TMP_WEEK_SHOP tws JOIN
     CX_DATES dat
     on dat.WEEK_ID = tws.WEEK_ID LEFT OUTER JOIN
     CX_SALES_ITEMS sal
     on sal.DATE_ID = dat.DATE_ID and
        sal.SHOP_NUM = tws.SHOP_ID JOIN
     CX_STYLES sty 
     on sal.STY_QUAL = sty.STY_QUAL
WHERE sty.STY_RET_TYPE='BIKES' and
      (sal.SHOP_NUM in ('70006', '70008', '70010', '70018', '70028', '70029', '70012', '70016', '70026')
GROUP BY tws.WEEK_ID, tws.SHOP_ID
ORDER BY tws.WEEK_ID, tws.SHOP_ID;

You have three problems that are "undoing" the left outer join. The inner join condition will fail when sal.STY_QUAL is NULL. SImilarly, the where conditions have the same problem.

You need for all the joins to be left outer joins and to move the where conditions to on clauses:

SELECT tws.WEEK_ID as Week, isnull(tws.SHOP_ID,0)as Shop,
       isnull(count(sal.SALES_ITEMS_ID),0)Sales, isnull(sum(sal.LINEVALUE),0)Sales_Value
FROM TMP_WEEK_SHOP tws JOIN
     CX_DATES dat
     on dat.WEEK_ID = tws.WEEK_ID LEFT OUTER JOIN
     CX_SALES_ITEMS sal
     on sal.DATE_ID = dat.DATE_ID and
        sal.SHOP_NUM = tws.SHOP_ID and
        sal.SHOP_NUM in ('70006', '70008', '70010', '70018', '70028', '70029', '70012', '70016', '70026'
                        ) LEFT OUTER JOIN
     CX_STYLES sty 
     on sal.STY_QUAL = sty.STY_QUAL and
        sty.STY_RET_TYPE='BIKES'
GROUP BY tws.WEEK_ID, tws.SHOP_ID
ORDER BY tws.WEEK_ID, tws.SHOP_ID;

In addition, count() never returns a NULL value, so using isnull() or coalesce() is unnecessary.

Altri suggerimenti

your other join onvolving CX_SALES_ITEMS sal needs to be an left outer join as well

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top