これが私のデータのサンプルであり、私が見たいものです:
JOB OPSEQ OPCOMPLETE OPCODE
100 1 yes pull
100 2 yes weld
100 3 no grind
100 4 no machine
100 5 no asmbl
したがって、opcomplete=no の場合の min(opseq) と opcomplete=yes の場合の max(opseq)、および min と max opseq のオペコードを選択したいと考えています。この例では、次のようになります:
min(opseq):3最小操作
のオペコード: 研磨
max(opseq): 2 最大操作のオペコード: 溶接
私がこれを探している理由は、完全ではない最小の opseq のオペコードを取得するためです。
最小および最大 opseqs がうまく機能するようになりました。これは私が持っていたものです:
(SELECT company, jobnum, MAX(oprseq) AS maxclosed, opcomplete
FROM joboper AS joboper_2
WHERE (company = 'lot') AND (opcomplete = '1')
GROUP BY company, jobnum, opcomplete) AS t_joboper1 ON joboper.company = t_joboper1.company AND joboper.jobnum = t_joboper1.jobnum INNER JOIN
(SELECT company, jobnum, MIN(oprseq) AS minopen, opcomplete
FROM joboper AS joboper_1
WHERE (company = 'lot') AND (opcomplete = '0')
GROUP BY company, jobnum, opcomplete) AS t_joboper2 ON joboper.company = t_joboper2.company AND joboper.jobnum = t_joboper2.jobnum
そのため、オペコードを追加しようとしても機能せず、あらゆる種類の重複値を取得し始めました。これは私が書いたものです:
(SELECT company, jobnum, MAX(oprseq) AS maxclosed, opcomplete, opcode as maxopcode
FROM joboper AS joboper_2
WHERE (company = 'lot') AND (opcomplete = '1')
GROUP BY company, jobnum, opcomplete, opcode) AS t_joboper1 ON joboper.company = t_joboper1.company AND joboper.jobnum = t_joboper1.jobnum INNER JOIN
(SELECT company, jobnum, MIN(oprseq) AS minopen, opcomplete, opcode as minopcode
FROM joboper AS joboper_1
WHERE (company = 'lot') AND (opcomplete = '0')
GROUP BY company, jobnum, opcomplete, opcode) AS t_joboper2 ON joboper.company = t_joboper2.company AND joboper.jobnum = t_joboper2.jobnum
現時点でのコード全体を次に示します (オペコードはすべての重複値を取り込みます:
SELECT jobhead.company, jobhead.jobnum, jobhead.partnum,
jobhead.partdescription, jobhead.startdate,
jobhead.reqduedate, jobhead.prodqty, jobhead.qtycompleted,
joboper.oprseq, joboper.opcode, joboper.opcomplete,
joboper.qtycompleted AS joboperqtycomplete,
resourcegroup.description AS rgroupdescription,
dmrhead.dmrnum, poheader.ponum, vendor.name AS vendorname,
t_joboper2.minopen AS minopen, t_joboper2.minopcode AS minopcode,
t_joboper1.maxclosed AS maxclosed, t_joboper1.maxopcode AS maxopcode
FROM jobhead LEFT OUTER JOIN
joboper INNER JOIN
(SELECT company, jobnum, MAX(oprseq) AS maxclosed,
opcomplete, opcode as maxopcode
FROM joboper AS joboper_2
WHERE (company = 'lot') AND (opcomplete = '1')
GROUP BY company, jobnum, opcomplete, opcode) AS t_joboper1
ON joboper.company = t_joboper1.company
AND joboper.jobnum = t_joboper1.jobnum INNER JOIN
(SELECT company, jobnum, MIN(oprseq) AS minopen, opcomplete,
opcode as minopcode
FROM joboper AS joboper_1
WHERE (company = 'lot') AND (opcomplete = '0')
GROUP BY company, jobnum, opcomplete, opcode) AS t_joboper2
ON joboper.company = t_joboper2.company
AND joboper.jobnum = t_joboper2.jobnum
ON jobhead.company = joboper.company
AND jobhead.jobnum = joboper.jobnum LEFT OUTER JOIN
resourcegroup ON joboper.company = resourcegroup.company
AND joboper.opcode = resourcegroup.opcode LEFT OUTER JOIN
dmrhead ON joboper.company = dmrhead.company
AND joboper.jobnum = dmrhead.jobnum
AND joboper.assemblyseq = dmrhead.assemblyseq
AND joboper.oprseq = dmrhead.oprseq LEFT OUTER JOIN
porel ON joboper.company = porel.company
AND joboper.jobnum = porel.jobnum
AND joboper.assemblyseq = porel.assemblyseq
AND joboper.oprseq = porel.jobseq LEFT OUTER JOIN
podetail ON porel.company = podetail.company
AND porel.ponum = podetail.ponum
AND porel.poline = podetail.poline LEFT OUTER JOIN
poheader ON podetail.company = poheader.company
AND podetail.ponum = poheader.ponum LEFT OUTER JOIN
vendor ON poheader.company = vendor.company
AND poheader.vendornum = vendor.vendornum
WHERE (jobhead.jobreleased = 1)
AND (jobhead.jobcomplete = 0)
AND (jobhead.company = 'lot')
AND (jobhead.plant = '001')
私がここでやろうとしていることは、これがすべて意味をなすことを願っています。完全に明らかでない場合は、ここで質問するのはこれが初めてです。事前に、すべての助けに感謝します!!!
新規 - 12-21-12
助けてくれてありがとう!両方の提案を試しましたが、どちらも希望どおりの結果を得ることができませんでした。しかし、それぞれの答えは、最終的に必要なものを手に入れるのに必要なものにたどり着くのに役立ちました。これは私の最初の質問なので、どの回答を解決策としてマークするまで何をすべきかわかりませんか? 私が言ったように、両方の答えが私を大いに助けてくれました. これにさらに取り組んだ後、私は自分の質問を明確にするために一生懸命努力しましたが、戻ってみると、どうすれば物事をもっとうまく表現できたのかがわかります. 繰り返しますが、私はすべての助けに非常に感謝しており、将来より良い質問をすることを楽しみにしています.
ちなみに、これが最終的に動作したコードです。
SELECT jobhead.company, jobhead.jobnum, jobhead.partnum, jobhead.partdescription, jobhead.startdate, jobhead.reqduedate, jobhead.prodqty, jobhead.qtycompleted,
joboper.oprseq, joboper.opcode, joboper.opcomplete, joboper.qtycompleted AS joboperqtycomplete, resourcegroup.description AS rgroupdescription,
dmrhead.dmrnum, poheader.ponum, vendor.name AS vendorname, t_joboper2.minopen, t_joboper1.maxclosed, t_joboper3.opcode AS minopcode
FROM jobhead LEFT OUTER JOIN
joboper INNER JOIN
(SELECT company, jobnum, MAX(oprseq) AS maxclosed, opcomplete
FROM joboper AS joboper_1
WHERE (company = 'lot') AND (opcomplete = '1')
GROUP BY company, jobnum, opcomplete) AS t_joboper1 ON joboper.company = t_joboper1.company AND joboper.jobnum = t_joboper1.jobnum INNER JOIN
(SELECT company, jobnum, MIN(oprseq) AS minopen, opcomplete
FROM joboper AS joboper_2
WHERE (company = 'lot') AND (opcomplete = '0')
GROUP BY company, jobnum, opcomplete) AS t_joboper2 ON joboper.company = t_joboper2.company AND
joboper.jobnum = t_joboper2.jobnum INNER JOIN
(SELECT company, jobnum, oprseq, opcomplete, opcode
FROM joboper AS joboper_3
WHERE (company = 'lot') AND (opcomplete = '0'))
AS t_joboper3 ON t_joboper2.company = t_joboper3.company AND t_joboper2.jobnum = t_joboper3.jobnum AND
t_joboper2.minopen = t_joboper3.oprseq ON jobhead.company = joboper.company AND jobhead.jobnum = joboper.jobnum LEFT OUTER JOIN
resourcegroup ON joboper.company = resourcegroup.company AND joboper.opcode = resourcegroup.opcode LEFT OUTER JOIN
dmrhead ON joboper.company = dmrhead.company AND joboper.jobnum = dmrhead.jobnum AND joboper.assemblyseq = dmrhead.assemblyseq AND
joboper.oprseq = dmrhead.oprseq LEFT OUTER JOIN
porel ON joboper.company = porel.company AND joboper.jobnum = porel.jobnum AND joboper.assemblyseq = porel.assemblyseq AND
joboper.oprseq = porel.jobseq LEFT OUTER JOIN
podetail ON porel.company = podetail.company AND porel.ponum = podetail.ponum AND porel.poline = podetail.poline LEFT OUTER JOIN
poheader ON podetail.company = poheader.company AND podetail.ponum = poheader.ponum LEFT OUTER JOIN
vendor ON poheader.company = vendor.company AND poheader.vendornum = vendor.vendornum
WHERE (jobhead.jobreleased = 1) AND (jobhead.jobcomplete = 0) AND (jobhead.company = 'lot') AND (jobhead.plant = '001')