SQL子查询,其中列=列-10

时间:2017-08-11 10:52:32

标签: sql sql-server tsql

SELECT

worksorders.id AS 'Works Order No',
worksorders.partid AS 'Part No',
allpartmaster.partdesc AS 'Description',
worksorders.quantity AS 'Qty',
wip.wqleft AS 'Qty Rem',
worksorders.duedate AS 'Due Date',
worksorders.lateststartdate AS 'Latest Start Date',
worksorders.kitstatus AS 'Kit Status',
wip.wopnumber AS 'Op No',
wip.wmachine AS 'Work Centre',
wip.wdesc AS 'WC Description',
wip.wstate as 'Op State',
(SELECT wstate FROM wip WHERE wip.wona = worksorders.id and wip.wopnumber = (wip.wopnumber - 10)) as 'Prev Op State'

FROM efacdb.dbo.allpartmaster allpartmaster, efacdb.dbo.wip wip, efacdb.dbo.worksorders worksorders

WHERE worksorders.id = wip.wona AND allpartmaster.partnum = worksorders.partid AND ((worksorders.status = 'ACTIVE') AND (wip.wstate = 'NS'))

AND wip.wmachine = 'POLISH'

ORDER BY worksorders.lateststartdate ASC

我在查询时遇到问题,我不确定它是否可行。我返回的操作按步骤10编号,我需要返回上一操作的状态。这是关键路线:

(SELECT wstate FROM wip WHERE wip.wona = worksorders.id and wip.wopnumber = (wip.wopnumber - 10)) as 'Prev Op State'

我想从父SELECT查询中获取wopnumber,取掉它的10并返回该行的wstate列。

这可能吗?

2 个答案:

答案 0 :(得分:0)

也许这就是你想要的?

SELECT

        worksorders.id AS 'Works Order No',
        worksorders.partid AS 'Part No',
        allpartmaster.partdesc AS 'Description',
        worksorders.quantity AS 'Qty',
        wip.wqleft AS 'Qty Rem',
        worksorders.duedate AS 'Due Date',
        worksorders.lateststartdate AS 'Latest Start Date',
        worksorders.kitstatus AS 'Kit Status',
        wip.wopnumber AS 'Op No',
        wip.wmachine AS 'Work Centre',
        wip.wdesc AS 'WC Description',
        wip.wstate as 'Op State',
        app.wstate as 'Prev Op State'

FROM efacdb.dbo.allpartmaster allpartmaster, efacdb.dbo.wip wip, efacdb.dbo.worksorders worksorders
     cross apply (SELECT wstate 
                  FROM wip wip1 
                  WHERE wip1.wona = worksorders.id and wip1.wopnumber = wip.wopnumber - 10) app

WHERE worksorders.id = wip.wona AND allpartmaster.partnum = worksorders.partid AND ((worksorders.status = 'ACTIVE') AND (wip.wstate = 'NS'))

AND wip.wmachine = 'POLISH'

ORDER BY worksorders.lateststartdate ASC

答案 1 :(得分:0)

也许是这样的?

.selectable {
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}

免责声明:仅在记事本中进行测试