Table to stay in the same line as FROM keyword - sqlfluff

This should be a simple one but can't find which is the configuration option after looking at the documentation.
Example sql:
SELECT
people.id,
people.firstname,
people.lastname,
cities.name
FROM people LEFT JOIN cities ON cities.id = people.cityid WHERE people.firstname IN ('plop', 'zoo')
Output sql:
SELECT
people.id,
people.firstname,
people.lastname,
cities.name
FROM
people
LEFT JOIN
cities ON cities.id = people.cityid
WHERE people.firstname IN ('plop', 'zoo')
Current config in .sqlfluff:
[sqlfluff]
dialect = snowflake
sql_file_exts = .sql
I expect the sql to have the table name in the same line, both for the FROM and LEFT JOIN clauses. (The ON clause can be in the same line or the next one, but indented preferably)
Expected sql:
SELECT
people.id,
people.firstname,
people.lastname,
cities.name
FROM people
LEFT JOIN cities ON cities.id = people.cityid
WHERE people.firstname IN ('plop', 'zoo')
Can anyone point me to the right setting?

Related

How to use GROUP BY with Firebird?

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.

Add table to query

I have query where I select id_categories and categories_names, now I added new table categories_description, same as categories_names but with descriptions (of course it has id_category too), how to modify this query to select k.id_category, kn.name and my new category_description from my new table categories_descriptions (sometimes there is no description for category).
SELECT
c.id_category,
cn.name,
FROM
categories c,
categories_names cn
WHERE
c.id_category = cn.id_category
I am using PostgreSQL but I think in MySQL it will be the same.
You mentioned that sometimes the description is missing so LEFT JOIN should be suitable:
SELECT c.id_category,
cn.name,
cd.category_description
FROM
categories c
JOIN categories_names cn ON c.id_category = cn.id_category
LEFT JOIN categories_descriptions cd ON cn.id_category = cd.id_category

Postgres - Get data from each alias

In my application i have a query that do multiple joins with a table position. Just like this:
SELECT *
FROM (...) as trips
join trip as t on trips.trip_id = t.trip_id
left outer join vehicle as v on v.vehicle_id = t.trip_vehicle_id
left outer join position as start on trips.start_position_id = start.position_id and start.position_vehicle_id = v.vehicle_id
left outer join position as "end" on trips.end_position_id = "end".position_id and "end".position_vehicle_id = v.vehicle_id
left outer join position as last on trips.last_position_id = last.position_id and last.position_vehicle_id = v.vehicle_id;
My table position has 35 columns(for example position_id).
When I run the query, in result should appear the table position 3 times, start, end and last. But postgres can not distinguish between, for exemplar, start.position_id, end.position_id and last.position_id. So this 3 columns are group and appear as one, position_id.
As the data from start.position_id and end.position_id are different, the column, position_id, that appear in result, it's empty.
Without having to rename all the columns, like this: start.position_id as start_position_id.
How can i get each group of data separately, for exemple, get all columns from the table 'start'. In MYSQL i can do this operation by calling fetch_fields, and give the function an alias, like 'start'.
But i can i do this in Postgres?
Best Regards,
Nuno Oliveira
My understanding is that you can't (or find it difficult to) discern between which table each column with a shared name (such as "position_id") belongs to, but only need to see one of the sets of shared columns at any one time. If that is the case, use tablename.* in your SELECT, so SELECT trips.*, start.*... would show the columns from trips and start, but no columns from other tables involved in the join.
SELECT [...,] start.* [,...] FROM [...] atable AS start [...]

Difference in query joins

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.

Joining several tables: table specified more than once error

I am attempting to call data after joining all of my tables in a postgreSQL query.
I am getting the following error:
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: table name "place" specified more than once
)
Failed to execute SQL chunk
After referring to similar posts and online resources, I have attempted setting aliases (which only create new errors about not referring to the other tables in the FROM clause), reordering the table names (the cleanest, reordered chunk is posted below), and experimenting with UPDATE/FROM/JOIN as an alternative to SELECT/FROM/JOIN. However, I think I ultimately need to be using the SELECT clause.
SELECT *
FROM place
INNER JOIN place ON place.key = form.key
INNER JOIN form ON form.id = items.formid
INNER JOIN items ON items.recid = rec.id
INNER JOIN rec ON rec.id = sub.recid
INNER JOIN sub ON sub.id = type.subid
INNER JOIN type ON type.name = det.typeid;
You have "place" in your query twice, which is allowed but you would have to alias the second use of the table "place". Also you reference something called "det", but it's not a table or alias anywhere in your query. If you want only one place that's fine, just remove the INNER JOIN place and move your place.key = form.key to the form join or to a where clause.
If you want to place tables in their because you are trying to join the table to itself the alias the second one, but you will want to make sure that you then have a clause to join those two tables on (could be part of an on or a where)
The issue ended up being the order of table names. The code below joined everything just fine:
SELECT *
FROM place
INNER JOIN form ON place.key = form.key
INNER JOIN items ON form.id = items.formid
INNER JOIN rec ON items.recid = rec.id
INNER JOIN sub ON rec.id = sub.recid
INNER JOIN type ON sub.id = type.subid
INNER JOIN det ON type.name = det.typeid;