PostGIS ST_Distance discrepancy between GeogFromText and Point - postgis

SELECT t.coords, t2.coords,
ST_Distance(ST_GeogFromText('SRID=4326;POINT(-115.83225 44.18711)'), ST_GeogFromText('SRID=4326;POINT(-105.83225 54.18711)')) AS d1,
ST_Distance(ST_Point(t.coords[2], t.coords[1]), ST_Point(t2.coords[2], t2.coords[1])) AS d2
FROM locations
INNER JOIN locations t2 ON t2.id = 1
WHERE t.id = 2;
t.coords = "{44.18711,-115.83225}" and t2.coords = "{54.18711,-105.83225}"
d1 is returned as 1326899.86792105 and d2 as 14.142135623731.
Why is that? Thanks!

Seems I have to cast the ST_Point into a geography type, then it works.

Related

Update does not add the correct values

My select statement returns the ids from the tables I have joined correctly(393, starting at 92 and going up 484, its not a PK field, so some ids can be used more then once) there is a total of 550 rows I need to update, when I run the update it only adds the number 92 to the table for all 550 rows.
update movements set [location] = locaID.id FROM (
SELECT
lo.id
FROM [Harvest_Transactions] ht
left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id
left join [batches] b on b.name = hms.Batch_id
left join [phases] p on p.batch = b.id and p.[type] = 3
left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and lo.Bay_id = gt.Bay_id
where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05 09:33:00.000' OR hms.CreatedOn is null )
)locaID where product = 14
what am I missing so it updates with the correct values?
My first impression of your query is that only the rows with product = 14 in movements table will be updated with the single value coming from the subquery.
If you want to update the values based on another derived table, you need some way to link the rows together - i.e a JOIN between your table being updated and the table that holds the other data (see here )
What is the common column between movements and
SELECT
lo.id
FROM [Harvest_Transactions] ht
left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id
left join [batches] b on b.name = hms.Batch_id
left join [phases] p on p.batch = b.id and p.[type] = 3
left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and
lo.Bay_id = gt.Bay_id
where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05
09:33:00.000' OR hms.CreatedOn is null )
The rough syntax shouldI think be
UPDATE movements
set [location] = locaID.id
FROM movements
JOIN (
SELECT
lo.id
FROM [Harvest_Transactions] ht
left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id
left join [batches] b on b.name = hms.Batch_id
left join [phases] p on p.batch = b.id and p.[type] = 3
left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and
lo.Bay_id = gt.Bay_id
where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05
09:33:00.000' OR hms.CreatedOn is null )
) locaID
ON movements.<some column> = locaID.<some column>

Avoid table function in where clause?

I have added table function in the where clause.
select cmp_id, acno_code, sl_type, sl_code, 0 op_Dr, 0 op_cr, 0 tr_Dr, sum(amount) tr_Cr
from vf_finance
where cmp_id =#cmp_id1
and unitcode in (select * from UTILfn_Split( #unit_code,',') )
and stat_code in ('AT','PR' )
--and pc_code in (select * from UTILfn_Split( #sba,',') )
AND DOC_dT >=convert(datetime,#from_date,103) and doc_Dt <= convert(datetime,#to_date,103)
and amount < 0
GROUP BY cmp_id, acno_code, sl_type, sl_code
) as gl
inner join ps_Accmas acc on acc.cmp_id = gl.cmp_id and acc.acno_Code = gl.acno_code
inner join ps_owner o on gl.cmp_id = o.cmp_id
left outer join view_sl_code sl on gl.cmp_id = sl.cmp_id and gl.sl_type = sl.sl_type and gl.sl_Code = sl.sl_Code
inner join ps_slType slt on gl.cmp_id = slt.cmp_id and gl.sl_Type = slt.sl_type
where sl.sl_type in (select * from UTILfn_Split( #sl_type,',') )
and acc.acno_code in(select * from UTILfn_Split( #facno_code,',') )
group by gl.cmp_id, gl.acno_code,gl.sl_code,gl.sl_type,slt.sl_DEsc,acc.acno_DEsc, sl.sl_DEsc, o.owner_name
order by gl.cmp_id, gl.acno_code,gl.sl_code,gl.sl_type
Can anyone please suggest how I can avoid function in where clause?
You may try this. There are some issues in this existing query which I'll point first
First unitcode in (select * from UTILfn_Split( #unit_code,',') here you must use one column name instead of *, although i don't know about your function UTILfn_Split but still mention column name is preferable.
for your query you may use inner join instead of in with function having return type table.
Instead of
sl.sl_type in (select * from UTILfn_Split( #sl_type,',') )
You may try this
yourtble as sl inner join
(select value from UTILfn_Split( #sl_type,',') as t2
on sl.sl_type = t2.[value] ---- here column name with t2 depends on your function,
---what table structure is returning, in your case it is [value]

sql OR with like queries taking forever to return result

This is my query with OR LIKE in the where clause.
SELECT DISTINCT TOP 10 coalesce(cl2desc, hmdesc, gendesc, procdesc) as description,acctcode,uacs FROM
hpatchrg as t4 INNER JOIN
hcharge AS t5 ON t4.chargcode = t5.chrgcode LEFT JOIN
hmisc AS t6 ON t6.hmcode = t4.itemcode AND t5.chrgtable = 'MISCE' LEFT JOIN
hdmhdr AS t7 ON (t7.dmdcomb +''+ convert(varchar, t7.dmdctr)) = t4.itemcode AND t5.chrgtable = 'DRUGS' LEFT JOIN
hdruggrp AS t8 ON t7.grpcode = t8.grpcode LEFT JOIN
hgen AS t9 ON t9.gencode = t8.gencode LEFT JOIN
hprocm AS t10 ON t4.itemcode = t10.proccode AND t5.chrgtable = 'EXAMI' LEFT JOIN
hclass2 AS t11 ON t4.itemcode = t11.cl2comb AND t5.chrgtable = 'NONDR'
WHERE
(cl2desc LIKE '%cbc%') OR
(hmdesc LIKE '%cbc%') OR
(gendesc LIKE '%cbc%') OR
(procdesc LIKE '%cbc%')
This takes forever to return anything but when I do:
SELECT DISTINCT TOP 10 coalesce(cl2desc, hmdesc, gendesc, procdesc) as description,acctcode,uacs FROM
hpatchrg as t4 INNER JOIN
hcharge AS t5 ON t4.chargcode = t5.chrgcode LEFT JOIN
hmisc AS t6 ON t6.hmcode = t4.itemcode AND t5.chrgtable = 'MISCE' LEFT JOIN
hdmhdr AS t7 ON (t7.dmdcomb +''+ convert(varchar, t7.dmdctr)) = t4.itemcode AND t5.chrgtable = 'DRUGS' LEFT JOIN
hdruggrp AS t8 ON t7.grpcode = t8.grpcode LEFT JOIN
hgen AS t9 ON t9.gencode = t8.gencode LEFT JOIN
hprocm AS t10 ON t4.itemcode = t10.proccode AND t5.chrgtable = 'EXAMI' LEFT JOIN
hclass2 AS t11 ON t4.itemcode = t11.cl2comb AND t5.chrgtable = 'NONDR'
WHERE
(cl2desc LIKE '%cbc%')
also tried changing the (cl2desc LIKE '%cbc%') with other value
I get result very fast.
So i assume that the problem is with the OR LIKE in the where clause
What is the correct way to do OR with LIKE in the where clause
Too long for comment.
Well, for each column you have In the where clause it has to do a match on every single row. So take sum if the number of rows for each table in the where clause... that's how many times it has to check your logic. Indexes can't be used unless you do a full text index and use something like CONTAINS. I'd expect this query to be slow.
Only using a trailing % means you are searching for where the column value STARTS with the value you supplied. Thus, the engine doesn't have to check all possible locations in a column.
Also using TOP with out an ORDER BY doesn't guarantee the result set returned will be the same each time. So, not using an ORDER BY means you don't care which 10 rows or whatever number is returned.

Data from same table with different where clauses into same view

I'm trying to get two kinds of data from same table into a view. I tried adding a join to same table with a different alias but it caused some null values.
I want the datatable as a single row , if possible. I know this can be done with union operator too but it causes multiple rows.
Here is my select
SELECT
COUNT(PO.OrderID) AS MerkezOnaySiparisAdet, SUM(PO.TotalPromotionPrice) AS MerkezOnayToplamSatis,
COUNT(PO2.OrderID) AS DepoOnaySiparişAdet, SUM(PO2.TotalPromotionPrice) AS DepoOnayToplamSatis
FROM PrmOrder PO
LEFT JOIN PrmOrder PO2 ON PO.OrderID = PO2.OrderId AND (PO2.OrderStatusID != 1 AND PO2.IsOrderApproved = 1 AND PO2.IsInventoryApproved = 0 AND (PO2.OrderStatusID = 2 OR PO2.OrderStatusID = 3))
WHERE 1=1
AND PO.OrderStatusID != 1
AND PO.IsOrderApproved = 0
AND (PO.OrderStatusID = 2 OR PO.OrderStatusID = 3)
What is the correct way of doing this?
Maybe, this isn't the best way, but you can use subquery. For example
SELECT
A1.Sum1,
A2.Sum2 FROM
(
SELECT
SUM(sod.OrderQty) sum1,
1 joinnumber
FROM
Sales.SalesOrderDetail sod
WHERE
sod.ProductID = 772
) AS A1
JOIN (
SELECT
SUM(sod.OrderQty) sum2,
1 joinnumber
FROM
Sales.SalesOrderDetail sod
WHERE
sod.ProductID = 773
) AS A2
ON A1.joinnumber = A2.joinnumber

Merge data when inserting into another table with the same ID

I have a table with the following data, the lab_sample_id are identical, I’m inserting this data into another table. I would like to merge the data when inserting into another table if the lab_sample_id is the same for leg_id 1 and 3.
leg_id 1 & 3 will always have unique data but the lab_sample_id might be identical.
Here's my current code:
INSERT INTO SAMPLE_RESULT
(PPK, LAB_SAMPLE_ID, LEG_ID, PT, PT_METHOD,
PT_MIN_DETECTION, CU, CU_METHOD, CU_MIN_DETECTION)
SELECT B.PK, CSR.LAB_SAMPLE_ID, CSR.LEG_ID, CSR.PT, CSR.PT_METHOD,
CSR.PT_MIN_DETECTION, CSR.CU, CSR.CU_METHOD, CSR.CU_MIN_DETECTION
FROM [SOURCE] S
JOIN CLEARING_BATCH CB ON CB.PPK = S.PK
JOIN CLEARING_SAMPLE_RESULT CSR ON CSR.PPK = CB.PK
JOIN SAMPLE_REG SR ON SR.FIELD_SAMP_ID = CSR.LAB_SAMPLE_ID
LEFT JOIN PROSPECTS_AND_MINES PM ON PM.PPK = S.PK
LEFT JOIN LABORATORY L ON L.PPK = PM.PK
LEFT JOIN BATCH B ON L.PK = B.PPK
WHERE CB.PK =#Submission_Pk
AND CB.BATCH_ID = B.BATCH_ID
AND NOT EXISTS(SELECT *
FROM SAMPLE_RESULT SR
WHERE SR.PPK = B.PK
AND SR.LAB_SAMPLE_ID = CSR.LAB_SAMPLE_ID
AND SR.LEG_ID = CSR.LEG_ID)
Something like this should do the trick, where you can JOIN the table to itself:
SELECT t1.LAB_SAMPLE_ID,
t1.LEG_ID,
t1.PT,
t1.PT_METHOD,
t1.PT_MIN_DETECTION,
t2.cu,
t2.CU_METHOD,
t2.CU_MIN_DETECTION
FROM CLEARING_SAMPLE_RESULT t1
INNER JOIN CLEARING_SAMPLE_RESULT t2
ON t1.LAB_SAMPLE_ID = t2.LAB_SAMPLE_ID
WHERE t1.LAB_SAMPLE_ID = 1 AND t2.LAB_SAMPLE_ID = 3
Then you can modify your UPDATE logic to use this data.

Resources