訂單 工序 狀態 時間
100000001 工序一 開工 2020/5/9 13:50
100000001 工序一 完工 2020/5/9 13:50
100000001 工序二 開工 2020/5/9 13:50
100000002 工序一 開工 2020/5/9 13:50
100000003 工序一 開工 2020/5/9 13:50
100000004 工序一 開工 2020/5/9 13:50
100000004 工序一 完工 2020/5/9 13:50
100000004 工序二 開工 2020/5/9 13:50
100000005 工序一 開工 2020/5/9 13:50
100000005 工序二 開工 2020/5/9 13:50
100000006 工序一 開工 2020/5/9 13:50
100000006 工序一 完工 2020/5/9 13:50
需要通過SQL查詢在工序一已開工,工序二未開工的訂單,結果樣如下:
訂單 工序一開工時間 工序一完工時間
100000002 2020/5/9 13:50 /
100000003 2020/5/9 13:50 /
100000006 2020/5/9 13:50 2020/5/9 13:50
uj5u.com熱心網友回復:
create table #tab (id nvarchar(50),name nvarchar(50),status nvarchar(50))
insert into #tab(id,name,status)
select '1','工序一','開工' union all
select '1','工序一','完工' union all
select '1','工序二','開工' union all
select '2','工序一','開工' union all
select '3','工序一','開工' union all
select '4','工序一','開工' union all
select '4','工序一','完工' union all
select '4','工序二','開工' union all
select '5','工序一','開工' union all
select '5','工序二','開工' union all
select '6','工序一','開工' union all
select '6','工序一','完工'
select * from #tab a
where a.name='工序一' and a.status='開工'
and not exists(
select id from #tab b
where b.name='工序二' and b.status='開工' and a.id=b.id
)
uj5u.com熱心網友回復:
沒看到還有個查完工時間的需求,從新補充下:
if object_id('tempdb..#tab')is not null drop table #tab
create table #tab (id nvarchar(50),name nvarchar(50),status nvarchar(50),[time] datetime)
insert into #tab(id,name,status,[time])
select '1','工序一','開工',getdate() union all
select '1','工序一','完工',getdate()+1 union all
select '1','工序二','開工',getdate()+2 union all
select '2','工序一','開工',getdate()+3 union all
select '3','工序一','開工',getdate()+4 union all
select '4','工序一','開工',getdate()+5 union all
select '4','工序一','完工',getdate()+6 union all
select '4','工序二','開工',getdate()+7 union all
select '5','工序一','開工',getdate()+8 union all
select '5','工序二','開工',getdate()+9 union all
select '6','工序一','開工',getdate()+10 union all
select '6','工序一','完工',getdate()+11
select t.*,tab.time from (
select * from #tab a
where a.name='工序一' and a.status='開工'
and not exists(
select id from #tab b
where b.name='工序二' and b.status='開工' and a.id=b.id
)
)t
left join #tab tab on t.id=tab.id and t.name=tab.name and tab.status='完工'
uj5u.com熱心網友回復:

DECLARE @t TABLE(訂單 VARCHAR(10) NOT NULL,
工序 NVARCHAR(20) NOT NULL,
狀態 NVARCHAR(10) NOT NULL,
時間 DATETIME NOT NULL);
INSERT @t(訂單, 工序, 狀態, 時間)
VALUES('100000001', '工序一', '開工', '2020/5/9 13:50'),
('100000001', '工序一', '完工', '2020/5/9 13:50'),
('100000001', '工序二', '開工', '2020/5/9 13:50'),
('100000002', '工序一', '開工', '2020/5/9 13:50'),
('100000003', '工序一', '開工', '2020/5/9 13:50'),
('100000004', '工序一', '開工', '2020/5/9 13:50'),
('100000004', '工序一', '完工', '2020/5/9 13:50'),
('100000004', '工序二', '開工', '2020/5/9 13:50'),
('100000005', '工序一', '開工', '2020/5/9 13:50'),
('100000005', '工序二', '開工', '2020/5/9 13:50'),
('100000006', '工序一', '開工', '2020/5/9 13:50'),
('100000006', '工序一', '完工', '2020/5/9 13:50');
SELECT a.訂單, a.工序, a.狀態, a.時間, f.完工時間
FROM @t a
OUTER APPLY(SELECT c.時間 完工時間
FROM @t c
WHERE a.訂單 = c.訂單 AND a.工序 = c.工序 AND c.狀態 = '完工') f
WHERE a.狀態 = '開工' AND a.工序 = '工序一'
AND NOT EXISTS (SELECT 1 FROM @t b WHERE b.訂單 = a.訂單 AND b.工序 = '工序二' AND b.狀態 = '開工');
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/34531.html
標籤:疑難問題
下一篇:matlab破解版
