This is my hive query
INSERT OVERWRITE TABLE bts_monthly_points_liability_rollforward
SELECT currMonth.businessEventType,
prevMonth.totalFaceValue,
prevMonth.totalAccountingValue,
currMonth.earnedFaceValue,
currMonth.earnedAccountingValue,
currMonth.expiredFaceValue,
currMonth.expiredAccountingValue,
currMonth.earnedPointsReturnFaceValue,
currMonth.earnedPointsReturnAccountingValue,
currMonth.spendPointsFaceValue,
currMonth.spendPointsAccountingValue,
currMonth.spendPointsReturnFaceValue,
currMonth.spendPointsReturnAccountingValue,
currMonth.adjustmentFaceValue,
currMonth.adjustmentAccountingValue,
currMonth.totalFaceValue,
currMonth.totalAccountingValue
FROM
(
SELECT business_event_type AS businessEventType,
SUM(earned_face_value) AS earnedFaceValue,
SUM(earned_accounting_value) AS earnedAccountingValue,
SUM(expired_face_value) AS expiredFaceValue,
SUM(expired_accounting_value) AS expiredAccountingValue,
SUM(earned_return_face_value) AS earnedPointsReturnFaceValue,
SUM(earned_return_accounting_value) AS earnedPointsReturnAccountingValue,
SUM(spend_face_value) AS spendPointsFaceValue,
SUM(spend_accounting_value) AS spendPointsAccountingValue,
SUM(spend_return_face_value) AS spendPointsReturnFaceValue,
SUM(spend_return_accounting_value) spendPointsReturnAccountingValue,
CAST(0 AS BIGINT) AS adjustmentFaceValue,
CAST(0 AS BIGINT) AS adjustmentAccountingValue,
(SUM(earned_face_value)+SUM(expired_face_value)+SUM(earned_return_face_value)+SUM(spend_face_value)+SUM(spend_return_face_value)) AS totalFaceValue,
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(currMonth.spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
WHERE unix_timestamp(accounting_date,'yyyy-MM-dd') >= unix_timestamp("${startDate}",'yyyy-MM-dd') AND unix_timestamp(accounting_date,'yyyy-MM-dd') < unix_timestamp("${endDate}",'yyyy-MM-dd')
GROUP BY business_event_type
)currMonth
JOIN
(
SELECT business_event_type AS businessEventType,
(SUM(earned_face_value)+SUM(expired_face_value)+SUM(earned_return_face_value)+SUM(spend_face_value)+SUM(spend_return_face_value)) AS totalFaceValue,
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
WHERE unix_timestamp(accounting_date,'yyyy-MM-dd') >= unix_timestamp("${previousMonthStartDate}",'yyyy-MM-dd') AND unix_timestamp(accounting_date,'yyyy-MM-dd') < unix_timestamp("${startDate}",'yyyy-MM-dd')
GROUP BY business_event_type
)prevMonth
ON prevMonth.businessEventType = currMonth.businessEventType;
Error that I am receiving after running this query:
SemanticException [Error 10004]: Line 38:129 Invalid table alias or column reference 'currMonth': (possible column names are: business_event_type, accounting_date, earned_face_value, earned_accounting_value, expired_face_value, expired_accounting_value, earned_return_face_value, earned_return_accounting_value, spend_face_value, spend_accounting_value, spend_return_face_value, spend_return_accounting_value)
Command exiting with ret '255'
The problem is the line 35 of your query. Here's how the query works:
SELECT ...
FROM (
SELECT business_event_type AS businessEventType,
...
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(currMonth.spend_return_accounting_value)) AS totalAccountingValue
FROM ${pointsApplicationName}_points_balance
...
)currMonth
JOIN (...)prevMonth
ON prevMonth.businessEventType = currMonth.businessEventType;
Here you can see that you are using currMonth alias inside of the subquery that aliased as currMonth. The alias does not exist in this context, this is why you get an error. It should be like this:
(SUM(earned_accounting_value)+SUM(expired_accounting_value)+SUM(earned_return_accounting_value)+SUM(spend_accounting_value)+SUM(spend_return_accounting_value)) AS totalAccountingValue
Related
I came across this -
Using ALIAS column in WHERE recently and I understand why the WHERE clause is giving me an error but I can't seem to figure out any other way to write my nested query. The SELECT sub-query runs completely fine by itself.
The error is as follows:
Error Code: 1054. Unknown column 'actual_start_time' in 'IN/ALL/ANY subquery'
Table Structure:
Students: student_id student_name login_time logout_time
Tests: test_id test_start_time test_end_time
TestStats: test_id student_id test_duration
UPDATE test_stats
SET test_duration = datediff(hour, actual_start_time - actual_end_time)
WHERE (actual_start_time, actual_end_time)
IN (
SELECT
CASE
WHEN (s.login_time > t.test_start_time) THEN s.login_time
ELSE t.test_start_time
END AS actual_start_time,
CASE
WHEN (s.logout_time < t.test_end_time) THEN s.logout_time
ELSE t.test_end_time
END AS actual_end_time
FROM tests AS t, students AS s, test_stats AS ts
WHERE t.test_id = ts.test_id and s.student_id = ts.student_id);
This appears to be what you want:
UPDATE test_stats ts JOIN
tests t
ON t.test_id = ts.test_id JOIN
students s
ON s.student_id = ts.student_id
SET test_duration = timestampdiff(hour,
GREATEST(s.login_time, t.test_start_time),
LEAST(s.logout_time, t.test_end_time)
);
i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);
I want to get json out of my PostgreSQL database running version:
PostgreSQL 9.3.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
The data I am trying to get is like:
{
"a" : "value",
"b" : {
"c" : "some_vaue here"
}
}
I am getting value from one table and some_value from some other table using join.
How can I achieve that?
Here is something similar, but I'm getting an error:
QUERY="
SELECT row_to_json(o) FROM (
SELECT oltl.id::text as ordinal,
oltl.nid as code,
oltl.description as description,
SELECT row_to_json(j)
FROM ( SELECT cilantag.tag as code ) AS j
from olt_languages oltl
inner join ci_language_tags cilantag
on oltl.ci_language_tag_id=cilantag.id
) AS o";
The error I am getting is
ERROR: syntax error at or near "SELECT"
LINE 1: ...oltl.nid as code,oltl.description as description, SELECT row...
^ `
enter code here
A subquery in a select list has to be enclosed in brackets, try:
SELECT row_to_json(o)
FROM (
SELECT
oltl.id::text AS ordinal,
oltl.nid AS code,
oltl.description AS description,
( -- added
SELECT row_to_json(j)
FROM (
SELECT cilantag.tag AS code
) AS j
) AS tags -- + alias
FROM olt_languages oltl
INNER JOIN ci_language_tags cilantag
ON oltl.ci_language_tag_id=cilantag.id
) AS o
I have got an error "ERROR: subquery must return only one column " when I am runing this query:
INSERT INTO details (id, object_id, detail)
(
SELECT
CASE
WHEN (SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id))
THEN
concat(SUBSTRING(main_base.id, '(\d+.\d+.)'), n.counted :: TEXT, 'A')
ELSE
concat( SUBSTRING (main_base.id, '(\d+.\d+.)'), n.counted :: TEXT)
END AS id,
main_base.object_id,
main_base.details
FROM main_base
CROSS JOIN LATERAL
generate_series(1, COALESCE ((string_to_array(main_base.id, '-')) [2] :: INT, 1)) AS n (counted)
WHERE main_base.id LIKE '%-%' AND NOT main_base.details ~ '^\.\d+|\(\.\d+\)'
);
I have not clue what is wrong. I've read some topic that people had the same problem but still dont know how to fix it.
I think the problem is that:
SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)
Can return more than one row, so causes problems in the WHEN statement. It can return more than one row, as the subquery will return 1 every time the condition is met.
If you want to trigger the case statement based on when there exists some records in this set, could you use:
(SELECT COUNT(*) FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)) > 1
When I execute this query...
UPDATE tbl a,
(SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl) b
SET tbl.`url` = b.`url`
...I see this error:
Error Code: 1525
Incorrect XML value: 'parse error at line 57 pos 195: '</div>' unexpected (END-OF-INPUT wanted)'
But when I execute this query...
SELECT id, EXTRACTVALUE(content, '//a[contains(text(), "View")]/#href') AS url FROM tbl
...the query succeeds.
Why does the UPDATE query fail where the standalone SELECT query succeeds?