如何计算 table 中的 MAX not NULL?
How to calculate MAX not NULL in table?
如何在不为 NULL 的情况下获得 MAX 3 个月?
因此我需要 UserId、UserAgreementId 和 MAX Not NULL 以及 Group BY UserId
month
Id
UserId
UserAgreementId
SubsriptionDate
Amount
1
NULL
NULL
NULL
NULL
NULL
2
12
222
33333
2020-02-02 00:00:00.000
40000
3
NULL
NULL
NULL
NULL
NULL
4
13
222
33333
2020-04-03 00:00:00.000
5000
5
NULL
NULL
NULL
NULL
NULL
6
15
222
33333
2020-06-04 00:00:00.000
7000
7
16
222
33333
2020-07-02 00:00:00.000
8000
8
17
222
33333
2020-08-09 00:00:00.000
4000
9
NULL
NULL
NULL
NULL
NULL
10
NULL
NULL
NULL
NULL
NULL
11
NULL
NULL
NULL
NULL
NULL
12
NULL
NULL
NULL
NULL
NULL
因为结果必须是这样的
UserId
UserAgreementId
MAX
222
33333
3
Table AgreementId 在行中有 SubsriptionDate,我需要计算最大不为空的期间 Amount
UserAgreementId
SubsriptionDate
Amount
33333
2020-02-02 00:00:00.000
40000
33333
2020-04-03 00:00:00.000
5000
33333
2020-06-04 00:00:00.000
7000
33333
2020-07-02 00:00:00.000
8000
33333
2020-08-09 00:00:00.000
4000
您要查询的内容对于查询来说太复杂了 - 运行 需要很长时间。
例如,我在下面整理了一份草稿,它几乎为每个用户提供了最长的持续时间,但它引用了相同的 table 5 次。
这几乎不可读,令人费解,并且会给服务器带来比你想要的更多的负担。
取而代之的是获取记录并用更合适的table编程语言处理它们。
SELECT t.user_id, MAX(t.monthsTotal - s.monthsTotal + 1) AS longestDuration
FROM (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name) as t
INNER JOIN (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name AS a
LEFT OUTER JOIN table_name AS preceeding
ON a.user_id = preceeding.user_id
AND YEAR(a.subscriptionDate) * 12 + a.month = YEAR(preceeding.subscriptionDate) * 12 + preceeding.month + 1
WHERE preceedingMonth.id IS NULL) AS s
ON t.user_id = s.user_id
AND t.monthsTotal >= s.monthsTotal
LEFT OUTER JOIN (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name AS a
LEFT OUTER JOIN table_name AS preceeding
ON a.user_id = preceeding.user_id
AND YEAR(a.subscriptionDate) * 12 + a.month = YEAR(preceeding.subscriptionDate) * 12 + preceeding.month + 1
WHERE preceedingMonth.id IS NULL) AS dummy
ON t.user_id = dummy.user_id
AND dummy.monthsTotal > s.monthsTotal
AND t.monthsTotal > dummy.monthsTotal
WHERE dummy.id IS NULL
GROUP BY t.user_id
如何在不为 NULL 的情况下获得 MAX 3 个月?
因此我需要 UserId、UserAgreementId 和 MAX Not NULL 以及 Group BY UserId
month | Id | UserId | UserAgreementId | SubsriptionDate | Amount |
---|---|---|---|---|---|
1 | NULL | NULL | NULL | NULL | NULL |
2 | 12 | 222 | 33333 | 2020-02-02 00:00:00.000 | 40000 |
3 | NULL | NULL | NULL | NULL | NULL |
4 | 13 | 222 | 33333 | 2020-04-03 00:00:00.000 | 5000 |
5 | NULL | NULL | NULL | NULL | NULL |
6 | 15 | 222 | 33333 | 2020-06-04 00:00:00.000 | 7000 |
7 | 16 | 222 | 33333 | 2020-07-02 00:00:00.000 | 8000 |
8 | 17 | 222 | 33333 | 2020-08-09 00:00:00.000 | 4000 |
9 | NULL | NULL | NULL | NULL | NULL |
10 | NULL | NULL | NULL | NULL | NULL |
11 | NULL | NULL | NULL | NULL | NULL |
12 | NULL | NULL | NULL | NULL | NULL |
因为结果必须是这样的
UserId | UserAgreementId | MAX |
---|---|---|
222 | 33333 | 3 |
Table AgreementId 在行中有 SubsriptionDate,我需要计算最大不为空的期间 Amount
UserAgreementId | SubsriptionDate | Amount |
---|---|---|
33333 | 2020-02-02 00:00:00.000 | 40000 |
33333 | 2020-04-03 00:00:00.000 | 5000 |
33333 | 2020-06-04 00:00:00.000 | 7000 |
33333 | 2020-07-02 00:00:00.000 | 8000 |
33333 | 2020-08-09 00:00:00.000 | 4000 |
您要查询的内容对于查询来说太复杂了 - 运行 需要很长时间。
例如,我在下面整理了一份草稿,它几乎为每个用户提供了最长的持续时间,但它引用了相同的 table 5 次。
这几乎不可读,令人费解,并且会给服务器带来比你想要的更多的负担。
取而代之的是获取记录并用更合适的table编程语言处理它们。
SELECT t.user_id, MAX(t.monthsTotal - s.monthsTotal + 1) AS longestDuration
FROM (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name) as t
INNER JOIN (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name AS a
LEFT OUTER JOIN table_name AS preceeding
ON a.user_id = preceeding.user_id
AND YEAR(a.subscriptionDate) * 12 + a.month = YEAR(preceeding.subscriptionDate) * 12 + preceeding.month + 1
WHERE preceedingMonth.id IS NULL) AS s
ON t.user_id = s.user_id
AND t.monthsTotal >= s.monthsTotal
LEFT OUTER JOIN (SELECT a.*, YEAR(a.subscriptionDate) * 12 + a.month AS monthsTotal
FROM table_name AS a
LEFT OUTER JOIN table_name AS preceeding
ON a.user_id = preceeding.user_id
AND YEAR(a.subscriptionDate) * 12 + a.month = YEAR(preceeding.subscriptionDate) * 12 + preceeding.month + 1
WHERE preceedingMonth.id IS NULL) AS dummy
ON t.user_id = dummy.user_id
AND dummy.monthsTotal > s.monthsTotal
AND t.monthsTotal > dummy.monthsTotal
WHERE dummy.id IS NULL
GROUP BY t.user_id