|
楼主的问题可改为:
以B表为查询条件,查询A表中符合B表的所有记录,且对返回的每条记录向上查询第三条记录作为“前三”字段的记录,向下查询第三条记录作为“后三”字段的记录。
- SELECT P1.代码, P1.日期, P1.数量, 前三, 后三
- FROM ((SELECT T1.代码, T1.日期, SUM(1) AS 排名, T1.数量
- FROM (SELECT A.* FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T1, (SELECT A.代码,A.日期 FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T2
- WHERE T1.代码=T2.代码 AND T1.日期>=T2.日期
- GROUP BY T1.代码, T1.日期, T1.数量) AS P1 LEFT JOIN (SELECT T1.代码, SUM(1)+2 AS 排名,T1.数量 AS 前三
- FROM (SELECT A.* FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T1, (SELECT A.代码,A.日期 FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T2
- WHERE T1.代码=T2.代码 AND T1.日期>=T2.日期
- GROUP BY T1.代码, T1.日期, T1.数量) AS P2 ON (P1.排名=P2.排名) AND (P1.代码=P2.代码)) LEFT JOIN (SELECT T1.代码,SUM(1)-2 AS 排名,T1.数量 AS 后三
- FROM (SELECT A.* FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T1, (SELECT A.代码,A.日期 FROM A,B WHERE A.代码=B.代码 AND A.日期=B.日期) AS T2
- WHERE T1.代码=T2.代码 AND T1.日期>=T2.日期
- GROUP BY T1.代码, T1.日期, T1.数量) AS P3 ON (P1.排名=P3.排名) AND (P1.代码=P3.代码);
复制代码 |
|