嵌套选择查询Oracle SQL

时间:2016-01-04 00:49:46

标签: sql oracle oracle11g oracle-sqldeveloper

是的,我试图在Oracle SQL中构造一个嵌套的SELECT查询。目标是,SUM值为时间(存储为NUMBERS),然后乘以小时率。这发生在很多"案件中#34;和"客户"。然后,如果货币顺序中存在DESC,则提取前25%的结果。

我将提供我已经开始的两个脚本。一个比另一个更有用,但都不起作用。

脚本1:

    SELECT  client.client_id,
        client.first_name,
        client.surname,
        case.case_id,
        CONCAT(SUM(note.time_spent), CONCAT(' ', 'Hours')) AS total_time_spent
FROM    (SELECT total_time_spent,
                solicitor.solicitor_id,
                rate.solicitor_id,
                CONCAT('£', CONCAT(total_time_spent*rate.rate_per_hour)) AS total
        FROM    rate,
                solicitor
        INNER JOIN rate
        ON rate.solicitor_id = solicitor.solicitor_id
        GROUP BY solicitor_id)  
    note, 
    case
INNER JOIN client
ON case.case_id = note.case_id
INNER JOIN note
ON rate.solicitor_id = solicitor.solicitor_id
GROUP BY    note.client_id,
            note.solicitor_id
ORDER BY case.case_id,
         client.client_id;

脚本2:

SELECT  * 
FROM (
SELECT  client.client_id,
        client.first_name,
        client.surname
        CONCAT('£', SUM(note.time_spent*rate.rate_per_hour)) AS total
FROM    rate, note, client, case
WHERE note.solicitor_id = solicitor.solicitor_id
    AND solicitor.solicitor_id = rate.solicitor_id
    AND note.case_id = case.case_id
    AND case.client_id = client.client_id
ORDER BY total DESC
) WHERE ROWNUM <= (SELECT COUNT(*) FROM CLIENT)/4;

以下是TABLE CREATE脚本:

创建案例

CREATE TABLE case
(
case_id         NUMBER(4),
client_id       NUMBER(4),
description     VARCHAR(100) CONSTRAINT case_description_nn NOT NULL,
date_filed      DATE CONSTRAINT case_date_filled_nn NOT NULL,
date_closed     DATE,

CONSTRAINT case_client_id_fk FOREIGN KEY (client_id) REFERENCES client (client_id),
CONSTRAINT case_pk PRIMARY KEY (case_id)
);

创建注释

CREATE TABLE note
(
note_id         NUMBER(4),
case_id         NUMBER(4),
solicitor_id    NUMBER(2),
content         VARCHAR(500) CONSTRAINT note_content_nn NOT NULL,
time_spent      NUMBER(3) CONSTRAINT note_time_spent_nn  NOT NULL, -- I have made the assumption that time taken is recorded as an integer value, e.g. 6 [Hours] --
date_added      DATE DEFAULT (sysdate) CONSTRAINT note_date_addedd_nn NOT NULL,

CONSTRAINT note_solicitor_id_fk FOREIGN KEY (solicitor_id) REFERENCES solicitor (solicitor_id),
CONSTRAINT note_case_id_fk FOREIGN KEY (case_id) REFERENCES case (case_id),
CONSTRAINT note_pk PRIMARY KEY (note_id)
);

CREATE SOLICITOR

CREATE TABLE solicitor
(
solicitor_id    NUMBER(2),
first_name      CHAR(15) CONSTRAINT solicitor_first_name_nn NOT NULL,
surname         CHAR(20) CONSTRAINT solicitor_surname_nn NOT NULL,
telephone       VARCHAR(11) CONSTRAINT solicitor_telephone_nn NOT NULL,
email_address   VARCHAR(50) CONSTRAINT solicitor_email_address_nn NOT NULL,

CONSTRAINT solicitor_pk PRIMARY KEY (solicitor_id)
);

创建汇率

CREATE TABLE rate
(
rate_id         NUMBER(2),
solicitor_id    NUMBER(2),
rate_per_hour   DECIMAL(19,4) CONSTRAINT rate_rate_per_hour_nn NOT NULL,
start_date      DATE CONSTRAINT rate_start_date_nn NOT NULL,
end_date        DATE,

CONSTRAINT rate_solicitor_id_fk FOREIGN KEY (solicitor_id) REFERENCES solicitor (solicitor_id),
CONSTRAINT rate_pk PRIMARY KEY (rate_id)
);

创建客户

CREATE TABLE client
(
client_id       NUMBER(4),
first_name      CHAR(15) CONSTRAINT client_first_name_nn NOT NULL,
surname         CHAR(20) CONSTRAINT client_surname_nn NOT NULL,
address         VARCHAR(35) CONSTRAINT client_address_nn NOT NULL,
postcode        VARCHAR(8)  CONSTRAINT client_postcode_nn NOT NUll,
telephone       VARCHAR(11) CONSTRAINT client_telephone_nn NOT NULL,
email_address   VARCHAR(50) CONSTRAINT client_email_address_nn NOT NUll,

CONSTRAINT client_pk PRIMARY KEY (client_id)
);

1 个答案:

答案 0 :(得分:0)

    SELECT * FROM (
        SELECT  client.client_id,
                client.first_name,
                client.surname,
                CONCAT('£', SUM(note.time_spent*rate.rate_per_hour)) AS total
        FROM rate, note, client, case, solicitor
        WHERE  note.solicitor_id = solicitor.solicitor_id
        AND solicitor.solicitor_id = rate.solicitor_id
        AND note.case_id = case.case_id 
        AND case.client_id = client.client_id
    GROUP BY    client.client_id,
                client.first_name,
                client.surname
  ORDER BY total DESC
) WHERE ROWNUM <= (SELECT COUNT(*) FROM client)/4
ORDER BY total ASC;