This question already has answers here:
SQL result column showing no value due to precision?
(4 answers)
PostgreSQL 10.1 incorrect division output
(2 answers)
Simple arithmetic expression doesn't compute in PL/pgSQL 9.3.5
(1 answer)
Closed 6 months ago.
I´m trying to do the select function of the image below, but the result for "perc_vol_lei" is zero, that is incorrect.
select to_char (tb_esq.datahora, 'yyyy-mm-dd') as data_esq, tb_esq.tick, tb_esq.variacao, tb_esq.quantidade as qtt_26, tb_dir.quantidade as qtt_0,
(tb_esq.quantidade - tb_dir.quantidade) as volume_no_leilao,
((tb_esq.quantidade - tb_dir.quantidade)/tb_esq.quantidade) as perc_vol_lei
from tb_registros as tb_esq
join tb_registros as tb_dir on tb_esq.tick = tb_dir.tick
and to_char(tb_esq.datahora,'yyyy-mm-dd') = to_char(tb_dir.datahora,'yyyy-mm-dd')
where (tb_esq.quantidade - tb_dir.quantidade) > 50000 and tb_esq.refer =26 and tb_dir.refer = 0
AND tb_esq.tick <>'DOLFUTV' and tb_esq.tick <>'IBOV' and tb_esq.tick <>'WDOFUT'and tb_esq.tick <>'DOLFUT'
order by volume_no_leilao desc`
Anyone can help me?
Related
This question already has an answer here:
Error -104 creating Firebird stored procedure
(1 answer)
Closed 1 year ago.
I want to create a stored procedure in Firebird:
CREATE PROCEDURE CalcPvIncome
( BeginDate date,
EndDate date,
KwPrice decimal (2,2) )
RETURNS ( Total_PV_Production decimal (9,2),
Total_Income decimal (9,2) )
AS
BEGIN
FOR SELECT SUM(ENERGY/1000), SUM((ENERGY/1000) * :KwPrice)
FROM PVPROD
WHERE proddate >= :BeginDate AND proddate <= :Enddate
INTO :Total_PV_Production , :Total_Income
DO
BEGIN
SUSPEND ;
END
END
I get this error:
Engine Code : 335544569
Engine Message : Dynamic SQL Error SQL error code = -104 Unexpected end of command - line 18, column 9
The SQL statement:
SELECT
SUM(ENERGY/1000) AS Total_PV_Production,
sum((ENERGY/1000)*0.55) as Total_Income
FROM
PVPROD
where
proddate >= '12.06.2012' and proddate <= '12.07.2012'
You have to add SET TERM statement before and after the stored procedure. It is used to change the "terminator character". Here's an example:
SET TERM ^ ;
CREATE PROCEDURE CalcPvIncome
( BeginDate date,
EndDate date,
KwPrice decimal (2,2) )
RETURNS ( Total_PV_Production decimal (9,2),
Total_Income decimal (9,2) )
AS
BEGIN
...
END
SET TERM ; ^
Note that default terminator is ^ and also note that you are setting ; as new terminator before and resetting it back to ^ after stored procedure declaration.
This question already has answers here:
How to select the first row of each group?
(9 answers)
Closed 4 years ago.
I have a table in remote SQL database.
CUSTOMERID ACCOUNTNO VEHICLENUMBER TAGSTARTEFFDATE
20000000 10003014 MH43AJ411 2013-06-07 13:07:13.210
20000001 10003014 MH43AJ411 2014-08-08.19:10:11.519
20029961 10003019 GJ15CD7387 2016-07-28 19:21:54.173
20009020 10003019 GJ15CF7747 2016-05-25 18:46:55.947
20001866 10003019 GJ15CD7657 2015-07-11 15:17:14.503
20001557 10003019 GJ15CB9601 2016-05-05 16:45:58.247
20001223 10003019 GJ15CA7837 2014-06-06 14:57:42.583
20000933 10003019 MH02DG7774 2014-02-12 13:49:31.427
20001690 10003019 GJ15CD7477 2015-01-03 16:12:59.000
20000008 10003019 GJ15CB727 2013-06-17 12:36:01.190
20001865 10003019 GJ15CA7387 2015-06-24 15:01:14.000
20000005 10003019 MH02BY7774 2013-06-15 12:29:10.000
I want to export as JSON and this is the code snippet.
val jdbcSqlConnStr = s"jdbc:sqlserver://192.168.70.15;databaseName=$db;user=bhaskar;password=welcome123;"
val jdbcDbTable = table1
val jdbcDF = sqlContext.read.format("jdbc").options(Map("url" -> jdbcSqlConnStr,"dbtable" -> jdbcDbTable)).load()
//jdbcDF.show(10)
//jdbcDF.printSchema
val query = "SELECT ACCOUNTNO, collect_set(struct(`VEHICLENUMBER`, `CUSTOMERID`, `TAGSTARTEFFDATE`)) as VEHICLE FROM tp_customer_account GROUP BY ACCOUNTNO ORDER BY ACCOUNTNO"
jdbcDF.registerTempTable("tp_customer_account")
val res00 = sqlContext.sql(query.toString)
// res00.show(10)
res00.coalesce(1).write.json("D:/res15")
Issue:
But here the problem is that I am getting multiple VEHICLENUMBER because more than one TAGSTARTEFFDATE along with the same VEHICLENUMBER is present in the table.
Want to achieve:
So I want to retrieve the TAGSTARTEFFDATE which is maximum date for the same VEHICLENUMBER. I want to use SparkSQL query using SQLContext as I have given in the code snippet.
You can use Window functions with dense_rank() that goes something like this
val windowSpec = Window.partitionBy(col("VEHICLENUMBER")).orderBy(col("TAGSTARTEFFDATE").desc)
jdbcDF.withColumn("rank", dense_rank().over(windowSpec)).filter(col("rank") === 1).drop(col("rank"))
At the moment I'm not really sure how to express this logic with pure SQL syntax but if you are not restricted to using just SQL you can utilize this snippet.
Edit
Took help of a friend to get a SQL equivalent of above. Try if it works.
SELECT * FROM tp_customer_account WHERE dense_rank() over(partition by VEHICLENUMBER order by TAGSTARTEFFDATE) = 1
This question already has answers here:
Double colon `::` notation in SQL
(4 answers)
Closed 8 years ago.
I have below query which works properly but don't know meaning and use of :: in postgresql.
select (
select (
case when 1 > 0 then 1::float / (
select count(id) from transactions_products where transaction_id in (
select id from transactions_transactions tt
where status = 3 and fi = 355
and (invoice_date >= 1420754400)
and (invoice_date <= 1421099999)
and (tt.division_id = 107)
and (tt.department_id = 210)
) and is_vehicle = 1
)::float else 0 end)
limit 1) as f_4
:: is the cast operator for PostgreSQL.
Short, no-hassle answer: It casts the value to a float.
I am having a table in following structure.
_________________________________
|| ExpObjkey Exp1 Exp2 operator||
________________________________
1 2 3 +
2 4 5 +
3 6 7 -
I want to have records in the following order:
for expObjKey=1, we will have
ExpObjKey Expression
1 (4+5)+(6-7)
explanation:
for ExpObjKey 1 we will have 2 +3
then 2 will have 4+5
and 3 will have 6+7.
Its more like a hierarchy.
I have tried lots of possible ways but no near the solution.
SELECT expObjkey, SYS_CONNECT_BY_PATH(exp1||' ' ||operator|| exp2||')', ' ( ') "Path"
FROM bpmn_expression
CONNECT BY PRIOR
exp1=expObjkey or exp2=expObjkey
start with expObjkey=1
I don't think you can do this with a hierarchical query, but I'd be interested to be proven wrong. You'd need to start at the bottom of the tree and work your way up to allow the substitutions to happen; but then there doesn't seem to be a way to combine the partial expressions that produces. It might be possible with recursive subquery factoring, but that isn't available until 11gR2.
On 10g you could use your own recursive function to generate what you need:
create or replace function get_expression(p_key bpmn_expression.expobjkey%type)
return varchar2 is
row bpmn_expression%rowtype;
begin
select * into row from bpmn_expression where expobjkey = p_key;
return '(' || get_expression(row.exp1)
|| row.operator || get_expression(row.exp2) || ')';
exception
when no_data_found then
return to_char(p_key);
end;
/
select get_expression(1) as expression from dual;
EXPRESSION
------------------------------
((4+5)+(6-7))
SQL Fiddle.
You can strip the outer parentheses with trim or regexp_replace if you want to, but they may be acceptable.
If you add another layer, say a record with values 7, 8, 9, '*', this would give:
EXPRESSION
------------------------------
((4+5)+(6-(8*9)))
SQL Fiddle.
But this isn't going to be very efficient against a large data set, since it will do a lot of single-row look-ups.
This question already has answers here:
lastInsertId does not work in Postgresql
(4 answers)
Closed 8 years ago.
I'm trying to wrap my head around PDO. How come $lastid doesn't output anything?
function renderRoot($db){
$sql = "INSERT INTO nodes (name) VALUES ('/');";
$response = $db->query($sql);
$lastid = $db->lastInsertId();
echo $lastid;
return;
}
The code adds a value to the table and it has an column called id, that aoutincrements.
Here is my sql(postgresql):
$nodetable = "create table nodes (
id serial primary key,
parentid integer references nodes(id ),
name varchar
);";
In postgresql, lastInsertId() take an argument, in your case, the name of the sequence, i.e. nodes_id_seq
$lastid = $db->lastInsertId('nodes_id_seq');
Otherwise it will return the last oid, if any.