2つのクエリが別々に正常に動作します。それらが似ているとすれば、私はそれらを1つの実行可能なクエリに統合したいと思います。 where節が似ているので簡単です。しかし、sum、count、min関数はすべて異なる行に適用され、途中で取り込まれます。類似のwhere句で異なる集計関数ターゲットを持つ2つのクエリを統合する
コンテキスト:
が- ユーザーは、スコア(またはレート)の場所とポイントが
- ユーザーAがユーザーBを参照し、ユーザBが最初
- ポイントは後に期限切れにスコアを送信したときに、紹介ポイントを得ることができます得ることができます特定の日付
- 目標は、スコアリングのためのユーザーとその合計点と、特定の場所(地域/国)を参照するための合計点を作成することです。
- 位置パラメータは、ハードv 'Massachusetts'、 'United States'、およびscoreDateTimeの有効期限のために使用され、両方のselectサブクエリーで残念ながら重複しています。
質問:
がどのようにクエリは、以下の制約を組み合わせるために再編成することができますか?ある日の後に特定の場所から得点のリストから始める方法がなければなりません。唯一の問題は、ユーザーBの最初のスコア日付を取得し、有効期限が過ぎてもユーザーAに参照ポイントを提供することだけです。
select scoring.userId, scoring.points + referring.points as leaderPoints
from (
select userId, sum(ratingPoints) as points
from scores s, locations l
where s.locationId = l.locationId and
l.locationArea = 'Massachusetts' and
l.locationCountry = 'United States' and
s.scoreDateTime > '2016-04-16 18:50:53.154' and
s.userId != 0
group by s.userId
) as scoring
join (
select u1.userId, count(*) * 20 as points
from users u0
join users u1 on u0.userId = u1.userId
join users u2 on u2.referredByEmail = u1.emailAddress
join scores s on u2.userId = s.userId
join locations l on s.locationId = l.locationId
where l.locationArea = 'Massachusetts' and
l.locationCountry = 'United States' and
scoreDateTime = (
select min(scoreDateTime)
from scores
where userId = u2.userId
) and
scoreDateTime >= '2016-04-16 18:50:53.154'
group by u1.userId
) as referring on scoring.userId = referring.userId
order by leaderPoints desc
limit 10;
私はMySQLを使用していますが、明らかにCROSS APPLYは適用されないようです。 locationArea、locationCountry、およびscoreDateTimeの制約が重複しているので、それらを統合することを望んでいました。私がそのスコアのリストの外に到達する唯一の時間は、参照されたユーザーの最初のスコアの日時を得るためのmin(scoreDateTime)です。その日付がカットオフ日付の後にある場合にのみ、紹介ボーナスポイントを授与したいと思います。 – jmelvin
@jmelvin Cross Applyを削除する答えを修正しました。 –