Oracle是我们正在使用的数据库。我的要求是从myResults表获取最新记录,可以通过查看createdtimestamp列中的最新时间来获取,现在在 myResults.resID时显示来自myTransactions表的所有匹配记录= myTransactions.resID。 因为我必须显示来自4个不同表格的记录,我才加入它们。 myTransaction.transID和myFields.transID包含相同的值,myFields.formatNumber和myTransactions.formatID相同,如图所示数据库表。目前我的查询返回所有记录,但要求首先从myResults表中获取最新记录,并显示其他3个表中具有上述条件的所有相关记录。
请在sqlfiddle http://sqlfiddle.com/#!4/463aa/11/13中找到示例查询 我尽力使用它的水平,但没有运气,核心查询无效。但是你可以在sqlfiddle中看到其他表的输出。
请建议对我的核心查询进行哪些修改,以便显示最近的记录相关数据行。
PS:输出应该显示3行,因为myResults表返回最近时间的一条记录,通过比较myResults的resID和myTransactions的resID,我们有3行,myTransactions.transID = myFields.transID和myFormat.formatNumber等于myTransactions.formatId。
它非常令人困惑和复杂......只有当我花了很多时间而且我不是数据库人员时,请提出严肃的建议。
请找到应在http://sqlfiddle.com/#!4/463aa/15/17
中修改的核心查询以下是数据库表:
create table myResults(resID integer,productNumber varchar2(20),resultNumber varchar2(10),midnum varchar2(10),sosnum varchar2(10),status varchar2(10),description varchar2(10),createdtimestamp timestamp);
create table myTransactions(transID integer,formatId varchar2(15),total integer,spreadStatus varchar2(10),status varchar2(20),description varchar2(10),ctimestamp timestamp,resID integer);
create table myFields(transID integer,fieldname varchar2(10),fieldvalue varchar2(10));
create table myFormat(formatId integer,formatDesc varchar2(20),formatNumber varchar2(15));
中找到包含数据的所有表格
答案 0 :(得分:2)
您的SQL存在多个问题。
让我列举一些:
NVL(formatId, 'N/A')
formatid是 NUMBER ,因此您无法在NVL函数中将其替换为字符串。
myfields.field_name
列名称有语法错误,应该是fildname
没有下划线。
我更喜欢在 SQL * Plus 中执行SQL,以查看确切的错误和行号,这样可以很容易地修复它。
这就是你需要执行的方法,我已经解决了上面提到的问题:
声明变量:
SQL> var orderDirection varchar2(20);
SQL> var orderby varchar2(20);
SQL> var productnumber varchar2(20);
SQL> var resultNumber varchar2(20);
SQL> var sosnum varchar2(20);
SQL> var formatid varchar2(20);
分配值:
SQL> exec : orderdirection := 'asc';
PL/SQL procedure successfully completed.
SQL> exec : orderby := 'total';
PL/SQL procedure successfully completed.
SQL> exec : productnumber := 'B2B100';
PL/SQL procedure successfully completed.
SQL> exec : resultnumber := '100';
PL/SQL procedure successfully completed.
SQL> exec : sosnum := 'sos10';
PL/SQL procedure successfully completed.
SQL> exec : formatid := '203435';
PL/SQL procedure successfully completed.
执行SQL:
SQL> select transID, productNumber, resultNumber, sosnum, total,
2 NVL(formatId, 0) formatId, NVL(formatDesc, 'N/A') formatDesc,
3 NVL(formatNumber, 'N/A') formatNumber, status, description,
4 TO_CHAR(timestamp, 'yyyy-mm-dd hh24:mi:ss') timestamp, cnt
5 FROM (
6 SELECT prodHistory.*,
7 row_number() OVER ( ORDER BY
8 CASE
9 WHEN :orderDirection like '%asc%' THEN
10 CASE
11 WHEN :orderBy='productNumber' THEN productNumber
12 WHEN :orderBy='resultNumber' THEN resultNumber
13 WHEN :orderBy='sosnum' THEN sosnum
14 WHEN :orderBy='total' THEN total
15 WHEN :orderBy='formatId' THEN formatId
16 WHEN :orderBy='formatDesc' THEN formatDesc
17 WHEN :orderBy='formatNumber' THEN formatNumber
18 WHEN :orderBy='status' THEN status
19 WHEN :orderBy='description' THEN description
20 WHEN :orderBy='timestamp' THEN to_char(timestamp, 'yyyy-mm-dd hh24:mi:ss')
21 ELSE to_char(timestamp, 'yyyy-mm-dd hh24:mi:ss')
22 END
23 END ASC,
24 transID ASC
25 ) lineNumber
26 FROM (
27 select prodHistory_inner.*, COUNT(1) OVER() cnt
28 from (
29 select mytransactions.transID,
30 myresults.productNumber productNumber,
31 myresults.resultNumber resultNumber,
32 myresults.sosnum sosnum,
33 to_char(mytransactions.total, 999) total,
34 myformat.formatId formatId,
35 myformat.formatDesc formatDesc,
36 myformat.formatNumber formatNumber,
37 myresults.status status,
38 myfields.fieldvalue fieldvalue,
39 myresults.description description,
40 myresults.createdtimestamp timestamp
41 from MYRESULTS myresults
42 left outer join MYTRANSACTIONS mytransactions
43 on mytransactions.resID = myresults.resID
44 left outer join MYFIELDS myfields
45 on mytransactions.transID = myfields.transID
46 and (myfields.fieldname ='AXPARR' or myfields.fieldname = 'AXPARR_P')
47 left outer join MYFORMAT myformat
48 on myformat.formatNumber = mytransactions.formatId
49 where ( :productNumber is null
50 or (myresults.productNumber like :productNumber)
51 or myresults.productNumber = :productNumber)
52 and ( :resultNumber is null
53 or (myresults.resultNumber like :resultNumber)
54 or myresults.resultNumber = :resultNumber)
55 and ( :sosnum is null
56 or myresults.sosnum = :sosnum)
57 and (:formatId is null
58 or (mytransactions.formatId like :formatId)
59 or mytransactions.formatId = :formatId)
60 ) prodHistory_inner
61 ) prodHistory
62 );
TRANSID PRODUCTNUMBER RESULTNUMB SOSNUM TOTAL FORMATID FORMATDESC FORMATNUMBER STATUS DESCRIPTIO TIMESTAMP CNT
------- ------------- ---------- ------ ----- ---------- ----------- --------------- ---------- ---------- ------------------- ---
90 B2B100 100 sos10 10 1001 dispenser 203435 pass B2B passed 2015-08-22 11:30:00 1
SQL>
更新由于formatnumber
和formatid
上的过滤条件,上述查询只有一行。要根据order by timestamp
获得三行:
SQL> var orderDirection varchar2(20);
SQL> var orderby varchar2(20);
SQL> var productnumber varchar2(20);
SQL> var resultNumber varchar2(20);
SQL> var sosnum varchar2(20);
SQL> var formatid varchar2(20);
SQL> exec : orderdirection := 'asc';
PL/SQL procedure successfully completed.
SQL> exec : orderby := 'timestamp';
PL/SQL procedure successfully completed.
SQL> exec : productnumber := 'B2B100';
PL/SQL procedure successfully completed.
SQL> exec : resultnumber := '100';
PL/SQL procedure successfully completed.
SQL> exec : sosnum := 'sos10';
PL/SQL procedure successfully completed.
SQL> exec : formatid := '203435';
PL/SQL procedure successfully completed.
SQL>
SQL> select transID, productNumber, resultNumber, sosnum, total,
2 NVL(formatId, 0) formatId, NVL(formatDesc, 'N/A') formatDesc,
3 NVL(formatNumber, 'N/A') formatNumber, status, description,
4 TO_CHAR(timestamp, 'yyyy-mm-dd hh24:mi:ss') timestamp, cnt
5 FROM (
6 SELECT prodHistory.*,
7 row_number() OVER ( ORDER BY
8 CASE
9 WHEN :orderDirection like '%asc%' THEN
10 CASE
11 WHEN :orderBy='productNumber' THEN productNumber
12 WHEN :orderBy='resultNumber' THEN resultNumber
13 WHEN :orderBy='sosnum' THEN sosnum
14 WHEN :orderBy='total' THEN total
15 WHEN :orderBy='formatId' THEN formatId
16 WHEN :orderBy='formatDesc' THEN formatDesc
17 WHEN :orderBy='formatNumber' THEN formatNumber
18 WHEN :orderBy='status' THEN status
19 WHEN :orderBy='description' THEN description
20 WHEN :orderBy='timestamp' THEN to_char(timestamp, 'yyyy-mm-dd hh24:mi:ss')
21 ELSE to_char(timestamp, 'yyyy-mm-dd hh24:mi:ss')
22 END
23 END ASC,
24 transID ASC
25 ) lineNumber
26 FROM (
27 select prodHistory_inner.*, COUNT(1) OVER() cnt
28 from (
29 select mytransactions.transID,
30 myresults.productNumber productNumber,
31 myresults.resultNumber resultNumber,
32 myresults.sosnum sosnum,
33 to_char(mytransactions.total, 999) total,
34 myformat.formatId formatId,
35 myformat.formatDesc formatDesc,
36 myformat.formatNumber formatNumber,
37 myresults.status status,
38 myfields.fieldvalue fieldvalue,
39 myresults.description description,
40 myresults.createdtimestamp timestamp
41 from MYRESULTS myresults
42 left outer join MYTRANSACTIONS mytransactions
43 on mytransactions.resID = myresults.resID
44 left outer join MYFIELDS myfields
45 ON mytransactions.transID = myfields.transID
46 --and (myfields.fieldname ='AXPARR' or myfields.fieldname = 'AXPARR_P')
47 left outer join MYFORMAT myformat
48 ON myformat.formatNumber = mytransactions.formatId
49 where ( :productNumber is null
50 or (myresults.productNumber like :productNumber)
51 OR myresults.productNumber = :productNumber)
52 and ( :resultNumber is null
53 or (myresults.resultNumber like :resultNumber)
54 OR myresults.resultNumber = :resultNumber)
55 and ( :sosnum is null
56 OR myresults.sosnum = :sosnum)
57 /*AND (:formatId IS NULL
58 or (mytransactions.formatId like :formatId)
59 or mytransactions.formatId = :formatId)*/
60 and mytransactions.transid is not null
61 ) prodHistory_inner
62 ) prodHistory
63 );
TRANSID PRODUCTNUMBER RESULTNUMB SOSNUM TOTA FORMATID FORMATDESC FORMATNUMBER STATUS DESCRIPTIO TIMESTAMP CNT
---------- -------------------- ---------- ---------- ---- ---------- -------------------- --------------- ---------- ---------- ------------------- ----------
92 B2B100 100 sos10 10 1003 Sigular 302123 pass B2B passed 2015-08-22 11:30:00 3
91 B2B100 100 sos10 10 1002 exedemption 280908 pass B2B passed 2015-08-22 11:30:00 3
90 B2B100 100 sos10 10 1001 dispenser 203435 pass B2B passed 2015-08-22 11:30:00 3
SQL>