Subject Re: How can I avoid temporary tables?
Author c_pradelli
Hi Dimitry!

First at all, thank you for your responce.:)

>You also can try to denormalize your data moving sell-date into
>items. It would remove one table from SELECT and increase
> selectivity of the code-date index.
I know that but the sell-date is for this example, I use a lot of
different fields that are in the header to do this comparations.
I can't denormalize all the fields.

> Try this:
>
> SELECT p.Code,
> (SELECT sum(i.amount) FROM items i JOIN header h
> ON i.billnumber=h.billnumber
> WHERE i.Code=p.Code AND h.date BETWEEN dateA1 AND dateA2)
> AS Sales1,
> /* I believe that every item has appropriate header */
> (SELECT sum(i.amount) FROM items i JOIN header h
> ON i.billnumber=h.billnumber
> WHERE i.Code=p.Code AND h.date BETWEEN dateB1 AND dateB2)
> AS Sales2
> FROM products p
> /* I also believe that you want all products codes in result record
> set even if this product wasn's sold in both periods */

I already try this, is very slow because I have about 2000 products
so it will run 4000 subselect.

> Or you can try FULL JOIN of your SP_TEMPDATAs if you don't need
to
> see non-sold product codes.

I need to see the all the products.

Can I do something with views?

Best regards
Christian