PostgreSQL check value from sum in where [duplicate] - sql

This question already has answers here:
Referring to a Column Alias in a WHERE Clause
(9 answers)
Closed 7 years ago.
I'm triyng to add a the done value to where clause, so I can show only results from todo, that are already done, but keep getting an error:
select "todo", SUM( "test"."count" ) as "done"
from B
Where "done" = "todo"
this is the error:
q_driver: [PGRES_FATAL_ERROR]ERRORE: the column "done" don't exist
but the column is displayed if I remove the where clause

You cannot use alias name in same select query where clause
select * from
(
select "todo", SUM( "test"."count" ) as "done"
from B
GROUP BY "todo"
)
Where "done" = "todo"

Related

Column doesn't exist using CASE statement in PosgreSQL [duplicate]

This question already has answers here:
delete "column does not exist"
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Closed 10 months ago.
I am trying to calculate the payments for each age range in PostgreSQL.
select
case
when age < 50 then "0-50"
else "50+"
end as age_range,
SUM(payment_amount) as sum_payment_amount
from
(
select
age,
payment_amount
from
"002_DM_clients" dc
left join user_payment_log upl
on
dc.client_id = upl.client_id
) query1
group by
age_range;
But I get the following error:
Error executing query:
SQL Error [42703]: ERROR: column "0-50" does not exist
I have no idea what the problem could be.

How can I update this column with an aggregate function? [duplicate]

This question already has answers here:
SQL update query using joins
(13 answers)
Closed 2 years ago.
I calculated a sum for OREB that is unique to every pair of GameId and TeamId
using this query,
Query Image.
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
Using the results of this query I want to update the null column in this table
Table called dimGames.
How can I write an update statement to accomplish this?
Adapting this answer, I think you want:
with agg as (
select DGAM.GameID, HomeTeamID, SUM(OREB) as OREB_Home
from dimGames as DGAM
join dimPerformance as DPERF on DGAM.GameID = DPERF.GAME_ID and DGAM.HomeTeamID = DPERF.TEAM_ID
group by DGAM.GameID, HomeTeamID
)
UPDATE dimGames dg
SET dg.OREB_home = agg.OREB_Home
FROM agg
WHERE dg.GameId = agg.GameId and dg.HomeTeamId = agg.HomeTeamId

Error: column does not exist when in fact it does? why? [duplicate]

This question already has answers here:
Column 'mary' does not exist
(2 answers)
delete "column does not exist"
(1 answer)
SQL domain ERROR: column does not exist, setting default
(3 answers)
Simple Postgresql Statement - column name does not exists
(2 answers)
Why does Postgres say column does not exist? [duplicate]
(1 answer)
Closed 3 years ago.
I have the following PostgreSQL query below. If I remove one line AND r.type = "long_form" it works fine. I'm not sure why PostgreSQL is not liking that line:
SELECT TRUNC(DATE_PART('day', CURRENT_DATE - r.created_at )/7) AS weeks_ago,
date(min(r.created_at)) AS "Date Start",
date(max(r.created_at)) AS "Date End",
count(*) as "Reviews in Cohort",
AVG(has_note::int) as "Reviews w 1 or more Notes Ratio"
FROM (SELECT r.id, r.created_at,
( MAX(rn.note) IS NOT NULL ) as has_note
FROM reviews f JOIN
reviewss_notes rn
ON r.id = rn.review_id
WHERE r.completed_at IS NOT NULL
AND r.created_at > '2019-01-01'
AND r.type = "long_form"
GROUP BY r.id
) f
GROUP BY weeks_ago
ORDER BY weeks_ago DESC;
Here is the line in the query causing the issues:
AND r.type = "long_form"
The table's design includes a column of:
What am I doing wrong here?
check your quote characters ...
AND r.type = 'long_form'
Use single quotes are for string constants.

Select max(date) in Firebird [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Firebird Query- Return first row each group
(4 answers)
Closed 5 years ago.
I have a table in a Firebird 2.5 Database where I have to find out the latest entry for one specific articel no. Therefore the table has a colum "date".
I tried several queries but I always get more than 1 entry for the article no.
I used the following query:
select max(lp.datum) as Wareneingangsdatum, lp.nr, lp.artikelnr, lp.belegnr, lf.nr, lf.name, lf.plz, lf.ort, lf.laenderkuerzelnr, lae.bezeichnung
from lagerprotokoll lp
left join waeinpos we on we.idnr = lp.belegposidnr
left join waein we1 on we1.nr = we.nr
left join liefrant lf on lf.nr = we1.lieferantnr
left join laender lae on lae.nr = lf.laenderkuerzelnr
where lp.belegtyp = 14
group by 2,3,4,5,6,7,8,9, 10
order by 1 desc
How can I achieve the result so that I can only get the latest entry for one specific article in the table.

using group by in oracle not for count(*) but for min() function [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
how to use group by for 3 columns it is showing an error that it is not a group by expression.
here is the query.
select
brewer.name as brewer,beer.name as beer,count(ratings.score) as LowestRating
from ratings,beer,brewer
where ratings.beer=beer.id and beer.brewer=brewer.id
group by brewer,beer;
You should GROUP BY by columns, not whole tables:
select
brewer.name as brewer,
beer.name as beer,
count(ratings.score) as LowestRating
from
ratings,
beer,
brewer
where
ratings.beer = beer.id
and beer.brewer = brewer.id
group by brewer.name, beer.name;