In Django how to perform the following SQL:
select sum(valor_aprovado) as vla
from convenios
group by ano
order by ano
Desired result:
sum_value |
-------------|
472837446.59|
163438615.51|
448397994.27|
959203234.57|
739555948.32|
I'm using the following syntax, but I just want it to return the VLA field and not the ANO
Convenios.objects.values_list('ano').order_by('ano').annotate(vla=Sum('valor_aprovado')
Result:
[(2002, Decimal('36246385105.92')), (2003, Decimal('163442260.52')), (2004, Decimal('447464292.52')), (2005, Decimal('948880015.70')), (2006, Decimal('737373593.32')), (2007, Decimal('1449818896.88')), (2008, Decimal('1812889287.82')), (2009, Decimal('2306375485.81')), (2010, Decimal('8730479758.56')), (2011, Decimal('1662088139.88')), (2012, Decimal('1886396504.43')), (2013, Decimal('535507602.69')), (2014, Decimal('4279003118.70')), (2015, Decimal('1883240765.95')), (2016, Decimal('1245291830.72')), (2017, Decimal('2161176688.18')), (2018, Decimal('1346548803.43'))]
Can you help me?
Thanks
You can try the following query
Convenios.objects.values_list('ano').order_by('ano').annotate(vla=Sum('valor_aprovado')).values('vla')
Related
I use the wonderful function tableby to have some summary statistics of my population and I also use the amazing package papaja to write my article.
Here is my code for the use of tableby.
DemoGroup <- tableby (Group ~ Age + education + logMAR + QIT + IRP + ICV + AQ + otherDiag, data=demoShort)
summary(DemoGroup,digits = 1, digits.p =3, booktabs = T, title = "(\\#tab:demo) Demo")
I need my document in a two-column fashion (and use the two-column classoption). However, when I try to knit my document, I have this error related to my table (everything works fine in a single column document):
I was unable to find any missing LaTeX packages from the error log 2022_MMN_FS_TSA_WO.log.
! Package longtable Error: longtable not in 1-column mode.
Error: LaTeX failed to compile 2022_MMN_FS_TSA_WO.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
Is there a way to fix this error?
Thank you for your help,
Chers,
Adeline
activerecord 4.2.11.1
activerecord-jdbc-adapter 1.3.25
activerecord-jdbcpostgresql-adapter 1.3.25
jdbc-postgres 9.4.1206
The following method call returns a Date when using ruby 2.3.3 but returns a String when using JRuby-9.1.17.0 with activerecord-jdbcpostgresql-adapter :
2.3.3 :017 > Table.select('now() as date').first.date
Table Load (0.7ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> 2019-05-17 03:46:52 UTC
jruby-9.1.17.0 :002 > Table.select('now() as date').first.date
Table Load (2.0ms) SELECT now() as date FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> "2019-05-17 03:48:57.572526+00"
This only happens if the selected attribute does not exist in the database:
2.3.3 :012 > Table.select(:created_at).first.created_at
Table Load (0.6ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00
jruby-9.1.17.0 :019 > Table.select(:created_at).first.created_at
Table Load (0.8ms) SELECT "tables"."created_at" FROM "tables" ORDER BY "tables"."id" ASC LIMIT 1
=> Wed, 25 Sep 2013 14:26:17 -03 -03:00
This happens with either WebRick or Tomcat 7. This error does not occur with the same version of activerecord-jdbc-adapter if using Active Record 4.0 or 4.1. Upgrading to latest jruby-9.2.7.0 did not help either.
seems like a compatibility bug in AR-JDBC, at this point likely a won't fix as AR 4.2 also isn't supported.
you should try upgrading to 5.x if this is an issue or try resolving the problem and submit a PR ...
as a last resort try setting ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = true (altough I think its already truewhen running on AR 4.2)
as #kares noted, this look like a bug, also it is related to the configuration ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time. Its seems like if it is set to true the method call below returns a String, otherwise it returns a Time instance:
Locatario.select('now() as date').first.date
I have checked this behaviour with 3 versions of active_record:
Rails 3.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns String
JRuby: returns String
Rails 4.0/4.1
default raw_date_time? == nil
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns Time
Rails 4.2
default raw_date_time? == true
Table.select('now() as date').first.date
Ruby: returns Time
JRuby: returns String
So, it looks like to emulate active_record >= 4.0 the gem activerecord-jdbc-adapter should use raw_date_time = false, because it should return a Time instance with the method call in question. As a workaround I created a file with the following content under initializers:
if RUBY_PLATFORM == "java" && Rails::VERSION::STRING.starts_with?("4.2.")
ActiveRecord::ConnectionAdapters::JdbcConnection.raw_date_time = false
end
The query
SELECT REGEXP_SUBSTR('Outstanding Trade Ticket Report_08 Apr 14.xlsx', '\_(.*)\.') AS FILE_DATE FROM DUAL
gives the OUTPUT:
_08 Apr 14.
Please advise the correct regex to be used for getting the date without the characters.
I can use RTRIM and LTRIM but want to try it using regex.
You can use:
SELECT REGEXP_SUBSTR('Outstanding Trade Ticket Report_08 Apr 14.xlsx', '\_(.*)\.',
1, 1, NULL, 1) from dual
The last argument is used to determine which matched group to return.
Link to Fiddler
I have this DQL Query:
$repository->createQuery("SELECT f.id, COALESCE(f.name,
CONCAT(f.floor_number, ' NP')) as name FROM ".__NAMESPACE__.'\\Floor f
WHERE f.building = ?1')
->setParameter(1, $this->building);
but it doesn't work - i get
[Syntax Error] line 0, col 36: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '('
The reason is usage of CONCAT inside COALESCE.... is there any way how to make this work (without native query)?
This problem was fixed by an improvement to the DQL parser in Doctrine 2.4. Upgrade to 2.4 and this query will work.
I am new to using APEX PL/SQL and apologise in advance if my question has been covered already. I believe have searched exhasutively for an answer.
I have a standard query that works exactly as it should as a source for a region, but requires conversion to PL/SQL so I can process an LOV returned from a shuttle I am going to add.
This is the critical part of the working query:
select METRIC_DEFINITION_ID,
METRIC_NAME,
sum(decode(START_DATE, '01-JUN-12', VALUE)) as "Jun 2012"
from ... (a substantial query that works)
When I attempt to return this from a PL/SQL block:
BEGIN
return 'select METRIC_DEFINITION_ID,
METRIC_NAME,
sum(decode(START_DATE, ' '01-JUN-12' ', VALUE)) as "Jun 2012"
from ... (a substantial query that works)';
END;
I receive the error message:
(ORA-06550: line 9, column 33: PLS-00103: Encountered the symbol "01-JUN-12"
when expecting one of the following: * & = - + ; < ...
as if the compiler is expecting an operator.
I have tried
...TO_DATE(' '01-JUN-12' ')...
with no success. Passing the string to a variable and returning that makes no difference either.
All the documentation I have read suggests my original should work.
Does anyone know if there is different syntax for 'decode' in this context (APEX source for a region), or am I missing something glaringly obvious here?
All suggestions appreciated.
Cheers,
Jason
===========
Adding the complete query, as requested. Note the line
and METRIC_COLLECTION.METRIC_COLLECTION_ID IN :P44_COLLECTION_SELECTOR)
currently only works if a single item is selelcted in the shuttle named P44_COLLECTION_SELECTOR, hence the need to learn PL/SQL.
Dates will be based on inputs in the long run - this is a POC.
select
METRIC_DEFINITION_ID,
METRIC_NAME,
sum(decode(START_DATE, '01-JUN-12', VALUE)) as "Jun 2012",
sum(decode(START_DATE, '01-JUL-12', VALUE)) as "Jul 2012",
sum(decode(START_DATE, '01-AUG-12', VALUE)) as "Aug 2012",
sum(decode(START_DATE, '01-SEP-12', VALUE)) as "Sep 2012",
sum(decode(START_DATE, '01-OCT-12', VALUE)) as "Oct 2012",
sum(decode(START_DATE, '01-NOV-12', VALUE)) as "Nov 2012"
from
(select
METRIC_DEFINITION.METRIC_DEFINITION_ID as METRIC_DEFINITION_ID,
METRIC_DEFINITION.METRIC_NAME as METRIC_NAME,
METRIC_VALUE.START_DATE as START_DATE,
METRIC_VALUE.VALUE as VALUE
from
METRIC_VALUE METRIC_VALUE,
METRIC_DEFINITION METRIC_DEFINITION
where
METRIC_DEFINITION.METRIC_DEFINITION_ID=METRIC_VALUE.METRIC_DEFINITION_ID)
where METRIC_DEFINITION_ID IN
(select
METRIC_COLLECTION_MAP.METRIC_DEFINITION_ID as METRIC_DEFINITION_ID
from METRIC_COLLECTION_MAP METRIC_COLLECTION_MAP,
METRIC_COLLECTION METRIC_COLLECTION
where
METRIC_COLLECTION.METRIC_COLLECTION_ID=METRIC_COLLECTION_MAP.METRIC_COLLECTION_ID
and
METRIC_COLLECTION.METRIC_COLLECTION_ID IN :P44_COLLECTION_SELECTOR)
group by METRIC_DEFINITION_ID, METRIC_NAME
Why did you put spaces in between you quotes?
Try changing:
sum(decode(START_DATE, ' '01-JUN-12' ', VALUE))
To:
sum(decode(START_DATE, ''01-JUN-12'', VALUE))
I Figure this because:
execute immediate 'select to_date(' '01-JUN-12' ') from dual';
returns the same error.