简体   繁体   English

sql中如何处理子查询?

[英]how to handle sub query in sql?

I have a table containing the following columns: id, sid, subcode, subStatus,printdate 我有一个包含以下各列的表:id,sid,子代码,subStatus,printdate

Sid stand for student id. Sid代表学生证。 I want student with finalStaus.Finalstatus is base on last subcode of that sid.In first case sid is 5 and Finalstatus is Fail.Here lastsubcode is 4. In second case sid is 3 and finalstaus is promo.Here lastsubcode is 3. 我希望学生使用finalStaus.Finalstatus基于该sid的最后一个子代码。在第一种情况下sid是5并且Finalstatus是Fail。在这里lastsubcode是4

Subcode can be 1,2,3,4 ..... 子码可以是1,2,3,4 .....

Previously my table has 4 column. 以前我的表有4列。

Id    SId   SubCode  SubStatus     
1     5       4       Fail         
2     5       3       pass         
3     5       2       pass         
4     5       1       fail         
5     3       3       promo        
7     3       2       promo        
8     3       1       pass  

with the following query I am able to fetch same what I want. 通过以下查询,我能够获取所需的内容。

SELECT studentdetails.SId, studentdetails.substatus
FROM studentdetails
JOIN (
  SELECT studentdetails.sid, MAX(studentdetails.subcode) AS max_subcode
  FROM studentdetails
  GROUP BY studentdetails.sid
) m ON m.sid=studentdetails.sid AND m.max_subcode=studentdetails.subcode

Now I added extra column
Id    SId   SubCode  SubStatus     PrintDate
1     5       4       Fail         2014-06-05 17:00:00.000
2     5       3       pass         2014-06-05 17:10:00.000
3     5       2       pass         2014-06-05 17:20:00.000
4     5       1       fail         2014-06-05 17:40:00.000
5     3       3       promo        2014-06-06 00:20:00.000
7     3       2       promo        2014-06-06 00:10:00.000
8     3       1       pass         2014-06-05 11:59:00.000

I want student with finalStaus of perticular date.Here date is 5.Finalstatus is base on last subcode of that sid. 我想让学生具有perticular日期的finalStaus。这里的日期是5.Finalstatus基于该sid的最后一个子代码。 Here is my query 这是我的查询

SELECT studentdetails.SId, studentdetails.substatus
FROM studentdetails
JOIN (
  SELECT studentdetails.sid, MAX(studentdetails.subcode) AS max_subcode
  FROM studentdetails
  where PrintDate>= '06/05/2014' and printdate <'06/06/2014'
  GROUP BY studentdetails.sid
) m ON m.sid=studentdetails.sid AND m.max_subcode=studentdetails.subcode

With above query I am getting output. 通过上面的查询,我得到输出。

SId SubStatus
3   pass
5   Fail

Here my logic fail.I don't want sid=3 should come.Sid having value 3,its actual last subcode is 3 but that is next day.So I modified query. 这是我的逻辑失败。我不希望sid = 3出现。具有值3的sid,它的实际最后一个子代码是3,但这是第二天。所以我修改了查询。

SELECT studentdetails.SId, studentdetails.substatus
FROM studentdetails
JOIN (
  SELECT studentdetails.sid, MAX(studentdetails.subcode) AS max_subcode
  FROM studentdetails
  where PrintDate>= '06/05/2014' and printdate <'06/06/2014'
  and SId not in (Select SId from studentDetails where  PrintDate>= '06/06/2014' and printdate <'06/07/2014')
  GROUP BY studentdetails.sid
) m ON m.sid=studentdetails.sid AND m.max_subcode=studentdetails.subcode

OUtput: 输出:

SId SubCode
5   Fail

With the above query I am getting proper output.I just want to know when I am writing subquery, 通过上面的查询,我得到了正确的输出。我只想知道何时编写子查询,

SId not in : Here which sid will consider from 1 or 2.

1  sid from (SELECT studentdetails.sid, MAX(studentdetails.subcode) AS max_subcode
      FROM studentdetails
      where PrintDate>= '06/05/2014' and printdate <'06/06/2014')

   SId SubStatus
    3   pass
    5   Fail

2 all sid from studentdetails

I also wanted to find out only count of pass substatus of student whose final status is not pass.Only want count of substatus not sid 我还想只查找最终状态为不及格的学生的通过子状态的计数。只希望不通过sid的子状态的计数

   Id    SId   SubCode  SubStatus     
    1     5       4       Fail         
    2     5       3       pass         
    3     5       2       pass         
    4     5       1       fail 

Here id 1 and 2 has pass status.Here Sid is 5.But Final status of sid 5 is Fail.Because final status is base on max subcode.How to find that? 这里的id 1和2具有通过状态,这里的Sid是5,但是sid 5的最终状态是Fail,因为最终状态是基于最大子码的,如何找到呢?

Question 1. SId not in : Here which sid will consider from 1 or 2? 问题1. SId不在:此处从1或2考虑哪个sid?

Ans:Neither. 答:都不。 Subquery doesnot care about the query outside,. 子查询不在乎外部查询。 So it will be all sid from studentdetails filtered by the where Clause( PrintDate>= '06/06/2014' and printdate)<'06/07/2014') 因此,这将是所有学生详细信息的sid,并由where子句过滤(PrintDate> = '06 / 06/2014'和printdate)<'06/07/2014')

For your question 2: Just wrap your first query as a cte, then the remaining is simple 对于您的问题2:只需将第一个查询包装为cte,然后剩下的就很简单了

with cte (SId,SubStatus)as(
    SELECT studentdetails.SId, studentdetails.substatus
    FROM studentdetails
    JOIN (
      SELECT studentdetails.sid, MAX(studentdetails.subcode) AS max_subcode
      FROM studentdetails
      GROUP BY studentdetails.sid
    ) 
    m ON m.sid=studentdetails.sid AND m.max_subcode=studentdetails.subcode)

    select SId,count(SubStatus) as PassCount from studentdetails 
where SId in(select SId from cte where SubStatus <> 'pass') 
and SubStatus = 'pass' group by SId

PS: Remember to take care of upper and lower case if this is actual data PS:如果这是实际数据,请记住要注意大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM