我有一个查询,我想最终将其放入视图中。
首先,只要我有 2 列,我可以以不同的方式命名我的 Flag 列吗?
另外,我可以在同一张表上使用不同的 WHERE 子句执行联合吗?
最后,将这 3 个选择放入 1 个视图的正确方法是什么?
SELECT ID, 'Y' AS PW_CHANGE31, NULL AS LAST_LOGON90, NULL AS LAST_LOGON365 FROM user_info WITH (nolock) WHERE ([Last Password Change] < GETDATE() - 31)
UNION ALL
SELECT ID, NULL AS PW_CHANGE31, 'Y' AS LAST_LOGON90, NULL AS LAST_LOGON365 FROM user_info WITH (nolock) WHERE ([Last Logon] < GETDATE() - 90) AND ([Account Disabled] = 0)
UNION ALL
SELECT ID, NULL AS PW_CHANGE31, NULL AS LAST_LOGON90, 'Y' AS LAST_LOGON365 FROM user_info WITH (nolock) WHERE ([Last Logon] < GETDATE() - 365)
**更新为:
SELECT ID, '31' AS StatusVal FROM user_info WITH (nolock) WHERE ([Last Password Change] < GETDATE() - 31)
UNION
SELECT ID, '90' AS StatusVal FROM user_info WITH (nolock) WHERE ([Last Logon] < GETDATE() - 90) AND ([Account Disabled] = 0)
UNION
SELECT ID, '365' AS StatusVal FROM user_info WITH (nolock) WHERE ([Last Logon] < GETDATE() - 365)