Connecting Tech Pros Worldwide Forums | Help | Site Map

Optimize query

Newbie
 
Join Date: Jun 2009
Posts: 2
#1: Jun 29 '09
Hello people,

I have a complex question.

Imagine a supermarket where all sold products are written in a database. The sources are three cash registers. The database will continue filled with new data. I will:
- From a certain product (for example, "JAM", this product is part of a table 'products' in which these specific products are named) the data request which is received in a 30 second after the insertime product 'JAM'.
- The above action I break per cash register (cash_1, Cash_2, Cash_3).
- I want to select all products in all three subqueries occur.

As far as I have:
Expand|Select|Wrap|Line Numbers
  1. Select * From db 
  2. Where product in (
  3.         Select * from db 
  4.         where insertdate 
  5.             between (
  6.                 Select insertdate from db 
  7.                 where product = ‘JAM’ 
  8.                 and source = ‘Cash_1’
  9.             ) 
  10.             and (
  11.                 (Select insertdate from db 
  12.                  where product = ‘JAM’ 
  13.                  and source = ‘Cash_1’
  14.                 ) - (sysdate-0.000347)
  15.             )
  16.       ) 
  17. and product in (
  18.         Select * from db 
  19.         where insertdate 
  20.             between (
  21.                 Select insertdate from db 
  22.                 where product = ‘JAM’ 
  23.                 and source = ‘Cash_2’
  24.             ) 
  25.             and (
  26.                 (Select insertdate from db 
  27.                  where product = ‘JAM’ 
  28.                  and source = ‘Cash_2’
  29.                 ) - (sysdate-0.000347)
  30.             )
  31.     ) 
  32. And product in (
  33.         Select * from db 
  34.         where insertdate 
  35.             between (
  36.                 Select insertdate from db 
  37.                 where product = ‘JAM’ 
  38.                 and source = ‘Cash_3’
  39.             ) 
  40.             and (
  41.               (Select insertdate from db 
  42.                where product = ‘JAM’ 
  43.                and source = ‘Cash_3’
  44.               ) - (sysdate-0.000347)
  45.             )
  46.     )
Who can help me further? I like to hear from you.

Aldert

Member
 
Join Date: Aug 2007
Posts: 119
#2: Jul 4 '09

re: Optimize query


I think I understand what you're trying to do. You want the user to enter "JAM" and then have the query retrieve all occurences of the product "JAM" regardless of the source - Cash_1, Cash_2, or Cash_3. Is that correct?

If this is correct, you don't need such a long complicated query. First, look at line 1 of your query. You're telling the database to retrieve everything from 'db' but you said your table name was 'products.'

If the only possibilites for the source are Cash_1, Cash_2, and Cash_3, then you don't need to specify that at all.

SELECT * FROM products WHERE product = 'JAM'

If there are other sources - Cash_4 and Cash_5, but you just want the ones sold from Cash_1, Cash_2, and Cash_3, then you can use "OR" in your select statement:

SELECT * FROM products WHERE product = 'JAM' AND (source = 'Cash_1' OR source = 'Cash_2' OR source = 'Cash_3')

I hope this helps.
Reply