嵌套选择TOP 1语句

时间:2014-12-09 14:00:11

标签: sql sql-server select nested subquery

我正在为我们公司内部的资产管理系统工作。

系统允许用户在永久或临时的基础上登录/退出项目。

我正在研究系统的一些报道,并且在返回相关数据方面遇到了绊脚石。

所以我知道我需要在我的主查询中使用嵌套的Select TOP 1语句,但我无法使其工作。

主要查询如下:

SELECT [Asset-User].ID, [Asset-User].Asset_ID, [Assets].ID, [Assets].Signed_Out, [Assets].Asset_Tag_Serial_Num, [Assets].Name_Hostname, [Assets].Type_ID, [Asset_Type].ID, [Asset_Type].Name_Model, [Asset-User].User_ID, [Company_Users].ID,  [Asset-User].Sign_Out_Date, [Asset-User].Return_Date, [Asset-User].[Perm_Signout?]
FROM [Asset-User], [Assets], [Asset_Type], [Company_Users]
WHERE ([User_ID] = '1') AND [Asset-User].Asset_ID = [Assets].ID AND [Assets].Type_ID = [Asset_Type].ID  AND [Asset-User].User_ID = [Company_Users].ID AND [Assets].Signed_Out = '1'

因此,此查询将返回当前标记为“已签名”的所有内容,该内容已由用户1注销。

在这里的某个地方我需要在Asset-User.ID上添加一个嵌套的Select TOP 1,这样它才会返回标记为永久签名的项目,如果不是永久性的,那么它们最近已被用户1注销

这样只会给我一个当前分配给该用户的项目列表,如果其他人已经签署了该项目,则不会显示该资产

非常感谢任何帮助

根据要求下面的示例数据: https://docs.google.com/spreadsheets/d/1o4T6bsxyO-1dGE0-FUtWFboRupcq4o4V9i2Em0_BjyU/edit?usp=sharing

第一张表显示实际结果,第二张表显示了应该预期的结果

正如您将在此处看到的,此用户已多次注销了一些项目(这些项目可能最近未被此用户注销)

很难获得样本数据,但实际上列表中应该没有重复的Asset_ID,因为只应为每个Asset_ID显示最新的ID(与此相关的注销ID)。

这有帮助吗?

再次感谢

根据请求,这里是Schema构建代码,它将创建一个与

一起使用的示例数据库
CREATE TABLE Asset_Type
    ([ID] int, [Global_ID] int, [Name_Model] varchar(30), [Description_Spec] varchar(54))
;

INSERT INTO Asset_Type
    ([ID], [Global_ID], [Name_Model], [Description_Spec])
VALUES
    (1, 1, 'Dell Optiplex 3020', 'Windows 7 Professional, Intel Core i3 3.40GHz,4Gb RAM'),
    (2, 3, 'Viewsonic VA2231wa', 'Viewsonic Widescreen Monitor'),
    (3, 3, 'Samsung S24B150BL', 'Samsung LED 24" Widescreen Monitor')
;

CREATE TABLE Assets
    ([ID] int, [Asset_Tag_Serial_Num] varchar(29), [Type_ID] int, [Purchase_Date] varchar(10), [Purchase_Price] varchar(7), [Name_Hostname] varchar(36), [Signed_Out] int)
;

INSERT INTO Assets
    ([ID], [Asset_Tag_Serial_Num], [Type_ID], [Purchase_Date], [Purchase_Price], [Name_Hostname], [Signed_Out])
VALUES
    (1, '0206', 1, '2013-11-29', '323.30', 'WS0206', 1),
    (3, '0226', 2, NULL, NULL, 'Viewsonic VA2231wa - 0226', 1),
    (4, '0204', 1, '2013-11-29', '323.00', 'WS0204', 1),
    (5, '0205', 1, '2013-11-29', '323.00', 'WS0205', 1),
    (6, '0108', 1003, NULL, NULL, 'Small Office Sat Nav', 1),
    (7, '0092', 1004, NULL, NULL, 'Large Office Sat Nav', 1),
    (8, 'GWC36-DHDBC-J2MXY-H2BGY-8C79G', 1005, '1900-01-01', '0.00', 'MS Office for WS0020', 1),
    (9, '0020', 1006, '1900-01-01', '0.00', 'WS0020', 1),
    (10, '0173', 2, '1900-01-01', '0.00', 'Viewsonic VA2231wa - 0173', 1),
    (11, '0172', 1007, '1900-01-01', '0.00', 'Dell 19" Monitor 0172', 1),
    (12, '00104926EC6B', 1008, '1900-01-01', '0.00', 'Shortel 230 - EC6B', 1),
    (13, '0227', 1009, NULL, NULL, 'Blue - Yeti Mic', 0),
    (14, '0221', 1, NULL, NULL, 'WS0221', 1),
    (15, '0222', 1, '2013-11-29', '323.00', 'WS0222', 1),
    (16, '0223', 1, NULL, NULL, 'WS0223', 1),
    (17, '0220', 1, '2013-11-29', '323.00', 'WS0220', 1),
    (18, '0217', 1, '2013-11-29', '323.00', 'WS0217', 1),
    (19, '0218', 1, NULL, NULL, 'WS0218', 1),
    (20, '0219', 1, '2013-11-29', '323.00', 'WS0219', 1),
    (21, '0228', 2, NULL, NULL, 'Viewsonic VA2231wa - 0228', 1),
    (22, '0229', 1010, NULL, NULL, 'Dell 19" Monitor 0229', 1),
    (23, '00104931AA16', 1011, NULL, NULL, 'Shortel 115 - AA16', 1),
    (24, '0093 - DYTJ18X4DJ8T', 1012, NULL, NULL, 'Office IPad 3', 1),
    (25, '0095', 1013, '1900-01-01', '0.00', '320Gb External HDD', 1),
    (26, '0071', 1014, NULL, NULL, '0071 - NEC Projector', 0),
    (27, '0072', 1015, NULL, NULL, '0072 - Black Dell Projector', 0),
    (28, '0073', 1016, '1900-01-01', '0.00', '0073 - Dell Projector', 0),
    (29, '0230', 1017, '1900-01-01', '0.00', '0230 - Silver Dell Projector', 0),
    (30, '0064', 1018, NULL, NULL, 'WS0064', 0),
    (31, '0231', 1019, NULL, NULL, 'Freecom 1GB Pen - 0231', 1),
    (47, '0165', 2, NULL, NULL, 'Viewsonic VA2231wa - 0165', 1),
    (48, '0232', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0232', 1),
    (49, '0233', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0233', 1),
    (50, '0137', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0137', 1),
    (51, '0234', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0234', 1),
    (52, '0235', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0235', 1),
    (53, '0134', 1010, NULL, NULL, 'Dell 19" Monitor 0134', 0),
    (54, '0135', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0135', 1),
    (55, '0236', 3, '1900-01-01', '0.00', 'Samsung S24B150BL - 0236', 1),
    (56, '001049201D9A', 1008, '1900-01-01', '0.00', 'Shortel 230 - 1D9A', 1),
    (57, '0010492015AE', 1008, '1900-01-01', '0.00', 'Shortel 230 - 15AE', 1),
    (93, '0269', 1029, '1900-01-01', '0.00', 'TP-Link Switch - 0269', 0),
    (94, '0058', 1030, NULL, NULL, 'WS0058', 1),
    (95, '0270', 1031, NULL, NULL, 'MeetingRoom3', 1),
    (96, '0243', 1032, NULL, NULL, 'MeetingRoom2', 1),
    (97, '0271', 1027, NULL, NULL, 'Dynamode SW80010-D Switch - 0271', 0),
    (123, '0281', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0281', 0),
    (124, '0282', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0282', 1),
    (125, '0283', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0283', 0),
    (126, '0284', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0284', 1),
    (127, '0285', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0285', 1),
    (128, '0286', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0286', 1),
    (129, '0287', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0287', 1),
    (143, '0280', 1, '2014-07-03', '403.80', 'WS0280', 1),
    (144, '0296', 1, '2014-07-03', '403.80', 'WS0296', 1),
    (145, '0297', 1, '2014-07-03', '403.80', 'WS0297', 1),
    (146, '0298', 1, '2014-07-03', '403.80', 'WS0298', 1),
    (147, '0299', 1, '2014-07-03', '403.80', 'WS0299', 1),
    (148, '0052', 1036, '1900-01-01', '0.00', 'WS0052', 1),
    (168, '0312', 1047, NULL, NULL, 'Epson White HD - 0312', 1),
    (169, '0201', 1049, '1900-01-01', '0.00', 'Ipad 4 - 0201', 0),
    (170, 'HP27J-2C496-83KXB-RGMX6-8QJQG', 1020, '1900-01-01', '0.00', 'MS Office for Jonny D', 1),
    (171, '7N4QY-DFGWD-P6662-CFCHG-QYFP2', 1021, '1900-01-01', '0.00', 'MS Office for WS0215 (Simeon Laptop)', 1),
    (172, '0140', 1022, '1900-01-01', '0.00', 'Viewsonic VA2248-LED - 0140', 1),
    (198, '0109', 1037, '1900-01-01', '0.00', 'WS0109', 1),
    (199, '0324', 1052, '1900-01-01', '0.00', 'Philips 23.6" Monitor 0324', 1)
;

CREATE TABLE [Asset-User]
    ([ID] int, [Asset_ID] int, [User_ID] int, [Sign_Out_Date] datetime, [Return_Date] datetime, [Perm_Signout] int)
;

INSERT INTO [Asset-User]
    ([ID], [Asset_ID], [User_ID], [Sign_Out_Date], [Return_Date], [Perm_Signout])
VALUES
    (2, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (3, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (4, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (5, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (6, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (7, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (8, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (9, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (10, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (11, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
    (1002, 1, 1, '2014-04-01 00:00:00', '2014-04-01 00:00:00', 0),
    (1003, 1, 1, '2014-01-01 00:00:00', '2014-01-01 00:00:00', 1)

;

CREATE TABLE Company_Users
    ([ID] int, [Name] varchar(14), [Domain_Username] varchar(14), [Dept] varchar(16), [Email] varchar(25), [DD_Ext] int, [Job_Title] varchar(30), [Deleted_Left] int)
;

INSERT INTO Company_Users
    ([ID], [Name], [Domain_Username], [Dept], [Email], [DD_Ext], [Job_Title], [Deleted_Left])
VALUES
    (1, 'Neil Smithson', 'Neil.Smithson', '2nd Line Support', 'neil.smithson@dezrez.com', 3041, 'Second Line Support Technician', 0)

;

CREATE TABLE Global_Types
    ([ID] int, [Name] varchar(13), [Description] varchar(54))
;

INSERT INTO Global_Types
    ([ID], [Name], [Description])
VALUES
    (1, 'PC', 'Desktop PC')


;

1 个答案:

答案 0 :(得分:0)

这不是一个真正的答案,但会向您展示如何使用别名和(不是真正的)更新的连接样式来重写此查询。

SELECT au.ID
    , au.Asset_ID
    , a.ID
    , a.Signed_Out
    , a.Asset_Tag_Serial_Num
    , a.Name_Hostname
    , a.Type_ID
    , at.ID
    , at.Name_Model
    , au.User_ID
    , cu.ID
    , au.Sign_Out_Date
    , au.Return_Date
    , au.[Perm_Signout?]
FROM [Asset-User] au
join Assets a on au.Asset_ID = a.ID
join Asset_Type at on a.Type_ID = at.ID
join Company_Users cu on au.User_ID = cu.ID
WHERE au.User_ID = '1' 
    AND a.Signed_Out = '1'