SQL 查询具有带有二进制指示符和时间差的新列
Posted
技术标签:
【中文标题】SQL 查询具有带有二进制指示符和时间差的新列【英文标题】:SQL query to have new columns with binary indicator and time difference 【发布时间】:2018-08-01 03:28:17 【问题描述】:我正在尝试从 table1 编写 sql 查询以获得类似表 2 的结果,但未能成功。 任何建议将不胜感激。
我有一个如下表:
+--------+--------------+----------+---------------+-----------------------+
| UserID | account_type | Region | account_Level | Level_assessment_date |
+--------+--------------+----------+---------------+-----------------------+
| 1234 | A | west268 | Normal | Jul-17 |
| 1234 | A | west268 | platinum | Jul-18 |
| 2342 | B | north234 | Normal | May-15 |
| 3565 | A | west268 | Normal | Jun-14 |
| 3565 | B | west268 | Normal | May-17 |
| 5678 | A | west268 | platinum | Sep-15 |
| 6689 | A | north234 | Normal | Oct-16 |
| 6689 | B | north234 | platinum | Jan-18 |
+--------+--------------+----------+---------------+-----------------------+
我想查询 table1 以获取 table2,如下所示:
+--------+--------------+----------+---------------+-----------------------+----------------------+----------------------+
| UserID | account_type | Region | account_Level | Level_assessment_date | upgraded_to_platinum | Time_to_upgrade_DAYS |
+--------+--------------+----------+---------------+-----------------------+----------------------+----------------------+
| 1234 | A | west268 | Normal | Jul-17 | Y | 365 |
| 2342 | B | north234 | Normal | May-15 | N | N/A |
| 3565 | A | west268 | Normal | Jun-14 | N | N/A |
| 3565 | B | west268 | Normal | May-17 | N | N/A |
| 6689 | A | north234 | Normal | Oct-16 | N | N/A |
+--------+--------------+----------+---------------+-----------------------+----------------------+----------------------+
正常的每个用户帐户级别的记录 ,带有二进制(是/否)列upgrade-to-platinum,表示帐户级别是否已从普通升级到白金。 和列 Time_to_upgrade_DAYS 从正常到白金所需的天数(如果有)
【问题讨论】:
你的 rdbms 是什么? Sql Server、postgres、oracle? 为什么5678
没有出现在结果中?为什么每个 account_level 都有 Normal?
非常感谢您的帮助。我使用 sql 服务器。 5678 不在结果中,因为我只需要找到 account_level "Normal" 的结果,所以结果中不会出现 "platinum" account_level
【参考方案1】:
如果你有 sql-server 你可以试试这个。
DECLARE @table1 TABLE ( UserID INT, account_type VARCHAR(10), Region VARCHAR(10), account_Level VARCHAR(10), Level_assessment_date VARCHAR(10))
INSERT INTO @table1 VALUES
(1234, 'A', 'west268 ', 'Normal ', 'Jul-17'),
(1234, 'A', 'west268 ', 'platinum', 'Jul-18'),
(2342, 'B', 'north234', 'Normal ', 'May-15'),
(3565, 'A', 'west268 ', 'Normal ', 'Jun-14'),
(3565, 'B', 'west268 ', 'Normal ', 'May-17'),
(5678, 'A', 'west268 ', 'platinum', 'Sep-15'),
(6689, 'A', 'north234', 'Normal ', 'Oct-16'),
(6689, 'B', 'north234', 'platinum', 'Jan-18')
SELECT T1.*,
CASE WHEN T2.UserID IS NOT NULL THEN 'Y' ELSE 'N' END upgraded_to_platinum,
DATEDIFF(DAY,'01-'+T1.Level_assessment_date, '01-'+T2.Level_assessment_date) Time_to_upgrade_DAYS
FROM @table1 T1
LEFT JOIN @table1 T2 ON T1.UserID = T2.UserID and T1.account_type = T2.account_type and T2.account_Level ='platinum'
WHERE
T1.account_Level = 'Normal'
结果:
UserID account_type Region account_Level Level_assessment_date upgraded_to_platinum Time_to_upgrade_DAYS
----------- ------------ ---------- ------------- --------------------- -------------------- --------------------
1234 A west268 Normal Jul-17 Y 365
2342 B north234 Normal May-15 N NULL
3565 A west268 Normal Jun-14 N NULL
3565 B west268 Normal May-17 N NULL
6689 A north234 Normal Oct-16 N NULL
【讨论】:
不客气!如果它解决了问题,您可以将其标记为答案。【参考方案2】:SQL DEMO
SELECT "UserID", "account_type",
MAX("Region") as Region,
MAX("account_Level") as account_Level,
MAX(CASE WHEN "account_Level" = 'platinum'
THEN 'yes'
ELSE 'no'
END) as upgraded_to_platinum,
MAX(CASE WHEN "account_Level" = 'platinum' THEN "Level_assessment_date" END) -
MAX(CASE WHEN "account_Level" <> 'platinum' THEN "Level_assessment_date" END) as Time_to_upgrade_DAYS
FROM Table1
GROUP BY "UserID", "account_type"
ORDER BY "UserID", "account_type"
输出
| UserID | account_type | region | account_level | upgraded_to_platinum | time_to_upgrade_days |
|--------|--------------|----------|---------------|----------------------|--------------------------------------------------|
| 1234 | A | west268 | platinum | yes | 0 years 0 mons 365 days 0 hours 0 mins 0.00 secs |
| 2342 | B | north234 | Normal | no | (null) |
| 3565 | A | west268 | Normal | no | (null) |
| 3565 | B | west268 | Normal | no | (null) |
| 5678 | A | west268 | platinum | yes | (null) |
| 6689 | A | north234 | Normal | no | (null) |
| 6689 | B | north234 | platinum | yes | (null) |
【讨论】:
以上是关于SQL 查询具有带有二进制指示符和时间差的新列的主要内容,如果未能解决你的问题,请参考以下文章