我有一张包含这样记录的表格。
S.No | Name | Date | Type
1 | test | 2014-1-1 | In
2 | test | 2014-1-1 | Out
3 | test12 | 2014-1-1 | In
4 | test12 | 2014-1-1 | Out
5 | test123| 2014-1-1 | In
所需要的是一个sql查询,它只返回type = In的记录,并且在同一天没有为它做过type = out。我试过了 从dbo.TimeCheckInOut中选择日期,COUNT(*)作为计数,其中EmployeeNo =' E012'按日期分组。这不起作用..我怎样才能返回那些类型只有
的记录答案 0 :(得分:2)
如果你想获得只有type = In且没有type = out 的记录,那么我建议这样做:
select *
from Table1 as t
where
t.[Type] = 'In' and
not exists (
select *
from Table1 as tt where tt.Name = t.Name and tt.[Type] = 'Out'
)
<强> sql fiddle demo 强>
答案 1 :(得分:0)
试试这个
SELECT Name,Date,Type From
Table 1 Where Type = 'In' And
Name Not In (Select Name From Table1 Where Type = 'Out')
<强> Fiddle Demo 强>
<强> O / P 强>
SNO NAME DATE TYPE
------------------------------------------------
5 test123 January, 01 2014 00:00:00+0000 In
答案 2 :(得分:0)
SELECT * FROM table1 t1
WHERE t1.type = 'IN' AND NOT EXISTS (SELECT * FROM table1 t2 WHERE t2.type = 'OUT' AND t2.name = t1.name)
上述查询将产生所需的输出。
答案 3 :(得分:0)
您可以使用以下查询..
select * from test a
where a.Type='In'
and not exists (select null from test b where b.Type='Out' and b.Name=a.Name)
答案 4 :(得分:0)
另一种方法
select in.*
from table in
left join table out
on out.name = in.name
and in.type = 'In'
and out.type = 'Out'
where out.name is null