不同行的SQL查询

时间:2017-04-13 12:10:39

标签: sql sql-server

尝试从多个表中获取不同的行,具体取决于Job_Status。 我想只有job_status = 60而不是在job_status> = 90。 样品

STORENAME   JOBNO   SEQ   JOB_STATUS
ABC         743     1     20
ABC         743     2     30
ABC         743     3     60
ABC         743     4     60
ABC         743     5     90
ABC         771     1     20
ABC         771     2     20
ABC         771     3     60
ABC         771     4     60
ABC         895     1     10
ABC         895     2     20
ABC         895     3     30
ABC         895     4     30
ABC         895     5     30
ABC         895     6     30
ABC         895     7     30
ABC         895     8     20
ABC         895     9     30
ABC         895     10    30
ABC         895     11    30
ABC         895     12    30
ABC         895     13    60
ABC         895     14    90
ABC         895     15    90

返回预期

STORENAME   JOBNO   SEQ   JOB_STATUS
ABC         771     4     60
我试过的是

;WITH T AS
(
    SELECT
        *,
        CASE WHEN Job_Status>=90 THEN 0 ELSE 1 END AS IsConsider         
    FROM SERVICEJOB GD Inner Join
dbo.JOBTRACKING  AS JT ON JT.JOB_NO= GD.JOB_NO INNER JOIN
 dbo.DataDetails AS UL ON GD.StoreID = UL.StoreID       
)
SELECT
    StoreName,
    JOB_NO,
	 SEQ,
    Job_Status
FROM
(
    SELECT 
        StoreName,
        JOB_NO,
		TRACKING_SEQ
        Job_Status,
        ROW_NUMBER() OVER (PARTITION BY JOB_NO ORDER BY  SEQ DESC) JobPartNo
    FROM T 
    WHERE Job_Status=60
    AND JOB_NO NOT IN (SELECT JOB_NO FROM T WHERE IsConsider=0)
) AS X
WHERE JobPartNo=1

对于我来说,返回所有意味着状态90以及更多。

A1:AY55

3 个答案:

答案 0 :(得分:1)

create table jobs (STORENAME varchar(10), JOBNO int, SEQ int, JOB_STATUS int);
insert into jobs values
('ABC',   743,    1,  20),
('ABC',   743,    2,  30),
('ABC',   743,    3,  60),
('ABC',   743,    4,  60),
('ABC',   743,    5,  90),
('ABC',   771,    1,  20),
('ABC',   771,    2,  20),
('ABC',   771,    3,  60),
('ABC',   771,    4,  60),
('ABC',   895,    1,  10),
('ABC',   895,    2,  20),
('ABC',   895,    3,  30),
('ABC',   895,    4,  30),
('ABC',   895,    5,  30),
('ABC',   895,    6,  30),
('ABC',   895,    7,  30),
('ABC',   895,    8,  20),
('ABC',   895,    9,  30),
('ABC',   895,    10, 30),
('ABC',   895,    11, 30),
('ABC',   895,    12, 30),
('ABC',   895,    13, 60),
('ABC',   895,    14, 90),
('ABC',   895,    15, 90);
GO
24 rows affected
SELECT DISTINCT STORENAME, JOBNO, JOB_STATUS
FROM   jobs
WHERE  JOB_STATUS = 60
AND    JOBNO NOT IN (SELECT JOBNO
                         FROM   jobs
                         WHERE  JOB_STATUS >= 90)
GO
STORENAME | JOBNO | JOB_STATUS
:-------- | ----: | ---------:
ABC       |   771 |         60

dbfiddle here

答案 1 :(得分:0)

您想要的是找到状态为60但状态为>的作业。 90:

select job_no
from dbo.jobtracking
group by job_no
having count(case when job_status = 60 then 1 end) > 0
   and count(case when job_status > 90 then 1 end) = 0;

完整的查询:

select ul.storename, gd.job_no, gd.storeid, gd.warranty 
from dbo.servicejob gd
join dbo.datadetails ul on gd.storeid = ul.storeid
where gd.job_no in
(
  select jt.job_no
  from dbo.jobtracking jt
  group by jt.job_no
  having count(case when jt.job_status = 60 then 1 end) > 0
     and count(case when jt.job_status > 90 then 1 end) = 0
);

答案 2 :(得分:0)

使用 ROW_NUMBER()使用分区以及消除具有JobStatus> = 90的行

;WITH T AS
(
    SELECT
        *,
        CASE WHEN JobStatus>=90 THEN 0 ELSE 1 END AS IsConsider         
    FROM @tblTest           
)
SELECT
    StoreName,
    JobNo,
    Seq,
    JobStatus
FROM
(
    SELECT 
        StoreName,
        JobNo,
        Seq,
        JobStatus,
        ROW_NUMBER() OVER (PARTITION BY JobNo ORDER BY Seq DESC) JobPartNo
    FROM T 
    WHERE JobStatus=60
    AND JobNo NOT IN (SELECT JobNo FROM T WHERE IsConsider=0)
) AS X
WHERE JobPartNo=1

<强>输出:

enter image description here