I wrote a SQL query to design an ORM. Now, I am trying to convert this query to GORM. I read the documentation but I didn't make a proper decision if I use Clouse or conditions.
This is the SQL query I am intending to convert
select collection, count(*) as item_count from collections group by collection order by item_count desc, collection
Thanks in advance.
Something like
db.Select("collection", "count(*) as item_count").Group("collection").Order("item_count desc, collection").Find(&results)
should do the trick. Is that what you are looking for? If not, please comment and clarify.
Related
Im asking for PostgreSQL specifically, but answers for other popular SQL implementations are appreciated as well.
Given an sql query with multiple aggregates, especially array_agg, is the order of the aggregated values deterministic?
Example:
SELECT ARRAY_AGG(columnA), ARRAY_AGG(columnB) FROM myTable
GROUP BY columnC
Can I rely on both arrays to have the same order, meaning values at position i in both arrays will belong to the same source row?
I can't find anything about this in the docs and I'm unsure because I've read that parallelization could be used in calculating aggregates, which I'm afraid could possibly result in non-deterministic orders.
The order is never deterministic if you don't provide an order by
So if you need a specific order, then specify it:
SELECT ARRAY_AGG(columnA order by some_sort_column),
ARRAY_AGG(columnB order by some_sort_column)
FROM myTable
GROUP BY columnC
I'm not sure why people here didn't understand the question and all of them quote the documentation incorrectly, by providing correct answer, but for different question.
Yes, both aggregate functions are called on the same row, at the same time, and this is guided by the query parser.
If you don't believe or trust it you can do something better that doing the sorting:
SELECT ARRAY_AGG(CONCAT(myTable.ctid, ':', columnA)),
ARRAY_AGG(CONCAT(myTable.ctid, ':', columnB))
FROM myTable
GROUP BY columnC
now you'll see that both result aggregates having the same ctid on both sides, for each element of the array, simply because both are called at the same time and there is no way around for PG to do something else.
Assuming that is not enough to convince you, then you have no other choice but to refactor your original query like this:
SELECT ARRAY_AGG(ARRAY[columnA, columnB])
FROM myTable
GROUP BY columnC
Now you have what you want: columnA and columnB together!
My apologies if this has already been covered, I just could not find a suitable answer. If anyone know of one, please do let me know.
I wrote a quick tool to take all the fields in the second table and concatenate them, then update the AddtionalTripLineInfo field in the first table. it was fairly quick, however, my question is, is there an easier straight with a SQL query to do this and make the formatting the same?
I'm using SQL Server and it doesn't matter if this is not looking right or formatting, this is the job I was dealt and that is the format they want.
thank you in advance!
From your image I'm going to guess you're using SQL Server, if so...
CONCAT() will combine the SeqNo and the Distance
STRING_AGG() will combine the rows
SELECT
TripID,
STRING_AGG(
CONCAT(SeqNo, '|', ThruWayDistanceCalculated),
'~'
) WITHIN GROUP (
ORDER BY SeqNo
)
FROM
example
GROUP BY
TripID
Demo : https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c18456392ee5a242d45a945a8aaadbb2
is there a way to do such thing?
suppose that i have the following table called columnstogroupby
ColumnsToGroupBy
Column1
Column2
Coulmn3
and i have this query
Select sum(someColumn) Total,Column1,Column2,Column3 From MyTable Group By Column1,Column2,Column3
is there a way the i can create this query dynamically using the columnstogroupby table?
note that the sum(someColumn) Total is always the same, i just need to include all columns in the columnstogroupby in my select and group by clauses, is that possible?
Yes, this is possible using dynamic SQL.
See the MSDN help page for sp_executesql and be sure to consider the security implications.
A Google search for SQL Server dynamic returns plenty of examples too.
I have a table here that has millions of entries. The table stores event and each event has a timestamp. Given some WHERE clause params, I want the most recent event.
This is what we came up with:
SELECT *
FROM eventTable
WHERE timestamp_date >= '2012-07-16' AND
eventType = 1 AND someOtherField = 'value'
ORDER BY timestamp DESC
LIMIT 1;
Currently this is taking way too long to return. Is there a way to alter the SQL to give me what we want a lot faster? Also, is there a projection strategy for things like this?
thanks
jose
Essentially, you want at least one of your projections to be optimized according to the timestamp column in the order by clause. If you do your projections properly, this should be quite fast.
Edit: Just a note of clarification. I didn't mean to imply that the timestamp column should be the only field in the order by clause. Also, as also mentioned in the comments below, a query-specific run on the Database Designer would not be a bad thing in this case. I believe there is an option to optimize for speed. Since you are doing "select *" the output should probably serve as the super and buddy projections.
Unsure exactly how to put this question so apologies for the awkward phrasing, generally when I know how to properly describe a problem I can use the site search to look for it, or Google etc.
Anyway, simple enough issue - I'm querying a link / junction table with a variable array of AttributeIds, for example the IDs 14 and 17. Using the following table:
http://img220.imageshack.us/img220/5999/setattributecombo.gif
The only valid result I want to return from this query is where the ProductSetId is the same, so instead of 'IN' I want something like
WHERE AttributeIds IN 14,17 AND ProductSetId is the same
In the above example, the only valid result would be 5 but if I use an IN query I get 2,5,7 as results.
Any ideas?
SELECT ProductSetID, COUNT(*) AS CountOfMatchingRows
From MyTable
WHERE AttributeId IN (14,17 )
GROUP BY ProductSetID
HAVING COUNT(*) = 2
It sounds like you want to perform Relational Division. A good article on various methods is over at Simple-Talk. http://www.simple-talk.com/sql/t-sql-programming/divided-we-stand-the-sql-of-relational-division/