如何从子请求中提出请求?

时间:2014-01-24 14:43:49

标签: sql oracle

例如:

SELECT DISTINCT goods, price, category
FROM factory1
JOIN factory2
WHERE price IN ( SELECT price FROM factory2 WHERE date* ...)

如何通过子请求“factory2”中的“date”在主要请求中执行此操作?

AND date* BETWEEN TO_DATE ( '2013/07/01','YYYY/MM/DD' ) AND TO_DATE ( '2013/07/07', 'YYYY/MM/DD')


谢谢你的帮助。

更新#1

对不起,也许我的例子不太准确。 我需要在子查询中输入的信息必须要引入主查询 ,我不知道如何实现它。
我会尝试展示我想要的东西:

SELECT *
    FROM Orders
    WHERE snum IN
       ( SELECT snum 
         FROM Salespeople 
         WHERE odate = 10/03/1990 );

我需要做这样的事情:

SELECT *
    FROM Orders
    WHERE snum IN
       ( SELECT snum 
         FROM Salespeople 
         WHERE odate = (X) )
    AND (X) = 10/03/1990;

也许它需要functioncontext,但我以前从未使用过它。
谢谢。

更新#2
这是我真正任务的一部分:

CREATE VIEW cash_deposit
(
    type,
    city_code,
    city_name,
    member_code,
    member_name,
    operation,
    amount,
    sum_operation,
    sum_amount,
    date,
    ...
)
AS
SELECT DISTINCT type, city_code, city_name, member_code, member_name, operation, amount, sum_operation, sum_amount, date, ...
FROM (SELECT type, city_code, city_name, member_code, member_name, operation, amount, sum_operation, sum_amount, date
      FROM (SELECT DISTINCT *mainPartOfAmount*, member_code, member_name, operation, date
            FROM database1.action1 T
            JOIN database1.action2 S ON S.key=T.key AND S.act=1
            JOIN database1.action3 E ON E.key=S.key AND E.act=1 AND *mainPartOfAmount* IN
                (SELECT DISTINCT *mainPartOfAmount*
                 FROM (SELECT *mainPartOfAmount*, *summ_amount*, date
                       FROM database1.action1 T
                       JOIN database1.action2 S ON S.key=T.key AND S.act=1
                       JOIN database1.action3 E ON E.key=S.key AND E.act=1
                       JOIN database1.action4 Q ON Q.key=T.key
                       JOIN database1.action5 Z ON Z.key=Q.key
                       WHERE T.*summ_amount* IN (SELECT *summ_amount* 
                                                 FROM (SELECT R.*summ_amount*, R.date 
                                                       FROM database1.action1 R)
                                                )
                       AND --condition #1
                       GROUP BY *mainPartOfAmount*, date)
                )
          JOIN database1.action4 Q ON Q.key=T.key
          JOIN database1.action5 Z ON Z.key=Q.key
          WHERE T.*summ_amount* IN (SELECT *summ_amount* 
                                    FROM (SELECT R.*summ_amount*, R.date 
                                          FROM database1.action1 R)
                                   )
          AND --condition #2
          GROUP BY *mainPartOfAmount*, date
         )
     )Block1
JOIN
SELECT ( .... )Block2
ON Block1.member_code = Block2.member_code
JOIN
SELECT ( .... )Block3
ON Block1.member_code = Block3.member_code
--here must be the information is entered for the subquery of R.date in Block1:
--WHERE R.date BETWEEN TO_DATE ( '2013/07/01','YYYY/MM/DD' ) AND TO_DATE ( '2013/07/07', 'YYYY/MM/DD') (for example)
ORDER BY type, member_code, sum_operation, date

这就是为什么我试图尽可能简短地问:)

1 个答案:

答案 0 :(得分:1)

不完全确定这是不是您的意思,但我认为您正在混合joinwhere

你的意思是这样吗?如果没有,请明确你的意思。

SELECT DISTINCT goods, price, category
FROM factory1 f1
JOIN factory2 f2
ON f1.price = f2.price
WHERE f2.date BETWEEN TO_DATE ( '2013/07/01', 'YYYY/MM/DD' ) AND TO_DATE ( '2013/07/07', 'YYYY/MM/DD')