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.
Related
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?
Given a 2d array
select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]);
{{1,2,3},{4,0,0},{7,8,9}}
Is there a way to replace the slice at [2:2][2:] (the {{0,0}}) with values 5 and 6? array_replace replaces a specific value so I'm not sure how to approach this.
I believe it's more readable to code a function in plpgsql. However, a pure SQL solution also exists:
select (
select array_agg(inner_array order by outer_index)
from (
select outer_index,
array_agg(
case
when outer_index = 2 and inner_index = 2 then 5
when outer_index = 2 and inner_index = 3 then 6
else item
end
order by inner_index
) inner_array
from (
select item,
1 + (n - 1) % array_length(a, 1) inner_index,
1 + (n - 1) / array_length(a, 2) outer_index
from
unnest(a) with ordinality x (item, n)
) _
group by outer_index
)_
)
from (
select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]) a
)_;
I need to update all records that match my criteria. But the Sql below is giving this error:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
-- Set MasterAccountId = NULL where there is no Receivable with equivalent BillingAccountId and TaskAccountId
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
WHERE EXISTS ( SELECT * FROM MasterAccount M
WHERE (ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
Basically, I need to update all records that return in that subquery.
Does any one know how to fix it?
Can you give a try on this. This is base from the repond of https://stackoverflow.com/users/40655/robin-day on this link How do I UPDATE from a SELECT in SQL Server?.
UPDATE
R
SET
R.MasterAccountId = NULL
FROM
Receivable R
INNER JOIN
MasterAccount M
ON
(ISNULL(M.BillingAccountId, 0) > 0 AND M.BillingAccountId = R.BillingAccountId) OR
(ISNULL(M.TaskAccountId, 0) > 0 AND M.TaskAccountId = R.TaskAccountId))
I don't think you are getting the said error in posted query May be somewhere else. Again in your EXISTS subquery, instead of saying select * ... it's always better to say WHERE EXISTS ( SELECT 1 FROM MasterAccount M
Also try using the JOIN version of this query instead like
UPDATE R
SET R.MasterAccountId = NULL
FROM Receivable R
JOIN MasterAccount M ON M.BillingAccountId = R.BillingAccountId
OR M.TaskAccountId = R.TaskAccountId
WHERE ISNULL(M.BillingAccountId, 0) > 0
OR ISNULL(M.TaskAccountId, 0) > 0;
I'm trying to take data from one column, MyTable.SSN, and copy it to another in the same table, MyTable.SSNWithDashes, just formatted differently. If MyTable.SSN doesn't have exactly 9 digits, I don't care to process it at all.
I've tried this:
IF( SELECT LEN( [SSN] ) FROM [MyTable] ) = 9
UPDATE [MyTable] SET [SSNWithDashes] = LEFT( [SSN], 3 ) + '-' + SUBSTRING( [SSN], 4, 2 ) + '-' + RIGHT( [SSN], 4 )
ELSE
UPDATE [MyTable] SET [SSNWithDashes] = NULL
While this works, it throws an error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
While I do understand what the warning is saying, I'm not really sure how to go about this differently.
How can I refactor this to remove that warning (and perhaps read a little cleaner)?
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = CASE
WHEN LEN(SSN) = 9 THEN
LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
ELSE NULL
END;
Assuming your SSNWithDashes is already NULL then
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
WHERE LEN(SSN) = 9
Every other rows remain NULL
In my query I want to add the and clause in query if a specific condition occur.
See the where clause of my query
1 and ps.dept=dept.deptid
2 and ps.sdept=deptsub.deptid
3 and
4 if(ps.dept<>ps.sdept)
5 begin
6 deptsub.Parent=dept.deptid
7 end
8 and ps.deptcategory=deptcat.category
At 4 I want if condition fulfil then 6 should be added in query else not how is this possible.thanks!
How about:
and ps.dept=dept.deptid
and ps.sdept=deptsub.deptid
and ( ps.dept=ps.sdept or deptsub.Parent=dept.deptid )
and ps.deptcategory=deptcat.category
Try replacing lines 4, 5, 6, and 7 with this:
deptsub.Parent =
case when ps.dept <> ps.sdept then dept.deptid
else deptsub.Parent
end
The case statement will substitute dept.deptid when your condition is met, otherwise it will just substitute deptsub.parent - which will always = deptsub.parent
and ps.dept=dept.deptid
and ps.sdept=deptsub.deptid
and
(
((ps.dept<>ps.sdept) and (deptsub.Parent=dept.deptid))
or
(ps.dept = ps.sdept)
)
and ps.deptcategory=deptcat.category