Error relation does not exist SQL WITH on POSTGRESQL - sql

I get ERROR: relation "case_avg" does not exist in my sql request
with case_avg AS
(select
avg(vv.total_vcpu) as avgvcpu,
avg(vv.total_vram) as avgvram,
avg(vv.total_hdd) as avghdd,
pod,
report_date
from common.vmware_vm vv
group by pod,report_date)
select
(ca.avgvcpu-v.total_vcpu) as vcpu,
(ca.avgvram-v.total_vram) as vram,
(ca.avghdd-v.total_hdd) as hdd,
Concat(lower(v.vdc),lower(v.org)) as org_vdc
from
common.vmware_vm v
inner join case_avg ca on
ca.pod = v.pod and ca.report_date = v.report_date
group by org_vdc
How i can resolve this?

Related

How to get queries by a user group

I'd like to get all queries run by users in a specific user group for a Redshift database. Here's the query I'm running:
select
q.*,
u.usename,
swq.total_queue_time / 1000000 as queue_time
from stl_query q
inner join pg_user u on q.userid = u.usesysid
inner join pg_group g on u.usesysid = ANY (g.grolist)
inner join stl_wlm_query swq on q.query = swq.query
where q.userid <> 1
AND database = 'mydb'
AND g.groname = 'ops'
order by q.starttime desc;
However, I'm getting Column "g.grolist" has unsupported type "integer[]". I've tried other forms of the same query (e.g., putting the ANY condition in WHERE), but I keep getting the same error. How can I check whether a user ID occurs in the list of user IDs in pg_group?
I found this redshift guide having array types included in the unsupported list of PostgresSQL data types, and that's causing your SQL error of column has unsupported type.
Redshift is not exactly Postgres & thus doesn't have full feature support for all Postgres data types and functions. So refering redshift guide for any errors/differences in behaviour you encounter will be the best bet.
Find out more differences here: Amazon Redshift and PostgreSQL
For unsupported data types, it states:
If a query attempts to use an unsupported data type, including explicit or implicit casts, it will return an error.
However redshift does support some array & json-array function(s) which you can use to play with array column type. Like below query will output your desired result using array_to_string.
select
q.*,
u.usename,
swq.total_queue_time / 1000000 as queue_time
from stl_query q
inner join pg_user u on q.userid = u.usesysid
inner join pg_group g on array_to_string(g.grolist,',') like '%' || u.usesysid || '%'
inner join stl_wlm_query swq on q.query = swq.query
where q.userid <> 1
AND database = 'mydb'
AND g.groname = 'ops'
order by q.starttime desc;

hive query with between in join working in llap but not in hive

Below range query with join working fine in llap, but not in Hiveserver2/Hive.CLI.
Please suggest how to work with range join query in Hive.
Hive version : 1.2.1.2.6
HDP version : 2.6.0.3
Query:
select * from datahub.cgs_tmp_gre gre
INNER JOIN datahub.cgs_tmp_cgsrxclm_ev_ba ba
ON gre.guar_key = ba.guar_key and gre.serviced_dte = ba.serviced_dte
AND gre.ts between ba.obsv_start_ts and ba.obsv_stop_ts
AND gre.ts between ba.obsv_start_ts and ba.obsv_stop_ts
AND gre.phcy_claim_id=2;
Below is the error thrown when we run it in hive CLI or hive server 2:
Error: Error while compiling statement: FAILED: SemanticException Line 0:-1 Both left and right aliases encountered in JOIN 'obsv_stop_ts' (state=42000,code=40000)
This error is because you have the same join condition twice. between resolves to >= and <= and inequalities aren't supported in join conditions (before version 2.2.0). Move the condition to where clause and it should work.
select * --specify required columns rather than *
from datahub.cgs_tmp_gre gre
INNER JOIN datahub.cgs_tmp_cgsrxclm_ev_ba ba
ON gre.guar_key = ba.guar_key and gre.serviced_dte = ba.serviced_dte
WHERE gre.ts >= ba.obsv_start_ts and gre.ts <= ba.obsv_stop_ts --replaced between with >= and <=
AND gre.phcy_claim_id=2;

Error SQL 979 with the group by

I have a problem with my Java EE/hibernate application.
This query works in my unit test, but not in my application.
<named-query name="list.vacant.accessory">
<query>SELECT proty, COUNT(acc.id)
FROM ProductType proty, Accessory acc
LEFT JOIN acc.productHistoryList phl
WHERE phl.status LIKE 'En stock ATOS'
AND proty.id = acc.productType.id
AND phl.statusDate = (SELECT MAX(statusDate)
FROM ProductHistory ph
WHERE ph.product=phl.product)
GROUP BY proty
</query>
</named-query>
I have this error : SQL Error: 979, SQLState: 42000
ORA-00979: not an expression GROUP BY
Have you any idea ?
Thanks.
I don't know hibernate mapping. But purely from Query stand point:
SELECT proty, COUNT(acc.id)
FROM ProductType proty, Accessory acc
LEFT JOIN acc.productHistoryList phl
Here proty is referring to table / Entity and you are grouping by table name? Shouldn't there be a column name? proty.column?

sql joining as alias does not working

i am new to sql joins.. i have project that is 5 year old . Now i have to deploy it some new server .so deploy it to other server there i am facing some sql problem. here is the qyery .
SELECT DISTINCT d.*,DATE_FORMAT(d.downloads_updated, '%c/%d/%Y') AS updated,DATE_FORMAT(d.downloads_created, '%c/%d/%Y') AS created, s2.strings_english as title, s2.strings_english as description
FROM strings s, downloads d,
products_has_downloads pd
inner JOIN strings s2 ON d.downloads_description = s.strings_id WHERE d.downloads_id = pd.downloads_id AND s.strings_id = d.downloads_title AND d.downloads_status = 'Live' AND d.downloads_level = 'Public'
ORDER BY d.downloads_updated DESC LIMIT 5
i am getting this error
1054 - Unknown column 'd.downloads_description' in 'on clause'
i have not written this sql query.it is working fine on old server .
I am new to join and database .please can any one help me .
You are mixing both implicit and explicit join syntax which will not work.
You have the following implicit syntax where the tables are joined by commas:
FROM strings s, downloads d, products_has_downloads pd
The JOIN syntax has a higher precedence to the comma syntax so the alias for downloads is not available in the ON clause.
Try using all of the same syntax. I changed your query to use only explicit JOIN syntax:
SELECT DISTINCT d.*,
DATE_FORMAT(d.downloads_updated, '%c/%d/%Y') AS updated,
DATE_FORMAT(d.downloads_created, '%c/%d/%Y') AS created,
s2.strings_english as title,
s2.strings_english as description
FROM strings s
INNER JOIN downloads d
ON d.downloads_description = s.strings_id
INNER JOIN products_has_downloads pd
on d.downloads_id = pd.downloads_id
inner JOIN strings s2
on s2.strings_id = d.downloads_title
WHERE d.downloads_status = 'Live'
AND d.downloads_level = 'Public'
ORDER BY d.downloads_updated DESC
LIMIT 5

Doctrine query builder error on join

How do I write the following with doctrine query builder?
SELECT
registry.id ,
registry.couple_id,
registry.gift_id,
Sum(purchase.amount) as totalContribution,
gift.title,
gift.price
FROM
gift,
registry
LEFT JOIN
purchase ON purchase.registry_id = registry.id
WHERE
registry.gift_id = gift.id
AND registry.couple_id = 1
GROUP BY
registry.couple_id,
registry.gift_id
I tried:
$qb = $this->createQueryBuilder('g') //gift
->from('\BBB\GiftBundle\Entity\Registry', 'reg')
->select('g.id , g.title, g.description, g.price, g.imageMedium')
->addSelect('SUM(p.amount) as totalContribute')
->leftJoin('\BBB\GiftBundle\Entity\Purchase', 'p', 'ON','reg.id = p.registry')
->where('reg.gift = g.id')
->AndWhere('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->groupBy('reg.couple')
->groupBy('reg.gift')
->setParameter('coupleID', $coupleID);
But it gives me following error:
[Semantical Error] line 0, col 178 near 'p ON reg.id =': Error: Identification Variable BBB\GiftBundle\Entity\Purchase used in join path expression but was not defined before.
DQL can only join tables relating to the current entity (that being Registry in this DQL). Your Registry entity will need to have a declared OneToMany relation.
Assuming that this relation was called purchases you would join like so:
->leftJoin('reg.purchases', 'p')