I want to declare a variable called average, what should be the average of the completiondate-creationdate of the items in the todolist.
however, if I use these lines of code, mysql tells me that I have an error in my syntax.
Can somebody tell me what I should change to store the correct value in #average?
SET #average = AVG(todoitem.completiondate-todoitem.creationdate) from todolist
right join todoitem on todoitem.id=todolist.id
;
SELECT name FROM todolist
right join todoitem on todoitem.id=todolist.id
WHERE (todoitem.completiondate-todoitem.creationdate > average)
;
You are missing a select in the first query:
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todolist right join
todoitem
on todoitem.id = todolist.id;
Although I'm leaving it in, the join seems unnecessary. Why are you doing the join if you are using only one table?
select #average := AVG(todoitem.completiondate - todoitem.creationdate)
from todoitem;
You have to add SELECT:
SET #average = (SELECT AVG(todoitem.completiondate-todoitem.creationdate))
from todolist
right join todoitem on todoitem.id=todolist.id
-- or
SELECT AVG(todoitem.completiondate-todoitem.creationdate) INTO #average
from todolist
right join todoitem on todoitem.id=todolist.id
Related
I have the following query:
select coalesce(round(sum(vdamesatual) /
(select count(*)
from feriados
where month(data)=month(current_date) and
day(data)<day(current_date) and
diadasemana in(2,3,4,5,6) and
feriadonacional=0 and
uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))
*
(select count(*)
from feriados
where month(data)=month(current_date) and
day(data)>=day(current_date) and
diadasemana in(2,3,4,5,6) and
feriadonacional=0 and
uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))),0) as projecao
from baseprodutos
where Empresa = 'test' and
UFDest = 'uf' and
Fabricante = 'sample';
As you can see I have a coalesce right after the first select, and I am having trouble finding the right way to write this with sequelize; Would you have some suggestion?
I don't really follow the logic of your query, but the rule in Sequelize is that you can call any function with sequelize.fn() so a simple example of using COALESCE in Sequelize to get a value from 2 columns with an alias would be like this in your attributes:
[[sequelize.fn('COALESCE', mysql.col('col_name'), mysql.col('col_2_name')), 'alias'], 'another_col']
You can include another function if needed, like SUM by doing like this:
[[sequelize.fn('COALESCE', sequelize.fn('SUM', (mysql.col('col_name'), mysql.col('col_2_name'))), (some other code here ...)),'alias']]
I am trying to make a stored procedure like this
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg order by stationsNavn,city
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
With "as navn,id" i can create it, but i dont get anything, and with "order by stationsNavn,city" i get an error on create. It works fine without any of them like this.
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station;
end
What am i doing wrong?
try this out, I moved the order by and did it by navn and id
BEGIN
SELECT concat(stationsNavn,' ',city,' ',adresse,' ',postnummer)as navn,id
from AdresseListeBioAnlæg
inner join bioAlarmer on AdresseListeBioAnlæg.id = bioAlarmer.station
order by navn, id
end
I have a view with the following definition:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` +
sum(`cb_trans_detail`.`debito_partida`)) -
sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta`
on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`)
and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`)
and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`))))
join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`)
and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`))))
join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`)
and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`))))
left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` =
`cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1')
group by concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,
`cb_trans_detail`.`codigo_cuenta`)
If I select from this view as follows:
SELECT
`balance_general_view`.`numero_partida`,
`balance_general_view`.`fecha_partida`,
`balance_general_view`.`concepto_partida`,
`balance_general_view`.`nombre_cuenta`,
`balance_general_view`.`codigo_mayor`,
`balance_general_view`.`nombre_mayor`,
`balance_general_view`.`categoria`,
`balance_general_view`.`nombre`,
`balance_general_view`.`presentacion`,
`balance_general_view`.`codigo_cuenta`,
`balance_general_view`.`Debitos`,
`balance_general_view`.`Creditos`,
`balance_general_view`.`saldo_inicial`,
`balance_general_view`.`Saldo`,
`balance_general_view`.`Codigo`
FROM
`balance_general_view`
WHERE
`balance_general_view`.`fecha_partida` BETWEEN '2014-01-01' AND '2014-01-31'
this yields a different result than if I execute the query as follows:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` + sum(`cb_trans_detail`.`debito_partida`)) - sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`) AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta` on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = `cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1') and `cb_trans_head`.`fecha_partida` BETWEEN '2014-01-01' and '2014-01-31'
group by
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
My question is: How to get the result I need using the view and filtering programatically instead of hard-coding the where condition? Thank you. If you need the individual table definitions let me know. Much appreciated.
If you use a left join to a table X and a condition in the WHERE-clause to this table X, you change your left join to an inner one. If you want to restrict the results by the values of this left joined table, you must use this condition in the ON clause of the left join instead:
...
LEFT JOIN
cb_trans_head
ON
cb_trans_detail.numero_partida = cb_trans_head.numero_partida
AND
cb_trans_head.fecha_partida BETWEEN '2014-01-01' and '2014-01-31'
...
If there's another one that I overlook, tread it the same way.
code = 00000000005555
2nd option code = 00000000000555
hi i am try to find out same function like ltrim() of php
SELECT * FROM db1.stock JOIN db2.prodinfo ON replace(db2.prodinfo.code,0000000000,'') = replace(db1.stock.code,0000000000,'') WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'
i rum this temporary. becuse zero might be increase and decrease i
example above. i am just want to remove zero in this query
thanks
try by casting it into numeric,
SELECT *
FROM db1.stock JOIN db2.prodinfo ON
CAST(db2.prodinfo.code AS SIGNED) = CAST(db1.stock.code AS SIGNED)
WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'
or
SELECT *
FROM db1.stock JOIN db2.prodinfo ON
CAST(db2.prodinfo.code AS DECIMAL(15,0)) = CAST(db1.stock.code AS AS DECIMAL(15,0))
WHERE db1.stock.InvNo ='12' and db2.prodinfo.Cat = 'super'
I want to use a Select query from mysql database in C:
mysql_query(conn,"SELECT SI AS SUBSCRIBER_ID ,TG2 AS TAG_ID, SUM(CTR) AS NBR FROM (SELECT H.SUBSCRIBER_ID AS SI, TG.TAG_ID AS TG1,T.TAG_ID AS TG2, COUNT(TG.TAG_ID) AS COUNTER,CASE WHEN (TG.TAG_ID = T.TAG_ID) THEN COUNT(TG.TAG_ID) ELSE 0 END AS CTR from content_hits H left join CONTENT_TAG TG ON TG.CONTENT_ID = H.CONTENT_ID LEFT JOIN TAG T ON 1= 1 GROUP BY H.SUBSCRIBER_ID, TG.TAG_ID,T.TAG_ID) AS TAB GROUP BY SI,TG2");
After that, I want to use 'NBR' to fill an array of one dimension.
I tried this:
result = mysql_store_result(conn);
while ((row = mysql_fetch_row(result)))
{
t[i]=*row['NBR'];
printf("%d",t[i]);
}
But it didn't work.
You cannot access the row columns by name like you have t[i]=*row['NBR'];. Use for example fields = mysql_fetch_fields(result); to get the column names and iterate through the fields array to find which column id 'NBR' has. This id can then be used in t[i]=row[id];. This is all in the mysql connectors doc http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-fields.html