我在SQL中有307807行的表。如果我的start_date和end_date列具有无效值,我想删除记录。不幸的是,我的userID重复了不止一次。因此,我想删除受无效值影响的用户ID。
UserID ---- start_date ---- end_date
123 ---- 06-05-1934 ---- 16-05-1934
123 ---- 06-05-1933 ---- 16-05-1933
123 ---- /N ---- 16-03-1920
234 ---- 04-03-1945 ---- 17-04-1945
234 ---- 05-06-1999 ---- 06-07-1999
这里我想删除所有带有用户ID 123的记录,因为我不能再使用该数据。
我使用MS SQL Server Management Studio 17。
答案 0 :(得分:1)
使用;WITH cte
AS (SELECT indictr = Min(CASE WHEN Try_convert(datetime, start_date, 105) IS NULL OR Try_convert(datetime, end_date, 105) IS NULL THEN 0 ELSE 1 END)
OVER(partition BY UserID ),*
FROM Yourtable)
DELETE FROM cte
WHERE indictr = 0
检查token_url = "https://api.fitbit.com/oauth2/token"
access_url = "https://api.fitbit.com/oauth2/token"
auth_url = "https://www.fitbit.com/oauth2/authorize"
key = "XXXX"
secret = "XXXX"
fbr = oauth_app("Glinski",key,secret)
fitbit = oauth_endpoint(token_url,auth_url,access_url)
token = oauth2.0_token(fitbit,fbr, scope = c("activity","heartrate"))
是否有效
GET(url = "http://api.fitbit.com/1/user/-/activities/heartrate/date/2018-01-
01/today.json", config(token = token))
答案 1 :(得分:0)
您需要选择日期不佳的所有用户,然后删除这些用户的记录:
WITH BadUsers as (
SELECT DISTINCT UserID from TableA
where start_date = '/n' or end_date ='n'
)
DELETE FROM TableA
JOIN BadUsers ON
Table.UserID = BadUser.UserID
答案 2 :(得分:0)
您可以使用 TRY_CAST (Transact-SQL) 作为:
CREATE TABLE T (
ID INT,
SDate VARCHAR(10),
EDate VARCHAR(10)
);
INSERT INTO T VALUES
(123, '1934-05-06', '1634-05-16'),
(123, '/N', '1934-05-25'),
(123, '1934-05-26', 'WrongDate');
SELECT *
FROM T;
DELETE FROM T
WHERE ID IN
(
SELECT DISTINCT ID
WHERE (TRY_CAST(SDate AS DATE) IS NULL) OR (TRY_CAST(EDate AS DATE) IS NULL)
);
SELECT *
FROM T;