SQL - сумма полей по псевдониму - PullRequest
1 голос
/ 21 июля 2010

Я пытаюсь найти сумму всех перечисленных полей, но, учитывая, что они являются подзапросами ... Мне нужно использовать псевдонимы. Если я использую перечисленные псевдонимы, я получаю неизвестные ошибки столбца / поля ... и если я пытаюсь использовать сумму (баллы), используя группу, я получаю недопустимое использование ошибки предложения группы.

SELECT DISTINCT pr.competitorID AS compID, pr.age, CONCAT(pr.firstname, ' ', pr.lastname)AS name
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=1 AND participation_reports.competitorID=compID LIMIT 1) AS '100m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=25 AND participation_reports.competitorID=compID LIMIT 1) AS '200m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=37 AND participation_reports.competitorID=compID LIMIT 1) AS '400m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=49 AND participation_reports.competitorID=compID LIMIT 1) AS '800m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=61 AND participation_reports.competitorID=compID LIMIT 1) AS '1500m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=67 AND participation_reports.competitorID=compID LIMIT 1) AS '3000m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=69 AND participation_reports.competitorID=compID LIMIT 1) AS 'Javelin'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=81 AND participation_reports.competitorID=compID LIMIT 1) AS 'Shot Put'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=93 AND participation_reports.competitorID=compID LIMIT 1) AS 'Discus'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=105 AND participation_reports.competitorID=compID LIMIT 1) AS 'High Jump'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=117 AND participation_reports.competitorID=compID LIMIT 1) AS 'Long Jump'
, (SELECT SUM(participation_reports.points) FROM participation_reports WHERE participation_reports.competitorID=compID) AS total
FROM participation_reports AS pr
JOIN event_data on event_data.eventID=pr.heatEventID
 WHERE event_data.minAge <= pr.age AND event_data.maxAge >= pr.age AND sex = 'F'
 AND total > 0
 LIMIT 30

Спасибо за любые идеи.

Ответы [ 3 ]

3 голосов
/ 21 июля 2010

Дайте этому шанс:

SELECT 
    pr.competitorID AS compID
    , pr.age 
    , CONCAT(pr.firstname, ' ', pr.lastname) AS name
    , SUM(CASE 
        WHEN pr.heatEventID=1 
        THEN pr.points 
        ELSE 0 END) AS "100m"
    , SUM(CASE 
        WHEN pr.heatEventID=25 
        THEN pr.points 
        ELSE 0 END) AS "200m"
    ...
    , SUM(CASE 
        WHEN pr.heatEventID IN (1,25,...)
        THEN pr.points
        ELSE 0 END) AS total
FROM 
    participation_reports pr
    JOIN event_data 
    ON event_data.eventID = pr.heatEventID
WHERE 
    ...
GROUP BY 
    pr.competitorID
    , pr.age
    , CONCAT(pr.firstname, ' ', pr.lastname)
1 голос
/ 21 июля 2010

Оберните этот запрос как производную таблицу и затем выберите суммы.

0 голосов
/ 21 июля 2010

Я не совсем понимаю, что вам нужно, но если вы хотите получить сумму всех этих столбцов с псевдонимами, может быть, ваша проблема не в том, что они не являются допустимыми именами столбцов?Как насчет ...

SELECT DISTINCT pr.competitorID AS compID, pr.age, CONCAT(pr.firstname, ' ', pr.lastname)AS name
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=1 AND participation_reports.competitorID=compID LIMIT 1) AS c_100m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=25 AND participation_reports.competitorID=compID LIMIT 1) AS c_200m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=37 AND participation_reports.competitorID=compID LIMIT 1) AS c_400m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=49 AND participation_reports.competitorID=compID LIMIT 1) AS c_800m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=61 AND participation_reports.competitorID=compID LIMIT 1) AS c_1500m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=67 AND participation_reports.competitorID=compID LIMIT 1) AS c_3000m
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=69 AND participation_reports.competitorID=compID LIMIT 1) AS Javelin
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=81 AND participation_reports.competitorID=compID LIMIT 1) AS Shot_Put
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=93 AND participation_reports.competitorID=compID LIMIT 1) AS Discus
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=105 AND participation_reports.competitorID=compID LIMIT 1) AS High_Jump
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=117 AND participation_reports.competitorID=compID LIMIT 1) AS Long_Jump,
c_100m + c_200m + c_400m + c_800m + c_1500m + c_3000m + Javelin + Shot_Put + High_Jump + Long_Jump
, (SELECT SUM(participation_reports.points) FROM participation_reports WHERE participation_reports.competitorID=compID) AS total
FROM participation_reports AS pr
JOIN event_data on event_data.eventID=pr.heatEventID
 WHERE event_data.minAge <= pr.age AND event_data.maxAge >= pr.age AND sex = 'F'
 AND total > 0
 LIMIT 30

или я неправильно понял проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...