Rails: SQL SELECT Statement Not Appearing When Combined With GROUP BY - sql

I have the following code in Ruby:
Comment.select("comments.*, COALESCE(SUM(votes.value), 0) AS rating,
user_votes.value AS user_value").
joins("LEFT JOIN votes ON votes.voteable_type = '"+type+"' AND votes.voteable_id = comments.id").
joins("LEFT JOIN votes AS user_votes ON user_votes.voteable_type = '"+type+"' AND user_votes.voteable_id = comments.id AND user_votes.user_id = #{user_id}").
where(conditions).group("comments.id").order("comments."+method).limit(limit).offset(offset)
When Rails generates this SQL query, it doesn't include the full select statement:
SELECT COUNT(*) AS count_all, comments.id AS comments_id FROM `comments`
LEFT JOIN votes ON votes.voteable_type = 'reply' AND votes.voteable_id = comments.id
LEFT JOIN votes AS user_votes ON user_votes.voteable_type = 'reply' AND
user_votes.voteable_id = comments.id AND user_votes.user_id = 1 WHERE (commentable_id =
1 AND commentable_type = 'Impression')
GROUP BY comments.id ORDER BY comments.rating DESC LIMIT 10 OFFSET 0
If I remove the group statement, however, then it properly includes the full select statement. What's going on?

select comments.* in your select statement conflicts with group by("comments.id").
Every column selected must either be in the GROUP BY clause or be contained in an aggregate function.
SQL Server shows error for SQL:
select c.* from tt_country c group by c.name
Column 'tt_country.country_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
ActiveRecord and arel throws out the specified select because it is ambiguous / invalid.

Related

how to make DISTINCT, ORDER BY, CASE work together?

Required sort by condition - if field is exclusive, then sort by "fees"."exclusive_price" else by "fees"."additional_price".
SQL completely looks like this:
SELECT DISTINCT "numbers".*
FROM "numbers"
INNER JOIN "users_numbers" ON "users_numbers"."number_id" = "numbers"."id"
INNER JOIN "users" ON "users"."id" = "users_numbers"."user_id"
INNER JOIN "fees" ON "fees"."user_id" = "users"."id"
WHERE "numbers"."state" != 'removed'
ORDER BY CASE "numbers"."is_exclusive" WHEN TRUE THEN "fees"."exclusive_price" ELSE "fees"."additional_price" END desc"
But I get an error (in rails):
ActiveRecord::StatementInvalid: PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
Error because of distinct. Without distinct - success.
How to correct SQL?
Added columns from related tables to SELECT - didn't help:
SELECT DISTINCT "fees"."exclusive_price", "fees"."additional_price", "sender_numbers".*
FROM ...
A plain DISTINCT de-duplicates based on the complete SELECT list. The (final) ORDER BY step then only accepts expressions that are part of that SELECT list.
To order by that CASE expression, you'd have to include it in the SELECT list. Like:
SELECT DISTINCT n.*, CASE WHEN n.is_exclusive THEN f.exclusive_price ELSE f.additional_price END AS order_col
FROM numbers n
JOIN users_numbers un ON un.number_id = n.id
JOIN users u ON u.id = un.user_id
JOIN fees f ON f.user_id = u.id
WHERE n.state <> 'removed'
ORDER BY order_col DESC;
You would have to wrap that in an outer query to remove order_col from the SELECT list. But I am not sure, we have the complete picture, yet. And there may be simpler ways ...

postgres: LEFT JOIN table and field does not exist

This is my query
SELECT org.id,
org.name,
org.type,
org.company_logo,
(SELECT org_profile.logo_url FROM org_profile WHERE org_profile.org_id = org.id AND org_profile.status = 'active' ORDER BY org_profile.id DESC LIMIT 1) as logo_url,
org.short_description,
org_profile.value_prop,
count(*) OVER () AS total
FROM org
LEFT JOIN user_info ON user_info.id = org.approved_by
INNER JOIN (select distinct org_profile.org_id from org_profile) org_profile ON org_profile.org_id = org.id
WHERE
org.type = 'Fintech'
AND org.status = 'APPROVED'
AND org.draft != TRUE
ORDER BY org.id DESC
I am using LEFT JOIN query with my org_profile table. I used distinct for unique org id but the problem is org_profile.value_prop column does not work. The error is showing column org_profile.value_prop does not exist
I'm trying to solve this issue. But I don't figure out this issue.
basically, the error informs that you try to get the value_prop field from org_profile subquery, which basically doesn't exist.
It's difficult to give a working query by writting just on the paper, but I think that:
it's worth to apply the handy aliasses for each subquery
deduplication, if required, should be done in the subquery. When multiple fields used DISTINCT may be insufficient - RANK function may be required.
you make some operations to get the logo_url by a scalar subquery - it seems a bit strange, especially the same table is used in JOIN - I would suggest to move all logic related to org_profile to the subquery. Scalar expressions would throw an error in case multiple values would be found in output.
SELECT
org.id,
org.name,
org.type,
org.company_logo,
prof.logo_url,
org.short_description,
prof.value_prop,
count(*) OVER () AS total
FROM org
JOIN (
select distinct org_id, logo_url, value_prop -- other deduplication type (RANK) may be required
from org_profile
where status = 'active' -- ?
) prof ON org.id = prof.org_id
LEFT JOIN user_info usr ON usr.id = org.approved_by
WHERE
org.type = 'Fintech'
AND org.status = 'APPROVED'
AND org.draft != TRUE
ORDER BY org.id DESC

Oracle SQL - how to NOT SHOW athlete name that apears only once

created a view called winners, it contains the columns: athlete_name,year,medal_won
its basicly athletes that won olympic medal and the year,
it look like that,
data base is in live sql: https://livesql.oracle.com/apex/f?p=590:1000:0
select distinct year,athlete_name,medal
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
order by athlete_name
as you can see some name show only once and some names are showing more than once, i want to get rid off all lines of those who show ONLY ONCE, please help me.
thank you!
if i have understand your problem, must group your data,
select year,athlete_name,medal, count(*) "number of Medals"
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
group by year,athlete_name,medal;
If I followed you correctly, you can use window functions:
select *
from (
select og.year, oa.athlete_name, om.medal, count(*) over(partition by oa.id) cnt
from olym.olym_medals om
join olym.olym_athlete_games oag on oag.id = om.athlete_game_id
join olym.olym_nations ona on ona.id = oag.nation_id
join olym.olym_games og on og.id = oag.game_id
join olym.olym_athletes oa on oa.id = oag.athlete_id
) t
where cnt > 1
order by athlete_name
Notes:
I am unsure why you were using distinct in the first place, so I removed it (I suspect it is actually not needed)
I added table aliases to shorten the query, and prefixed the columns in the select clause with the table they belong to (you might want to review that) - these are best practices when dealing with multi-table queries
Use GROUP BY and HAVING COUNT(*) > 1:
SELECT year,
athlete_name,
medal
FROM olym.olym_medals
INNER JOIN olym.olym_athlete_games
ON olym_athlete_games.id = olym_medals.athlete_game_id
INNER JOIN olym.olym_nations
ON olym_nations.id = olym_athlete_games.nation_id
INNER JOIN olym.olym_games
ON olym_games.id = Olym_athlete_games.game_id
INNER JOIN olym.olym_athletes
ON olym_athletes.id = olym_athlete_games.athlete_id
GROUP BY
year,
athlete_name,
medal
HAVING COUNT(*) > 1
ORDER BY athlete_name

oracle ora-00979 symfony2 doctrine2

I'm building the following DQL:
SELECT partial operador.{id, pmn, neteo},
sum(outcollect.montoTotal) as montoOut,
outcollect.periodo as periodo,
outcollect.pagado
FROM RoamingOperadoresBundle:Operador operador
LEFT JOIN operador.operadorHub operadorHub
LEFT JOIN operador.outcollect outcollect
WHERE operador.ishub = 0
AND operador.neteo = 1
AND operador.incluirReportes = 1
AND operador.pmn not in('CHLTM', 'CHLCM')
AND operador.id not in(SELECT op.id
FROM RoamingOperadoresBundle:Operador op
LEFT JOIN op.operadorHub opHub
LEFT JOIN op.outcollect out
WHERE (opHub.desde <= out.periodo and (opHub.hasta >= out.periodo or opHub.hasta is null))
)
AND operador.activo = 1
GROUP BY operador.pmn, outcollect.periodo ORDER BY operador.pmn
Which transforms into this SQL:
SELECT o0_.id AS ID0,
o0_.pmn AS PMN1,
o0_.neteo AS NETEO2,
sum(o1_.monto_total) AS SCLR3,
o1_.periodo AS PERIODO4,
o1_.pagado AS PAGADO5
FROM operador o0_
LEFT JOIN operador_hub o2_ ON o0_.id = o2_.operador_id
LEFT JOIN outcollect o1_ ON o0_.id = o1_.operador_id
WHERE o0_.ishub = 0
AND o0_.neteo = 1
AND o0_.incluirReportes = 1
AND o0_.pmn NOT IN ('CHLTM', 'CHLCM')
AND o0_.id NOT IN (SELECT o3_.id FROM operador o3_ LEFT JOIN operador_hub o4_ ON o3_.id = o4_.operador_id LEFT JOIN outcollect o5_ ON o3_.id = o5_.operador_id WHERE (o4_.desde <= o5_.periodo AND (o4_.hasta >= o5_.periodo OR o4_.hasta IS NULL)))
AND o0_.activo = 1
GROUP BY o0_.pmn, o1_.periodo
ORDER BY o0_.pmn ASC
Which gives the following error:
ORA-00979: no es una expresión GROUP BY
Any pointers on how to change the DQL to solve this problem?
I'm using doctrine2, symfony2 and OCI8
Per Oracle documentation
ORA-00979 not a GROUP BY expression
Cause: The GROUP BY clause does not contain all the expressions in the SELECT clause. SELECT expressions that are not included in a group
function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must
be listed in the GROUP BY clause.
Action: Include in the GROUP BY clause all SELECT expressions that
are not group function arguments.
So you need to include all the expressions/column in select list to your group by. change the group by of your SQL query to below
group by o0_.pmn, o1_.periodo, o0_.neteo, o1_.pagado, o0_.id
GROUP BY of DQL to
GROUP BY operador.pmn, outcollect.periodo, operador.id,
operador.neteo, outcollect.pagado

Subquery with multiple joins involved

Still trying to get used to writing queries and I've ran into a problem.
Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)
The inner query gives an ID number in one column, the amount of times they appear in the table, and then a bit attribute if they are in region A. The outer query works but the error I get is incorrect syntax near the keyword IN. Of the inner query, I would like a number of how many of them are in region A
You must specify table name in query before where
Select count(region)
from table
where (regionTable.A=1) in
And you must choose one of them.
where regionTable.A = 1
or
where regionTable.A in (..)
Your query has several syntax errors. Based on your comments, I think there is no need for a subquery and you want this:
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A
which can be further simplified to:
select jxn.id, count(jxn.id) as counts
, 1 as A --- you can even omit this line
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
You are getting the error because of this line:
where (regionTable.A=1)
You cannot specify a condition in a where in clause, it should only be column name
Something like this may be what you want:
SELECT COUNT(*)
FROM
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from
jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
) sq
WHERE sq.a = 1