无论我做什么,我的 Oracle9i 查询都会生成重复的行。它还错误地将值和样本编号分配给日期。我尝试使用连接失败,并且使用 distinct 也没有运气。我还尝试在 where 语句中输入 sd.SAMPLE_NUM=shv.SAMPLE_NUM 。不过,这对我没有帮助。当我这样做时,我得到零行返回。预先感谢您的任何帮助。
SELECT
DISTINCT
DATE_SAMPLE,
SW_NAME,
SAMPLE_NUM,
BU_ID,
sum(case when ANALYSIS_NAME = 'METHANE-MOL%' then ANALYSIS_RESULT else null end) C1,
sum(case when ANALYSIS_NAME = 'ETHANE-MOL%' then ANALYSIS_RESULT else null end) C2,
sum(case when ANALYSIS_NAME = 'PROPANE-MOL%' then ANALYSIS_RESULT else null end) C3
FROM
(
SELECT
DISTINCT
sd.SW_NAME,
sd.SAMPLE_NUM,
sd.ANALYSIS_RESULT,
sd.ANALYSIS_NAME,
sd.BU_ID,
shv.DATE_SAMPLE
FROM
public_project.Sample_Data_Pbu_Lab_Vew sd,
public_project.Sample_Header_Vew shv
WHERE
(sd.SW_NAME LIKE '02-40%') AND
sd.BU_ID='PBU' AND
shv.DATE_SAMPLE > TO_DATE ('2013-01-01', 'YYYY-MM-DD')
GROUP BY
sd.SW_NAME,
sd.SAMPLE_NUM,
sd.ANALYSIS_RESULT,
sd.ANALYSIS_NAME,
sd.BU_ID,
shv.DATE_SAMPLE
)
GROUP BY
DATE_SAMPLE,
SW_NAME,
SAMPLE_NUM,
BU_ID;
查询输出示例(有几千行与此类似):
DATE_SAMPL SW_NAME SAMPLE_NUM BU_ID C1 C2 C3
---------- ------------ ------------- ---------- ---------- ---------- ----------
2013-01-01 02-40 PC66207 PBU 79.981 5.192 1.681
2013-01-01 02-40 PC43602 PBU 79.062 5.333 1.862
2013-01-01 02-40 PC44278 PBU
2013-01-01 02-40 PC61178 PBU
2013-01-01 02-40 PC66207 PBU 79.981 5.192 1.681
2013-01-01 02-40 PC43602 PBU 79.062 5.333 1.862
2013-01-01 02-40 PC44278 PBU
2013-01-01 02-40 PC61178 PBU
2013-01-01 02-40 PC66207 PBU 79.981 5.192 1.681
2013-01-01 02-40 PC43602 PBU 79.062 5.333 1.862
对表格的描述产生:
$SQL> DESCRIBE public_project.Sample_Data_Pbu_Lab_Vew
Name Null? Type
----------------------------------------- -------- -----------------
API VARCHAR2(16)
SW_NAME VARCHAR2(20)
BU_ID VARCHAR2(30)
PROJECT VARCHAR2(30)
SAMPLE_NUM NOT NULL VARCHAR2(15)
ANALYSIS_NAME NOT NULL VARCHAR2(80)
ANALYSIS_METHOD_NUM NOT NULL VARCHAR2(15)
ANALYSIS_RESULT NUMBER
ANALYSIS_UNITS VARCHAR2(20)
ANALYSIS_RESULT_TEXT VARCHAR2(80)
DATE_ANALYZED DATE
LAST_UPDATE_ID VARCHAR2(30)
LAST_UPDATE_DATE DATE
SQL> DESCRIBE public_project.Sample_Header_Vew
Name Null? Type
----------------------------------------- -------- -----------------
API VARCHAR2(16)
SW_NAME VARCHAR2(20)
BU_ID VARCHAR2(30)
PROJECT VARCHAR2(30)
SAMPLE_NUM NOT NULL VARCHAR2(15)
COMMENTS VARCHAR2(500)
DATE_SAMPLE DATE
DATE_RECEIVED DATE
METER_NUM VARCHAR2(4)
PWSID NUMBER
SAMPLE_CATEGORY VARCHAR2(20)
SAMPLE_DESC VARCHAR2(80)
SAMPLE_FACILITY VARCHAR2(18)
SAMPLE_LAB VARCHAR2(15)
SAMPLE_POINT VARCHAR2(80)
SAMPLE_TYPE VARCHAR2(20)
LAST_UPDATE_ID VARCHAR2(30)
LAST_UPDATE_DATE DATE
SAMPLE_DEPTH NUMBER
SAMPLE_DEPTH_REF VARCHAR2(5)