The following code section is returning multiple columns for a few records.
SELECT a.ClientID,ltrim(rtrim(c.FirstName)) + ' ' +
case when c.MiddleName <> '' then
ltrim(rtrim(c.MiddleName)) + '. '
else ''
end +
ltrim(rtrim(c.LastName)) as ClientName, a.MISCode, b.Address, b.City, dbo.ClientGetEnrolledPrograms(CONVERT(int,a.ClientID)) as Abbreviation
FROM ClientDetail a
JOIN Address b on(a.PersonID = b.PersonID)
JOIN Person c on(a.PersonID = c.PersonID)
LEFT JOIN ProgramEnrollments d on(d.ClientID = a.ClientID and d.Status = 'Enrolled' and d.HistoricalPKID is null)
LEFT JOIN Program e on(d.ProgramID = e.ProgramID and e.HistoricalPKID is null)
WHERE a.MichiganWorksData=1
I've isolated the issue to the ProgramEnrollments table.
This table holds one-to-many relationships where each ClientID can be enrolled in many programs. So for each program a client is enrolled in, there is a record in the table.
The final result set is therefore returning a row for each row in the ProgramEnrollments table based on these joins.
I presume my join is the issue but I don't see the problem.
Thoughts/Suggestions?
Thanks,
Chuck
The JOIN is not the issue, it's doing what it's meant to do with a one-to-many relationship
You could use a GROUP BY statement on your query, or alternatively use a sub-select to return DISTINCT values from the ProgramEnrollments/Program tables.
You don't seem to be using data from the ProgramEnrollments or Program tables so are they needed in the query (I presume they are, just thought I'd ask the question).
You don't actually appear to be using any columns in ProgramEnrollments or Program, so try removing those 2 JOINs.
Related
I would like to add to the table A all the column of the table B, doing a join based on a common column (type numeric). I am trying to do it using the LEFT JOIN but the columns added are all blank. this is impossible because table b stores, among others, the same ID values . Where I am wrong?
Select * from "2017_01" left join "Registry_2017" on '2017_01.ID' = 'Registry_2017.ID';
You are doing wrong.. I don't know why you can use right for Table calling "2017_01" and different with this '2017_01.ID'..
' = Single quote identifies as String
" = Double quote identifies as Table or Column to escape Naming
Select
*
From
"2017_01"
left join "Registry_2017" on '2017_01.ID' = 'Registry_2017.ID';
So when you doing this '2017_01.ID' = 'Registry_2017.ID' The condition will always become false because those 2 different String are not equal. Postgresql look the condition not as Table and Column but String because you are using Single quote
Select
*
from
"2017_01"
left join "Registry_2017" on "2017_01"."ID" = "Registry_2017"."ID";
So the query should be like that.. Even you already got answer and it got work i must tell this..
I have two columns in the same table that I want to join in Postgresql but for some reason I’m getting this error. Don’t know how to figure it out. Please help.
[42P01] ERROR: relation "a" does not exist
Position: 10
X table contains two pools(ABC,XYZ), ids, numbers and description. If an ID exists in one pool but not in the other, it should update description column to “ADD”. Pools need to be joined on number.
UPDATE A
SET A.Description = 'ADD'
FROM X AS A
LEFT JOIN X AS B ON B.number = A.number
AND B.id = 'ABC'
WHERE A.id = 'XYZ'
AND B.number IS NULL
AND A.Description IS NULL;
With standard SQL you can't do a join as part of an update, but what you can do is include a subquery to select the id's to update. The subquery can contain a join. I'm not entirely clear on what you're actually trying to accomplish, but you could do something like this:
UPDATE x SET description='ADD' WHERE number IN (
SELECT a.number FROM x AS a
LEFT OUTER JOIN x AS b ON a.number=b.number AND a.id='XYZ' AND b.id='ABC'
WHERE b.number IS NULL
);
This will join the table x with itself and will select (and update) any numbers's that don't have a matching number in the 'ABC' and 'XYZ' zone.
PostgreSQL does have a UPDATE FROM syntax that does let you update with complex subqueries. It's more flexible but it's non-standard. You can find an example of this type of query here.
This question already has answers here:
Nested Case statement type error (postgres)
(2 answers)
Closed 7 years ago.
I have a table with ~5,000 records. I have made three join columns in this table. The values in each column are not unique. I want to join to another table (sequentially) by each of these three columns to return values given a condition.
The join table contains multiple columns. Three of these columns are the join columns which will correspond to the first tables' join columns. The join columns in the join table are unique. I want to take the values from the join table and bring to a new column in the first table.
I have a code that I have put together from other suggestions and it runs but I am receiving over 8 million records in the return table. I want the table to only have the records from the first table.
Here is the code:
CREATE TABLE current_condition_joined AS SELECT
a.id, a.geom, a.condition_join_1, a.condition_join_2, a.condition_join_3,
coalesce(b.condition, c.condition2, d.condition3) as current_condition,
coalesce(b.ecosite, c.ecosite2, d.ecosite3) as current_ecosite,
coalesce(b.ecophase, c.ecophase2, d.ecophase3) as current_ecophase,
coalesce(b.consite, c.consite2, d.consite3) as current_consite,
coalesce(b.conphase, c.conphase2, d.conphase3) as current_conphase
FROM current_condition a
LEFT JOIN boreal_mixedwood_labeled b ON a.condition_join_1 = b.label
LEFT JOIN boreal_mixedwood_labeled c ON a.condition_join_2 = c.label2
LEFT JOIN boreal_mixedwood_labeled d ON a.condition_join_3 = d.label3
WHERE b.condition != 'ERROR' and c.condition2 != 'ERROR';
I want to get the values from the first join if condition is not ERROR, else the values from the second join if condition is not ERROR, else the values of the third join.
I've looked around, but all examples are asking slightly different things then I am so I can't piece it together.
This is not the same question as: Nested Case statement type error (postgres)
The question asked there was in regard to making a nested statement work. This question is about how the join works. Two different questions, two different posts.
Try add a DISTINCT.
CREATE TABLE current_condition_joined AS SELECT DISTINCT
a.id, a.geom, a.condition_join_1, a.condition_join_2, a.condition_join_3,
coalesce(b.condition, c.condition2, d.condition3) as current_condition,
coalesce(b.ecosite, c.ecosite2, d.ecosite3) as current_ecosite,
coalesce(b.ecophase, c.ecophase2, d.ecophase3) as current_ecophase,
coalesce(b.consite, c.consite2, d.consite3) as current_consite,
coalesce(b.conphase, c.conphase2, d.conphase3) as current_conphase
FROM current_condition a
LEFT JOIN boreal_mixedwood_labeled b ON a.condition_join_1 = b.label
LEFT JOIN boreal_mixedwood_labeled c ON a.condition_join_2 = c.label2
LEFT JOIN boreal_mixedwood_labeled d ON a.condition_join_3 = d.label3
WHERE b.condition != 'ERROR' and c.condition2 != 'ERROR';
You can try use GROUP BY too.
The code you present is what I gave you for your previous question:
Nested Case statement type error (postgres).
But you broke it by moving the conditions b.condition != 'ERROR' and c.condition2 != 'ERROR' to the WHERE clause, which is simply wrong. Consider:
Query with LEFT JOIN not returning rows for count of 0
If rows are multiplied, then your join conditions most probably identify multiple matching rows, multiplying each other. Hard to diagnose while you still refuse to provide the table definition of boreal_mixedwood_labeled like I requested repeatedly for your previous question.
I'm trying create a SELECT with GROUP BY in Firebird but I can't have any success. How could I do this ?
Exception
Can't format message 13:896 -- message file C:\firebird.msg not found.
Dynamic SQL Error.
SQL error code = -104.
Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).
(49,765 sec)
trying
SELECT FA_DATA, FA_CODALUNO, FA_MATERIA, FA_TURMA, FA_QTDFALTA,
ALU_CODIGO, ALU_NOME,
M_CODIGO, M_DESCRICAO,
FT_CODIGO, FT_ANOLETIVO, FT_TURMA
FROM FALTAS Falta
INNER JOIN ALUNOS Aluno ON (Falta.FA_CODALUNO = Aluno.ALU_CODIGO)
INNER JOIN MATERIAS Materia ON (Falta.FA_MATERIA = Materia.M_CODIGO)
INNER JOIN FORMACAOTURMAS Turma ON (Falta.FA_TURMA = Turma.FT_CODIGO)
WHERE (Falta.FA_CODALUNO = 238) AND (Turma.FT_ANOLETIVO = 2015)
GROUP BY Materia.M_CODIGO
Simple use of group by in firebird,group by all columns
select * from T1 t
where t.id in
(SELECT t.id FROM T1 t
INNER JOIN T2 j ON j.id = t.jid
WHERE t.id = 1
GROUP BY t.id)
Using GROUP BY doesn't make sense in your example code. It is only useful when using aggregate functions (+ some other minor uses). In any case, Firebird requires you to specify all columns from the SELECT column list except those with aggregate functions in the GROUP BY clause.
Note that this is more restrictive than the SQL standard, which allows you to leave out functionally dependent columns (ie if you specify a primary key or unique key, you don't need to specify the other columns of that table).
You don't specify why you want to group (because it doesn't make much sense to do it with this query). Maybe instead you want to ORDER BY, or you want the first row for each M_CODIGO.
I have a query which is dynamically generated.
SELECT '' + CAST(GalleryGallery_tGallery._Name AS VARCHAR(4000)) + '' AS NewName
FROM Photographers_tGalleries
LEFT OUTER JOIN Gallery_tGallery AS GalleryGallery_tGallery
ON BaseContent_tGalleries.[Gallery] = GalleryGallery_tGallery._Guid
LEFT OUTER JOIN BaseContent_tGalleries
ON Photographers_tGalleries._Guid =
BaseContent_tGalleries._Guid_Structure_Content
The joins appear correct to me. However, the query errors with The multi-part identifier "BaseContent_tGalleries.Gallery" could not be bound.
The following query does work. While the joins are matching the correct fields, they are in a different order. I am wondering why this one works and the other does not. We would like to fix the top one but since it is dynamic, I am looking for the least amount of change.
SELECT '' + CAST(GalleryGallery_tGallery._Name AS VARCHAR(4000)) + '' AS NewName
FROM Gallery_tGallery AS GalleryGallery_tGallery
LEFT OUTER JOIN BaseContent_tGalleries
ON GalleryGallery_tGallery._Guid = BaseContent_tGalleries.Gallery
LEFT OUTER JOIN Photographers_tGalleries
ON BaseContent_tGalleries._Guid_Structure_Content =
Photographers_tGalleries._Guid
Your join ordering for the first query is wrong. You need to reference BaseContent_tGalleries before Gallery_tGallery.
SELECT '' + CAST(g._Name AS VARCHAR(4000)) + '' AS NewName
FROM Photographers_tGalleries AS g
LEFT OUTER JOIN BaseContent_tGalleries AS b
ON g._Guid = b._Guid_Structure_Content
LEFT OUTER JOIN Gallery_tGallery AS gg
ON b.[Gallery] = gg._Guid;
Who named your tables and aliases by the way? GalleryGallery_tGallery, really? I've converted to shorter aliases to compensate for whoever really likes typing. A LOT.
The first query doesn't work since you're trying to use the table BaseContent_tGalleries in an ON statement, but it has not been joined yet. In other words, you're using a table as a join condition, but the table itself hasn't been joined yet.