久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 SQL Server 中使用 T-SQL 操作登錄注銷數(shù)據(jù)

Manipulate Login Logout data with T-SQL in SQL Server(在 SQL Server 中使用 T-SQL 操作登錄注銷數(shù)據(jù))
本文介紹了在 SQL Server 中使用 T-SQL 操作登錄注銷數(shù)據(jù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

有誰(shuí)知道用表1中的信息構(gòu)建表2的方法嗎?使用 Python 很容易接近,因?yàn)槲铱梢允褂冒葱袡z查".但是,后面有一個(gè)大數(shù)據(jù)集,所以如果我可以在SQL Server中用SQL語(yǔ)言進(jìn)行數(shù)據(jù)轉(zhuǎn)換,那就太好了.請(qǐng)注意,這不是真正的登錄和注銷數(shù)據(jù)結(jié)構(gòu)/問(wèn)題,我只想知道如何將表 2 中的數(shù)據(jù)轉(zhuǎn)換為表 1.它與我現(xiàn)在擁有的數(shù)據(jù)具有相似的結(jié)構(gòu),但用于其他用途.

Does anyone know the way of building table 2 with information in table 1? It is easy to approach with Python because I can use ‘checking by row’. However, there is a big dataset at the back so if I could conduct the data transformation with SQL language in SQL Server it will be nice. Notice that this is not a real login and logout data structure/problem, and I just want to know how to transform data in table 2 into table 1. It has the similar structure with the data I have right now but for other use.

詳情:當(dāng)用戶第一次登錄我的系統(tǒng)時(shí),我在表2中用‘LoginTime’記下時(shí)間.他可能會(huì)多次登錄我的系統(tǒng),但我只會(huì)記錄他第一次登錄的時(shí)間.當(dāng)他第一次注銷我的系統(tǒng)時(shí),我會(huì)將表 1 中的Eventtime"記錄為表 2 中的LogoutTime".如果同一用戶沒(méi)有注銷,我會(huì)將 LogoutTime 保留為NULL".

Details: When the user first logs in to my system, I write down the time with ‘LoginTime’ in table 2. He might login to my system for several times but I will only record the very first time when he login. When he first logout of my system, I will record the ‘Eventtime’ from table 1 as ‘LogoutTime’ in table 2. If the same user doesn’t logout, I will keep the LogoutTime as ‘NULL’.

表一

UserID  EventTime   Event
1   9/1/13 15:33    0
1   9/1/13 17:00    0
1   9/1/13 18:00    0
1   9/1/13 18:20    1
1   9/1/13 18:30    1
1   9/2/13 11:05    0
1   9/2/13 11:45    1
1   9/2/13 13:50    0
2   9/1/13 16:15    0
2   9/1/13 17:00    1
2   9/1/13 18:01    0
2   9/1/13 18:02    0
2   9/1/13 19:02    1
3   9/1/13 17:10    0
3   9/1/13 19:10    1
3   9/2/13 21:01    0

表 2

UserID  LoginTime   LogoutTime
1   9/1/13 15:33    9/1/13 18:20
1   9/2/13 11:05    9/2/13 11:45
1   9/2/13 13:50    NULL
2   9/1/13 16:15    9/1/13 17:00
2   9/1/13 18:02    9/1/13 19:02
3   9/1/13 17:10    9/1/13 19:10
3   9/1/13 21:01    NULL

推薦答案

您好,

-- I assume that your date and time data is in format "mm/dd/yy", which means style 1
-- For better aqurecy I am using datetime2(7) 
drop table if exists T;
create table T(UserID int, EventTime datetime2(7), [Event] bit)
GO
INSERT T(UserID,EventTime,Event)
values
(1,CONVERT(datetime2(7),'9/1/13 15:33', 1), 0),
(1,CONVERT(datetime2(7),'9/1/13 17:00', 1), 0),
(1,CONVERT(datetime2(7),'9/1/13 18:00', 1), 0),
(1,CONVERT(datetime2(7),'9/1/13 18:20', 1), 1),
(1,CONVERT(datetime2(7),'9/1/13 18:30', 1), 1),
(1,CONVERT(datetime2(7),'9/2/13 11:05', 1), 0),
(1,CONVERT(datetime2(7),'9/2/13 11:45', 1), 1),
(1,CONVERT(datetime2(7),'9/2/13 13:50', 1), 0),
(2,CONVERT(datetime2(7),'9/1/13 16:15', 1), 0),
(2,CONVERT(datetime2(7),'9/1/13 17:00', 1), 1),
(2,CONVERT(datetime2(7),'9/1/13 18:01', 1), 0),
(2,CONVERT(datetime2(7),'9/1/13 18:02', 1), 0),
(2,CONVERT(datetime2(7),'9/1/13 19:02', 1), 1),
(3,CONVERT(datetime2(7),'9/1/13 17:10', 1), 0),
(3,CONVERT(datetime2(7),'9/1/13 19:10', 1), 1),
(3,CONVERT(datetime2(7),'9/2/13 21:01', 1), 0)
GO
SELECT * FROM T
order by UserID, EventTime, Event
GO

解決方案初稿

請(qǐng)檢查這是否滿足您的需求

First draft of solution

Please check if this solve your needs

;with MyCTE as (
    SELECT UserID, EventTime, [Event]
        , [RN1-RN2] = ROW_NUMBER() over (order by UserID, EventTime, [Event]) - ROW_NUMBER() over (partition by UserID, [Event] order by UserID, EventTime, [Event])
    FROM T
),
MyCTE2 as (
    select distinct UserID, [Event] 
        , MIN(EventTime) OVER (partition by UserID,[Event], [RN1-RN2]) M
    from MyCTE
)
select UserID
  , [0] as LoginTime
  , [1] as LogoutTime
From (
    select UserID, [Event], M
        , ROW_NUMBER() OVER(partition by UserID,[Event] order by M) as GroupNum
    from MyCTE2
)x
pivot
(
  MIN(M)
  for [Event] in([0], [1])
)p
order by UserID, [LoginTime]
GO

如果此解決方案適合您,那么我們知道我滿足了您的需求,我們可以進(jìn)行下一步,即討論性能.為此,我們需要獲取您的真實(shí)標(biāo)簽;e 結(jié)構(gòu)和更多示例數(shù)據(jù)(來(lái)自您的 DDL+DML)

If this solution fits you then we know that I got your needs, and we can move to the next step which is discuss about performance. For this we will need to get your real tab;e structure ans some more sample data (DDL+DML from you)

這篇關(guān)于在 SQL Server 中使用 T-SQL 操作登錄注銷數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號(hào)或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 超碰导航| 男女视频免费 | 黄色一级大片视频 | 国产女人叫床高潮大片免费 | 亚洲欧美国产一区二区三区 | 久久y| 午夜视频网站 | 国产精品一区久久久久 | 国产精品99999999 | 午夜视频一区二区三区 | 综合久久久 | 自拍偷拍亚洲一区 | 国产精品色哟哟网站 | 美女爽到呻吟久久久久 | 91丨九色丨国产在线 | 看黄在线 | 国产精品视频一区二区三区 | 综合亚洲视频 | 欧美区在线观看 | 一级免费看片 | 亚洲高清久久 | 欧美激情黄色 | 美女视频一区二区三区 | 综合久久99| 97超碰在线播放 | 国产福利一区二区 | 免费在线成人网 | av在线播放不卡 | 伊人网伊人网 | 精品在线观看一区 | 成人一级片在线观看 | 精品视频在线免费观看 | 亚洲一区二区三区在线视频 | 我爱操| 久久久久久久久久久爱 | 中文字幕亚洲区一区二 | 亚洲精选久久 | 亚洲精品久久久9婷婷中文字幕 | 久久久久亚洲精品 | 日韩欧美在线播放 | 欧美精品一区二区三区视频 |