Я хочу создать запрос, который создает вычисляемое поле ANSWER_SCORE, в которое затем упорядочивает результаты запроса. Я также хочу выбрать два других поля: QUESTION_ID и ANSWER_ID.
Я хочу выбрать только разные QUESTION_ID. Однако я также хочу выбрать некоторую другую информацию, связанную с этим QUESTION_ID (которая будет общей для всех запросов, которые вернули этот QUESTION_ID. Я не уверен, как это сделать.
Вот мой запрос:
SELECT QUESTION_ID,
ANSWER_ID,
sum(ANSWER_SCORE) AS ANSWER_SCORE_SUMMED
FROM(SELECT
nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID,
cr.COMMUNICATIONS_ID AS ANSWER_ID,
case when cr.CONSUMER_ID= nc.SENDER_CONSUMER_ID then 2*(1 - EXP(-0.5 * (cal.REAL_TIPS_AMOUNT / ATV.AVG_TIPS) + .15))*(24/(((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(cal.LAST_MOD_TIME)+3600)/3600)))*(IFNULL(ces.EXPERT_SCORE,2) * IFNULL(cirm.CONSUMER_RATING,2) + (12.5 * IFNULL((bit_count((conv(scr_tip.survey_string, 2, 10) & conv(scr_view.survey_string, 2, 10)) | ((0xFFFFFFFF>>(32-6))&~(conv(scr_tip.survey_string, 2, 10)|conv(scr_view.survey_string, 2, 10))))/6),.3)))
else (1 - EXP(-0.5 * (cal.REAL_TIPS_AMOUNT / ATV.AVG_TIPS) + .15))*(24/(((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(cal.LAST_MOD_TIME)+3600)/3600)))*(IFNULL(ces.EXPERT_SCORE,2) * IFNULL(cirm.CONSUMER_RATING,2) + (12.5 * IFNULL((bit_count((conv(scr_tip.survey_string, 2, 10) & conv(scr_view.survey_string, 2, 10)) | ((0xFFFFFFFF>>(32-6))&~(conv(scr_tip.survey_string, 2, 10)|conv(scr_view.survey_string, 2, 10))))/6),.3)))
end as ANSWER_SCORE
FROM (SELECT 232 AS CONSUMER_ID,
ACTION_LOG_ID,
COMMUNICATIONS_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4) AS cr
JOIN network_communications AS nc
ON cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
JOIN network_communications AS nc1
ON nc.PARENT_COMMUNICATIONS_ID=nc1.COMMUNICATIONS_ID
JOIN (SELECT ACTION_LOG_ID,
LAST_MOD_TIME,
CONSUMER_ID,
SENDER_CONSUMER_ID,
sum(tips_amount) AS real_tips_amount
FROM consumer_action_log
WHERE COMM_TYPE_ID=4
GROUP BY COMMUNICATIONS_ID, SENDER_CONSUMER_ID) AS cal
ON cr.ACTION_LOG_ID=cal.ACTION_LOG_ID
JOIN communication_interest_mapping AS cim
ON nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
LEFT JOIN consumer_interest_rating_mapping AS cirm
ON cr.CONSUMER_ID=cirm.CONSUMER_ID
AND cim.CONSUMER_INTEREST_EXPERT_ID=cirm.CONSUMER_INTEREST_ID
JOIN network_communications_message AS ncm
ON nc.PARENT_COMMUNICATIONS_ID=ncm.COMMUNICATIONS_ID
LEFT JOIN consumer_expert_score AS ces
ON nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
AND cim.CONSUMER_INTEREST_EXPERT_ID=ces.CONSUMER_EXPERT_ID
LEFT JOIN surveycustomerrelation AS scr_tip
ON cal.SENDER_CONSUMER_ID=scr_tip.Cust_ID
LEFT JOIN surveycustomerrelation AS scr_view
ON cr.CONSUMER_ID=scr_view.CUST_ID
STRAIGHT_JOIN
(
SELECT AVG(cal.TIPS_AMOUNT) AS AVG_TIPS,
cal.SENDER_CONSUMER_ID AS CONSUMER_ID
FROM CONSUMER_ACTION_LOG AS cal
WHERE COMM_TYPE_ID=4
GROUP BY cal.SENDER_CONSUMER_ID
) AS ATV
ON cal.SENDER_CONSUMER_ID=ATV.CONSUMER_ID
) AS ASM
GROUP BY ANSWER_ID
ORDER BY ANSWER_SCORE_SUMMED DESC
LIMIT 0,20;