ORDER BY issue in Postgres (Heroku) - sql

The following code works for Postgres (Heroku):
#messages = Message.select("DISTINCT
ON (messages.conversation_id)
*").where("messages.sender_id = (?) OR messages.recipient_id = (?)",
current_user.id, current_user.id)
However, when attempting to order the results by appending .order("messages.read_at DESC") I receive the following error:
ActionView::Template::Error (PGError: ERROR: column id_list.alias_0 does not exist)
In looking at the generated SQL, I see that an alias is being created around the ORDER BY statement when not asked for:
messages.recipient_id = (32))) AS id_list ORDER BY id_list.alias_0 DESC)
I've not been able to figure out a workaround short of using "find_by_sql" for the entire statement - which takes a heavy toll on the app.

Don't vote this, I only post because posting many lines in comments does not show very well.
I would write a "query that returns messages grouped by their conversation_id, so that the last message in each conversation is shown" like this:
SELECT m.*
FROM messages m
JOIN
( SELECT conversation_id
, MAX(created_date) AS maxdate
FROM messages
WHERE ...
GROUP BY conversation_id
) AS grp
ON grp.conversation_id = m.conversation_id
AND grp.maxdate = m.created_date
ORDER BY m.read_at DESC
No idea how this can be done in Heroku or if it even possible, but it avoids the DISTINCT ON. If that's causing the error, it may be of help.

Related

Build query that brings only sessions that have only errors?

I have a table with sessions events names. Each session can have 3 different types of events.
There are sessions that have only error type event and I need to identify them by getting a list those session.
I tried the following code:
SELECT
test.SessionId, SS.RequestId
FROM
(SELECT DISTINCT
SSE.SessionId,
SSE.type,
COUNT(SSE.SessionId) OVER (ORDER BY SSE.SessionId, SSE.type) AS total_XSESIONID_TYPE,
COUNT(SSE.SessionId) OVER (ORDER BY SSE.SessionId) AS total_XSESIONID
FROM
[CMstg].SessionEvents SSE
-- WHERE SSE.SessionId IN ('fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb' )
) AS test
WHERE
test.total_XSESIONID_TYPE = test.total_XSESIONID
AND test.type = 'Errors'
-- AND test.SessionId IN ('fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb' )
Each session can have more than one type, and I need to count only the sessions that have only type 'errors'. I don't want to include sessions that have additional types of events in the count
While I'm running the first query I'm getting a count of 3 error event per session, but while running the all procedure the number is multiplied to 90?
Sample table :
sessionID
type
fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb
Errors
fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb
Errors
fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb
Errors
00c896a0-dccc-41bf-8dff-a5cd6856bb76
NonError
00c896a0-dccc-41bf-8dff-a5cd6856bb76
Errors
00c896a0-dccc-41bf-8dff-a5cd6856bb76
Errors
00c896a0-dccc-41bf-8dff-a5cd6856bb76
Errors
In this case I should get
sessionid = fa3ed523-60f9-4af0-a85f-1dec9e9d2cdb
Please advice - hope this is clearer now, thanks!
It's been a long time but I think something like this should get you the desired results:
SELECT securemeSessionId
FROM <TableName> -- replace with actual table name
GROUP BY securemeSessionId
HAVING COUNT(*) = COUNT(CASE WHEN type = 'errors' THEN 1 END)
And a pro tip: When asking sql-server questions, it's best to follow these guidelines
SELECT *
FROM NameOfDataBase
WHERE type!= 'errors'
Is it what you wanted to do?

Rails impressionist gem - missing FROM-clause entry for table "impressions"

I am using Rails 4.2 and have set up the impressionist gem which is working fine for logging page impressions.
I am trying to do exactly the same thing as in this Stackoverflow post but the answer isn't working. I tried:
start_time = 30.days.ago
#mostReadAlbums30Days = Album.joins(:impressions).where("impressions.created_at<='#{Time.now}' and impressions.created_at >= '#{start_time}'").group("impressions.impressionable_id").order("count(impression‌​s.id) DESC")
This produces the following SQL and error message
PG::UndefinedTable: ERROR: missing FROM-clause entry for table
"impression‌​s" LINE 1: ... BY impressions.impressionable_id ORDER BY
count(impression...
^ : SELECT "albums".* FROM "albums" INNER JOIN "impressions" ON
"impressions"."impressionable_id" = "albums"."id" AND
"impressions"."impressionable_type" = $1 WHERE
(impressions.created_at<='2015-01-02 00:50:18 -0200' and
impressions.created_at >= '2014-12-03 02:50:18 UTC') GROUP BY
impressions.impressionable_id ORDER BY count(impression‌​s.id) DESC
I'm using Postgresql v9.3.5.2. How can I get this query to work?
The main problem (per comment):
There's a subtle typo in your query. It contains a Unicode Character ZERO WIDTH SPACE (U+200B) between the n and s of ORDER BY count(impression‌s.id). Look at how impressions gets line-broken in the rendering of the error message, that wouldn't happen otherwise.
The GROUP BY problem:
since the query selects albums.*, it has to GROUP BY albums.id rather than GROUP BY impressions.impressionable_id (assuming that albums.id is a primary key). This is numerically equivalent since the tables are equi-joined through these two fields anyway, but as it seems, the SQL engine in this context can't figure out this equivalency by itself.

Trouble with integrating COUNT function in SQL query using Oracle

I am trying to self educate myself in SQL in order to better use databases at work. For this purpose I am using Oracle Application Express. This if my first time using the COUNT function and I am having some difficulties integrating it within my query. I have done a great deal of research and read quite a bit of literature but I just can't seem to get it right.
My goal is to display the channel_name and channel_number columns (from the channel table) for each channel along with a count of the number of customers that have that channel as a favorite channel (survey_result column from the survey table). Please see below for code:
SELECT channel.channel_number,
channel.channel_name,
survey.survey_result,
count(SELECT survey.survey_result FROM survey)
From Channel, survey
WHERE survey.channel_number = channel.channel_number
Currently I am getting the error message:
ORA-00936: missing expression.
Try this:
Below query gives you only those channels which have minimum 1 customer.
SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers
FROM Channel C
INNER JOIN survey S ON S.channel_number = C.channel_number
GROUP BY C.channel_number, C.channel_name;
And below query gives you all channels whether it has customer or not.
SELECT C.channel_number, C.channel_name, COUNT(S.survey_result) NoOfCustomers
FROM Channel C
LEFT JOIN survey S ON S.channel_number = C.channel_number
GROUP BY C.channel_number, C.channel_name;
Either of these may work for you
SELECT channel.channel_number,
channel.channel_name,
count(survey.survey_result)
From Channel, survey
WHERE survey.channel_number = channel.channel_number
GROUP BY
channel.channel_number,
channel.channel_name
or
SELECT channel.channel_number,
channel.channel_name,
survey.survey_result,
(SELECT count(survey_result) FROM survey)
From Channel, survey
WHERE survey.channel_number = channel.channel_number
count is an aggregate function thus you should have a group by on channel.channel_number and channel.channel_name. then just use count(survey.survey_result) instead of count(SELECT survey.survey_result FROM survey). Madhivanan's and Saharsh Shah's answers look good to me. including this answer to explain why.

Rails query - latest Post for each day

A User has_many Posts. I want to retrieve the latest Post for each day (using created_at), ignoring other posts that may have been written earlier. Another way to pose this question might to ask for a each top salary earning employee by department - same thing I think.
How do I write this query in Rails (4.0 preferably)? I think it has something to do with group and maximum but I can't seem to get it. Is there a way to do it without resorting to SQL?
To clarify, what I'd like returned is an array of post objects that are the last ones written on their respective date.
Thanks!
Something like this. You can convert this to AREL syntax as needed:
SELECT posts.created_at, *
FROM posts
INNER JOIN (
SELECT MAX(created_at) AS max_order_date FROM posts
GROUP BY DATE(posts.created_at)
) AS last_postings ON last_postings.max_order_date = posts.created_at
ORDER BY DATE(created_at) DESC
LIMIT 10
AREL syntax might be:
join_sql = <<-SQL
INNER JOIN (
SELECT MAX(created_at) AS max_order_date FROM posts
GROUP BY DATE(posts.created_at)
) AS last_postings ON last_postings.max_order_date = posts.created_at
SQL
Post.joins(join_sql).order('DATE(created_at) DESC')
Remove the LIMIT as it suits you.
It's not very clean, but this works in Rails 3 (taken from a Book model in my case) using PostgreSQL syntax for truncating the created_at to the date:
max_created_at_list = Book.select("max(created_at) as created_at").group("date_trunc('day',created_at)")
last_books = Book.where(:created_at => max_created_at_list)
... or just:
Book.where(:created_at =>Book.select("max(created_at) as created_at").group("date_trunc('day',created_at)"))
You'd want an index on created_at for large data sets, and either created_at to be constrained to not null at the database level or an "is not null" predicate if the RDBMS you use does not index nulls (eg. Oracle)
Try this
Post.select("user_id, max(created_at) as created_at").group(:user_id)

PGError: ERROR: column “recipes.id” must appear in the GROUP BY clause or be used in an aggregate function

I have this SQL query in my DB which is causing a problem with PostgreSQL on heroku, Causing the page not to load with the above error in the heroku logs. I am using postgreSQL 9.1.6 so previous bugs have apparently been fixed
def self.top_countries
joins(:recipes).
select('countries.*, count(*) AS recipes_count').
group('countries.id').
order('recipes_count DESC')
end
I am unsure on how to refactor this so that it will work.Could anyone advise please?
Thank You
def self.top_countries
joins(:recipes).
select('countries.id, count(*) AS recipes_count').
group('countries.id').
order('recipes_count DESC')
This generates the SQL
select countries.id, count(*) AS recipes_count
from countries
join recipes on countries.id = recipes.country_id
group by countries.id
order by recipes_count
You'll notice that you only have 2 columns in the SELECT.
Not being a Heroku expert, I suspect you can get it to work by explicitly listing all column that you need from countries, and grouping by the full column list i.e.
def self.top_countries
joins(:recipes).
select('countries.id, countries.name, countries.other, count(*) AS recipes_count').
group('countries.id, countries.name, countries.other').
order('recipes_count DESC')
There might be a more concise way to join the original answer (top part) with another join to top_countries on countries.id to get the rest of the columns after the group by.