|
Где туплю в запросе ? SUM(CASE ----==
|
|||
---|---|---|---|
#18+
Есть таблица journal вида type_operation pl_monat amount 1 5 10 2 1 50 3 2 5 2 1 15 3 5 20 По идее должно выводить type_operation 1 2 5 1 10 2 65 3 5 20 Не выводит ((((; PHP выдаёт ошибку: SELECT 'journal.type_operation', SUM(CASE WHEN journal.pl_monat = '1' THEN journal.amount ELSE 0 END) AS 1, SUM(CASE WHEN journal.pl_monat = '5' THEN journal.amount ELSE 0 END) AS 5, SUM(CASE WHEN journal.pl_monat = '2' THEN journal.amount ELSE 0 END) AS 2 FROM journal GROUP BY journal.type_operation Warning: pg_query(): Query failed: ERROR: syntax error at or near "1" LINE 1: ...pl_monat = '1' THEN journal.amount ELSE 0 END) AS 1, SUM(CAS... ^ in C:\OSPanel\domains\localhost\charts\crosstab.php on line 51 Warning: pg_fetch_array() expects parameter 1 to be resource, boolean given in C:\OSPanel\domains\localhost\charts\crosstab.php on line 55 Образец кода: function getDataString(){ global $db; $query_month='SELECT DISTINCT pl_monat FROM journal'; $result=pg_query($db, $query_month); //$query="SELECT type_operation, sum FROM journal"; //$res=pg_query($db, $query); $sql = "SELECT journal.type_operation"; $headings = array(); while ($row = pg_fetch_array($result)) { $sql .= ", SUM(CASE WHEN journal.pl_monat = '{$row['pl_monat']}' THEN journal.amount ELSE 0 END) AS {$row['pl_monat']}"; $headings[] = $row['pl_monat']; } $sql .= ' FROM journal GROUP BY journal.type_operation'; echo $sql; $cross = pg_query($sql); return $data; ... |
|||
:
Нравится:
Не нравится:
|
|||
03.05.2019, 20:42 |
|
Где туплю в запросе ? SUM(CASE ----==
|
|||
---|---|---|---|
#18+
dmbx, почему вы решили, что число - это допустимый алиас для поля без использования кавычек? https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). ... |
|||
:
Нравится:
Не нравится:
|
|||
03.05.2019, 20:52 |
|
Где туплю в запросе ? SUM(CASE ----==
|
|||
---|---|---|---|
#18+
Melkij , спасибо! Переправил на: SELECT 'journal.type_operation', SUM(CASE WHEN journal.pl_monat = '1' THEN journal.amount ELSE 0 END) AS field_1, SUM(CASE WHEN journal.pl_monat = '5' THEN journal.amount ELSE 0 END) AS field_5, SUM(CASE WHEN journal.pl_monat = '2' THEN journal.amount ELSE 0 END) AS field_2 FROM journal GROUP BY journal.type_operation И всё застреляло :)) ... |
|||
:
Нравится:
Не нравится:
|
|||
03.05.2019, 21:30 |
|
|
start [/forum/topic.php?fid=53&gotonew=1&tid=1995218]: |
0ms |
get settings: |
11ms |
get forum list: |
12ms |
check forum access: |
2ms |
check topic access: |
2ms |
track hit: |
53ms |
get topic data: |
10ms |
get first new msg: |
7ms |
get forum data: |
2ms |
get page messages: |
167ms |
get tp. blocked users: |
1ms |
others: | 275ms |
total: | 542ms |
0 / 0 |