Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 我怎样才能解决这样的问题_Sql Server - Fatal编程技术网

Sql server 我怎样才能解决这样的问题

Sql server 我怎样才能解决这样的问题,sql-server,Sql Server,我在sql server中有一个问题。我有一些记录,如 PKId EquipmentID StudentId Issueddate Returneddate 1 116 230 2014-01-14 17:14:58.940 2014-01-18 14:12:038.876 2 116 237 2014-01-14 17:14:58.940 NULL

我在sql server中有一个问题。我有一些记录,如

PKId EquipmentID StudentId      Issueddate                  Returneddate
1        116     230            2014-01-14 17:14:58.940     2014-01-18 14:12:038.876
2        116     237            2014-01-14 17:14:58.940     NULL
3        117     400            2014-01-14 17:14:58.940     2014-01-18 14:12:038.876
这里EquipmentID和StudentId是外键。我们需要编写一个查询,根据Returneddate为我们提供数据。我们需要检查第一行EquipmentID为116的条件,并将其分配给StudentId 230,其中Returneddate不为null意味着现在这116个设备是免费的,意味着它未分配,但在下一行相同设备分配给学生237,hiis returneddate为空表示该设备分配给某人,在最后一种情况下,设备ID 117分配给学生ID 400,且其returneddate不为空表示该设备可用

所以我们只需要显示那些可用的记录。如果我以这个案例为例,它会给我

3 117 400 2014-01-14 17:14:58.940 2014-01-18 14:12:038.876
行作为输出

我试过:

select * from Equipment where (EquipmentID not in (select EquipmentID
from PEquip where Returneddate is null))
请帮帮我

希望您能理解。

试试这个:

select * from theTable t1 where returneddate = 
    (select max Issueddate from theTable t2 where t1.EquipmentId = t2.EquipmentId)
and returneddate is not NULL;
试试这个:

select * from theTable t1 where returneddate = 
    (select max Issueddate from theTable t2 where t1.EquipmentId = t2.EquipmentId)
and returneddate is not NULL;

如果我没有弄错,您需要
EquipmentID
,其中没有空
Returneddate

SELECT * FROM T as T1 
      WHERE NOT EXISTS (SELECT * FROM T 
                            WHERE EquipmentID=T1.EquipmentID
                                  AND Returneddate IS NULL)

如果我没有弄错,您需要
EquipmentID
,其中没有空
Returneddate

SELECT * FROM T as T1 
      WHERE NOT EXISTS (SELECT * FROM T 
                            WHERE EquipmentID=T1.EquipmentID
                                  AND Returneddate IS NULL)



假设
PkId
的顺序与数据的年表一致,我会选择

SELECT PKId, EquipmentID, StudentId, Issueddate, Returneddate 
FROM Equipment E 
INNER JOIN (SELECT MAX(PKId) AS PKId FROM Equipment GROUP BY EquipmentId) AS T
ON E.PKId = T.PKId
WHERE E.ReturnedDate IS NOT NULL

逻辑:为每个
设备ID
选择最新条目,如果最新条目返回的日期不为空。

假设
PkId
的顺序与数据的时间顺序一致,我会选择

SELECT PKId, EquipmentID, StudentId, Issueddate, Returneddate 
FROM Equipment E 
INNER JOIN (SELECT MAX(PKId) AS PKId FROM Equipment GROUP BY EquipmentId) AS T
ON E.PKId = T.PKId
WHERE E.ReturnedDate IS NOT NULL

逻辑:为每个
EquipmentId
选择最新条目,如果最新条目返回的日期不为空。

MySQL或SQL Server?它是哪一个?显示您已尝试的查询DmySQL或SQL Server?它是哪一个?显示您尝试过的查询我认为第一条记录将作为输出根据您的查询您说什么我认为第一条记录将作为输出根据您的查询您说什么