I have the following query:
SELECT o.outcode AS lead_postcode, v.outcode AS venue_postcode, 6 * o.lat AS distance
FROM venue_postcodes v, uk_postcodes o
WHERE o.outcode = 'CF3'
GROUP BY v.outcode
HAVING SUM(distance)>100
ORDER BY distance
This stopped working when I added the part GROUP BY v.outcode HAVING SUM(distance)>100
It says Server was unable to process request. ---> Invalid column name 'distance'.
Any ideas why?
distance is a column alias and you can't refer to a column alias in a HAVING clause. But you can use aliases in an ORDER BY.
Try changing to:
HAVING SUM(6 * o.lat)>100
ORDER BY distance
The "alias" distance only just defined within the query as "6*o.lat" can not yet be used within the query but only afterwards.
alternative solution is
SELECT i.*
FROM (
SELECT o.outcode AS lead_postcode, v.outcode AS venue_postcode, 6 * o.lat AS distance
FROM venue_postcodes v, uk_postcodes o
WHERE o.outcode = 'CF3'
) i
GROUP BY i.outcode
HAVING SUM(i.distance)>100 ORDER BY i.distance
Use ORDER BY 6 * o.lat. You cannot use the clause AS for an ORDER BY
I believe you need to use SUM(6* o.lat), because not every database server can use aliased columns in having clause (It has to do with query planning, parsing etc.). Depends on what DB you use.
Related
I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:
SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC
I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?
Try moving the subquery to a join, something like this:
SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
FROM bom x JOIN work_orders b
ON x.assembly = b.assembly
WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
FROM inventory y WHERE
y.part_number = x.part_number)
GROUP BY x.part_number) n
ON a.part_number = n.part_number
ORDER BY a.assembly ASC
Or create a temporary cursor with the subquery and then use it to join the main table.
Hope this helps.
Luis
select top(1)
Tb_Customer.ID
from
Tb_Customer
inner join
Tb_Agency_Eshterak on Tb_Customer.ID = Tb_Agency_Eshterak.CustomerID
where
Tb_Agency_Eshterak.TypeE = 2
order by
((convert(decimal(10), Tb_Customer.Lat) - '36.828381258846065')
(convert(decimal(10), Tb_Customer.Lat) - '36.828381258846065')) +
((convert(decimal(10), Tb_Customer.Lng) - '54.454983147717144')
(convert(decimal(10), Tb_Customer.Lng) - '54.454983147717144')) ASC
Does this query have any problems?
I Cant Excute it In Sql Server
This Is Emergency Thanks
You are missing paranthesis). You are missing operators in your ORDER BY clause. I have put multiplication operator, to make the query compile. Also, it is not recommended to have this big expression in the ORDER BY clause.
I would suggest you to have a persisted computed column and use that column in your ORDER BY clause.
select top(1) Tb_Customer.ID
from Tb_Customer
inner join Tb_Agency_Eshterak
on Tb_Customer.ID=Tb_Agency_Eshterak.CustomerID
where Tb_Agency_Eshterak.TypeE=2
ORDER BY
((CONVERT(decimal(10),Tb_Customer.Lat)-'36.828381258846065') * (CONVERT(decimal(10),Tb_Customer.Lat)-'36.828381258846065')) + ((CONVERT(decimal(10),Tb_Customer.Lng) - '54.454983147717144') * (CONVERT(decimal(10),Tb_Customer.Lng) - '54.4549831477171'))
I am generating records with sum aggregate function and subquery, but the alias is not work there in inner query.
my query is
select UPP.item_total,
(select sum(INN.item_value_afs) total_item_value_afs from
(select distinct INN.reg_no,INN.tpt_cuo_nam,INN.item_total,INN.item_value_afs
from sigtasad.customs_import_data INN where INN.reg_no=UPP.reg_no and INN.tpt_cuo_nam=UPP.tpt_cuo_nam)) total_item_value,
sum(UPP.code_tax_amount), UPP.cmp_nam from SIGTASAD.CUSTOMS_IMPORT_DATA UPP where
UPP.reg_no='38699' and UPP.company_tin='9003247336' group by
UPP.reg_no,UPP.tpt_cuo_nam,UPP.cmp_nam,UPP.item_total ;
this query generate this error :
ORA-00904: "UPP"."TPT_CUO_NAM": invalid identifier
I want like this result!!!
Your query has numerous errors and bad habits. For instance:
You qualify a column name with an undefined table alias.
You are aggregating by columns not in the select.
You are using sum() on a subquery that has sum().
Based on the picture that you show, you probably want something like this:
select upp.item_total,
sum(item_value_afs) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Or perhaps:
select upp.item_total,
sum(sum(item_value_afs)) over (partition by upp.cmp_nam, upp.item_total) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Your innermost subquery
(select distinct nn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)
references a table that is not joined (upp). It also does not have an alias but that problem would come later. Please note, that there also seems to be a type nn.reg_no instead of inn.reg_no
The structure of the tables is not displayed here but fixing the problem would mean something along the lines of:
(select distinct inn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn, SIGTASAD.CUSTOMS_IMPORT_DATA upp
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)
I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).
As a newbie to Postgresql (I'm moving over because I'm moving my site to heroku who only support it, I'm having to refactor some of my queries and code. Here's a problem that I can't quite understand the problem with:
PGError: ERROR: column "l_user_id" does not exist
LINE 1: ...t_id where l.user_id = 8 order by l2.geopoint_id, l_user_id ...
^
...query:
select distinct
l2.*,
l.user_id as l_user_id,
l.geopoint_id as l_geopoint_id
from locations l
left join locations l2 on l.geopoint_id = l2.geopoint_id
where l.user_id = 8
order by l2.geopoint_id, l_user_id = l2.user_id desc
clause "l.user_id as l_user_id, l.geopoint_id as l_geopoint_id" was added because apparently postgres doesn't like order clauses with fields not selected. But the error I now get makes it look like I'm also not getting aliasing. Anybody with postgres experience see the problem?
I'm likely to have a bunch of these problems -- the queries worked fine in mySql...
In PostgreSQL you can not use expression with an alias in order by. Only plain aliases work there. Your query should look like this:
select distinct
l2.*,
l.user_id as l_user_id,
l.geopoint_id as l_geopoint_id
from locations l
left join locations l2 on l.geopoint_id = l2.geopoint_id
where l.user_id = 8
order by l2.geopoint_id, l.user_id = l2.user_id desc;
I assume you mean that l2.user_id=l.user_id ought to go first.
This is relevant message on PostgreSQL-general mailing list. The following is in the documentation of ORDER BY clause:
Each expression can be the name or
ordinal number of an output
column (SELECT list item), or it
can be an arbitrary expression formed
from input-column values.
So no aliases when expression used.
I ran into this same problem using functions from fuzzystrmatch - particularly the levenshtein function. I needed to both sort by the string distance, and filter results by the string distance. I was originally trying:
SELECT thing.*,
levenshtein(thing.name, '%s') AS dist
FROM thing
WHERE dist < character_length(thing.name)/2
ORDER BY dist
But, of course, I got the error "column"dist" does not exist" from the WHERE clause. I tried this and it worked:
SELECT thing.*,
(levenshtein(thing.name, '%s')) AS dist
FROM thing
ORDER BY dist
But I needed to have that qualification in the WHERE clause. Someone else in this question said that the WHERE clause is evaluated before ORDER BY, thus the column was non-existent when it evaluated the WHERE clause. Going by that advice, I figured out that a nested SELECT statement does the trick:
SELECT * FROM
(SELECT thing.*,
(levenshtein(thing.name, '%s')) AS dist
FROM thing
ORDER BY dist
) items
WHERE dist < (character_length(items.name)/2)
Note that the "items" table alias is required and the dist column alias is accessible in the outer SELECT because it's unique in the statement. It's a little bit funky and I'm surprised that it has to be this way in PG - but it doesn't seem to take a performance hit so I'm satisfied.
You have:
order by l2.geopoint_id, l_user_id = l2.user_id desc
in your query. That's illegal syntax. Remove the = l2.user_id part (move it to where if that's one of the join conditions) and it should work.
Update Below select (with = l2.user_id removed) should work just fine. I've tested it (with different table / column names, obviously) on Postgres 8.3
select distinct
l2.*,
l.user_id as l_user_id,
l.geopoint_id as l_geopoint_id
from locations l
left join locations l2 on l.geopoint_id = l2.geopoint_id
where l.user_id = 8
order by l2.geopoint_id, l_user_id desc
"was added because apparently postgres doesn't like order clauses with fields not selected"
"As far as order by goes - yes, PostgresQL (and many other databases) does not allow ordering by columns that are not listed in select clause."
Just plain untrue.
=> SELECT id FROM t1 ORDER BY owner LIMIT 5;
id
30
10
20
50
40
(5 rows)