I have a table for polygon_geoms and marker_geoms.
How do I retrieve polygons that have markers within them.
I have tried:
SELECT A.geom
FROM polygon_geoms A
LEFT JOIN marker_geoms B
ON ST_Within(B.geom, A.geom);
But all I get is an empty result.
On a sample geometry set like this:
Your query with slight modifications returns proper result (polygon id's A, D):
select p1.id
from (values (st_geomfromtext('Point (34.9796 48.4359)')),
(st_geomfromtext('Point (34.9821 48.4354)')),
(st_geomfromtext('Point (34.9837 48.4337)')),
(st_geomfromtext('Point (34.9902 48.4353)')),
(st_geomfromtext('Point (34.9956 48.4352)')),
(st_geomfromtext('Point (34.9888 48.4288)')),
(st_geomfromtext('Point (34.9907 48.4202)')),
(st_geomfromtext('Point (34.9952 48.4213)')),
(st_geomfromtext('Point (34.9994 48.4212)')),
(st_geomfromtext('Point (34.9980 48.4191)')),
(st_geomfromtext('Point (35.0002 48.4178)')),
(st_geomfromtext('Point (35.0021 48.4260)'))) m1(geom)
join (values ('A', st_geomfromtext('Polygon ((34.9772 48.4351, 34.9798 48.4320, 34.9847 48.4329, 34.9850 48.4356, 34.9825 48.4374, 34.9803 48.4382, 34.9783 48.4368, 34.9772 48.4351))')),
('B', st_geomfromtext('Polygon ((34.9949 48.4305, 34.9954 48.4273, 34.9985 48.4260, 35.0033 48.4281, 35.0035 48.4309, 35.0013 48.4333, 34.9968 48.4335, 34.9949 48.4305))')),
('C', st_geomfromtext('Polygon ((34.9847 48.4249, 34.9832 48.4209, 34.9862 48.4192, 34.9887 48.4201, 34.9893 48.4218, 34.9896 48.4236, 34.9887 48.4257, 34.9847 48.4249))')),
('D', st_geomfromtext('Polygon ((34.9960 48.4199, 34.9972 48.4165, 35.0008 48.4168, 35.0022 48.4210, 34.9984 48.4231, 34.9960 48.4199))'))) p1(id, geom)
on st_within(m1.geom, p1.geom)
group by p1.id
So, I'm guessing, it's either something is wrong with your geometries or they do not overlap in deed.
Related
I get the error Incorrect syntax near ')' from the following SQL query:
SELECT
"GLPOST"."ACCTID",
"GLPOST"."FISCALYR",
"GLPOST"."FISCALPERD",
"GLPOST"."SRCELEDGER",
"GLPOST"."JRNLDATE",
"GLPOST"."BATCHNBR",
"GLPOST"."ENTRYNBR",
"GLPOST"."JNLDTLDESC",
"GLPOST"."JNLDTLREF",
"GLPOST"."TRANSAMT",
"APIBC"."POSTSEQNBR",
"APIBC"."CNTBTCH"
FROM ("MHLDAT"."dbo"."GLPOST" "GLPOST"
INNER JOIN "MHLDAT"."dbo"."GLJEH" "GLJEH"
ON (("GLPOST"."DRILSRCTY"="GLJEH"."DRILSRCTY")
AND ("GLPOST"."DRILLDWNLK"="GLJEH"."DRILLDWNLK")
AND "GLPOST"."DRILAPP"="GLJEH"."DRILAPP")))
INNER JOIN "MHLDAT"."dbo"."APIBC" "APIBC"
ON "APIBC"."POSTSEQNBR" = (CAST ("SUBSTRING" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),3,CAST ("LEFT" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),1) AS "INT" )) AS "INT" ))
WHERE
"GLPOST"."SRCELEDGER"=N'AP' AND "GLPOST"."FISCALYR"=N'2021' AND "GLPOST"."FISCALPERD"=N'01' AND "GLJEH"."ERRBATCH"=0
Any suggestions to resolve please?
SELECT
GLPOST.ACCTID,
GLPOST.FISCALYR,
GLPOST.FISCALPERD,
GLPOST.SRCELEDGER,
GLPOST.JRNLDATE,
GLPOST.BATCHNBR,
GLPOST.ENTRYNBR,
GLPOST.JNLDTLDESC,
GLPOST.JNLDTLREF,
GLPOST.TRANSAMT,
APIBC.POSTSEQNBR,
APIBC.CNTBTCH
FROM MHLDAT.dbo.GLPOST
INNER JOIN MHLDAT.dbo.GLJEH
ON GLPOST.DRILSRCTY=GLJEH.DRILSRCTY
AND GLPOST.DRILLDWNLK=GLJEH.DRILLDWNLK
AND GLPOST.DRILAPP=GLJEH.DRILAPP
INNER JOIN MHLDAT.dbo.APIBC
ON APIBC.POSTSEQNBR = CAST(SUBSTRING(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
3,
CAST(LEFT(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
1) AS INT)
) AS INT)
WHERE
GLPOST.SRCELEDGER = N'AP'
AND GLPOST.FISCALYR = N'2021'
AND GLPOST.FISCALPERD = N'01'
AND GLJEH.ERRBATCH = 0
You are using a lot of unnecessary parenthesis and double quoting, remove them (specially on the names of the functions SUBSTRING and LEFT). Remove the aliases (they are unnecessary because you use the same name as the table).
I have aligned your query so you can visually see where starts and ends every subexpression.
I am trying to parse email files that will be coming periodically for data that is contained within. We plan to setup cfmail to get the email within the box within CF Admin to run every minute.
The data within the email consists of name, code name, address, description, etc. and will have consistent labels so we are thinking of performing a loop or find function for each field of data. Would that be a good start?
Here is an example of email data:
INCIDENT # 12345
LONG TERM SYS# C12345
REPORTED: 08:39:34 05/20/19 Nature: FD NEED Address: 12345 N TEST LN
City: Testville
Responding Units: T12
Cross Streets: Intersection of: N Test LN & W TEST LN
Lat= 39.587453 Lon= -86.485021
Comments: This is a test post. Please disregard
Here's a picture of what the data actually looks like:
So we would like to extract the following:
INCIDENT
LONG TERM SYS#
REPORTED
Nature
Address
City
Responding Units
Cross Streets
Comments
Any feedback or suggestions would be greatly appreciated!
Someone posted this but it was apparently deleted. Whoever it was I want to thank you VERY MUCH as it worked perfectly!!!!
Here is the function:
<!---CREATE FUNCTION [tvf-Str-Extract] (#String varchar(max),#Delimiter1
varchar(100),#Delimiter2 varchar(100))
Returns Table
As
Return (
with cte1(N) as (Select 1 From (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))
N(N)),
cte2(N) as (Select Top (IsNull(DataLength(#String),0)) Row_Number() over (Order By
(Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A
),
cte3(N) as (Select 1 Union All Select t.N+DataLength(#Delimiter1) From cte2 t
Where Substring(#String,t.N,DataLength(#Delimiter1)) = #Delimiter1),
cte4(N,L) as (Select S.N,IsNull(NullIf(CharIndex(#Delimiter1,#String,s.N),0)-
S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By N)
,RetPos = N
,RetVal = left(RetVal,charindex(#Delimiter2,RetVal)-1)
From ( Select *,RetVal = Substring(#String, N, L) From cte4 ) A
Where charindex(#Delimiter2,RetVal)>1
)
And here is the CF code that worked:
<cfquery name="body" datasource="#Application.dsn#">
Declare #S varchar(max) ='
INCIDENT 12345
LONG TERM SYS C12345
REPORTED: 08:39:34 05/20/19 Nature: FD NEED Address: 12345 N TEST
LN City: Testville
Responding Units: T12
Cross Streets: Intersection of: N Test LN & W TEST LN
Lat= 39.587453 Lon= -86.485021
Comments: This is a test post. Please disregard
'
Select Incident = ltrim(rtrim(B.RetVal))
,LongTerm = ltrim(rtrim(C.RetVal))
,Reported = ltrim(rtrim(D.RetVal))
,Nature = ltrim(rtrim(E.RetVal))
,Address = ltrim(rtrim(F.RetVal))
,City = ltrim(rtrim(G.RetVal))
,RespUnit = ltrim(rtrim(H.RetVal))
,CrossStr = ltrim(rtrim(I.RetVal))
,Comments = ltrim(rtrim(J.RetVal))
From (values (replace(replace(#S,char(10),''),char(13),' ')) )A(S)
Outer Apply [dbo].[tvf-Str-Extract](S,'INCIDENT' ,'LONG
TERM' ) B
Outer Apply [dbo].[tvf-Str-Extract](S,'LONG TERM SYS'
,'REPORTED' ) C
Outer Apply [dbo].[tvf-Str-Extract](S,'REPORTED:' ,'Nature'
) D
Outer Apply [dbo].[tvf-Str-Extract](S,'Nature:'
,'Address' ) E
Outer Apply [dbo].[tvf-Str-Extract](S,'Address:' ,'City'
) F
Outer Apply [dbo].[tvf-Str-Extract](S,'City:'
,'Responding ') G
Outer Apply [dbo].[tvf-Str-Extract](S,'Responding Units:','Cross'
) H
Outer Apply [dbo].[tvf-Str-Extract](S,'Cross Streets:' ,'Lat'
) I
Outer Apply [dbo].[tvf-Str-Extract](S+'|||','Comments:' ,'|||'
) J
</cfquery>
<cfoutput>
B. #body.Incident#<br>
C. #body.LongTerm#<br>
D. #body.Reported#<br>
SQL tends to have limited string functions, so it isn't the best tool for parsing. If the email content is always in that exact format, you could use either plain string functions or regular expressions to parse it. However, the latter is more flexible.
I suspect the content actually does contain new lines, which would make for simpler parsing. However, if you prefer searching for content in between two labels, regular expressions would do the trick.
Build an array of the label names (only). Loop through the array, grabbing a pair of labels: "current" and "next". Use the two values in a regular expression to extract the text in between them:
label &"\s*[##:=](.*?)"& nextLabel
/* Explanation: */
label - First label name (example: "Incident")
\s* - Zero or more spaces
[##:=] - Any of these characters: pound sign, colon or equal sign
(.*?) - Group of zero or more characters (non-greedy)
nextLabel - Next label (example: "Long Term Sys")
Use reFindNoCase() to get details about the position and length of matched text. Then use those values in conjunction with mid() to extract the text.
Note, newer versions like ColdFusion 2016+ automagically extract the text under a key named MATCH
The newer CF2016+ syntax is slicker, but something along these lines works under CF10:
emailBody = "INCIDENT # 12345 ... etc.... ";
labelArray = ["Incident", "Long Term Sys", "Reported", ..., "Comments" ];
for (pos = 1; pos <= arrayLen(labelArray); pos++) {
// get current and next label
hasNext = pos < arrayLen(labelArray);
currLabel = labelArray[ pos ];
nextLabel = (hasNext ? labelArray[ pos+1 ] : "$");
// extract label and value
matches = reFindNoCase( currLabel &"\s*[##:=](.*?)"& nextLabel, emailBody, 1, true);
if (arrayLen(matches.len) >= 2) {
results[ currLabel ] = mid( emailBody, matches.pos[2], matches.len[2]);
}
}
writeDump( results );
Results:
I did ask this question before but deleted the post.
Here is my question: I have a table in SQL Server with multiple events in the table, I am trying to filter on the events and the datetime between the events on a certain X min.
I need help with T-SQL queries.
What can use use to do the following (any links will be appreciated):
take event_id and datetime of device, compare to a list of other event_id's to see if there are any other events withing the last 24 min for that device.
What would work for me? Nested queries? Where clause? CTE?
I have tried 6 queries but not getting the result I need, do I use (max) datetime and then compare on event_ID's?
I know this is not much but any help or links would be appreciated....
On Request (Full Query no changes)
1.
SELECT A.[Unit_id]
,A.[TransDate]
,A.[event_id]
,A.[event_msg]
,B.[TransDate]
,B.[event_id]
,B.[event_msg]
FROM
(Select
A.[Unit_id]
,MAX(A.[TransDate]) AS Transdate
,A.[event_id]
,A.[event_msg]
FROM [JammingEvents].[dbo].[EventLogExtended] AS A
WHERE event_id = '345'
GROUP BY A.[Unit_id]
,A.[event_id]
,A.[event_msg]
) AS A
INNER JOIN
(SELECT
B.[Unit_id]
,MAX(B.[TransDate]) AS Transdate
,B.[event_id]
,B.[event_msg]
FROM [JammingEvents].[dbo].[EventLogExtended] AS B
WHERE B.event_id = '985'
GROUP BY B.[Unit_id]
,B.[event_id]
,B.[event_msg]
) AS B
ON
A.Unit_id = B.Unit_id
WHERE (B.TransDate BETWEEN DATEADD(MINUTE,-24,A.Transdate) AND DATEADD(MINUTE,24,A.Transdate))
this works but only if you compare 2 event Id's. the problem is i need to do this on a bulk scale.
2.
SET NOCOUNT ON
GO
SELECT MAX(ELE.Transdate) AS Transdate
,ELE.[Unit_id]
,ELE.[event_id]
,ELE.[event_msg]
,ELE.[Latitude]
,ELE.[Longitude]
,ELE.[date_ack]
,ELE.[user_id_ack]
,ELE.[date_closed]
,ELE.[user_id_closed]
,ELE.[action]
,ELE.[EventSeqNo]
,ELE.[MsgSeqNo]
,ELE.[Speed]
,ELE.[Heading]
,ELE.[Status1]
,ELE.[Status2]
,ELE.[GeoLocation]
,ELE.[Reg_No]
,ELE.[Company]
,ELE.[Fleet_Code]
,ELE.[IgnStatus]
,ELE.[comboActioned]
FROM
[JammingEvents].[dbo].[EventLogExtended] ELE
INNER JOIN
(
SELECT
F.Transdate ,F.[Unit_id] ,F.[event_id] ,F.[event_msg] ,F.[Latitude] ,F.[Longitude] ,F.[date_ack] ,F.[user_id_ack]
,F.[date_closed] ,F.[user_id_closed] ,F.[action] ,F.[EventSeqNo] ,F.[MsgSeqNo] ,F.[Speed] ,F.[Heading] ,F.[Status1]
,F.[Status2] ,F.[GeoLocation] ,F.[Reg_No] ,F.[Company] ,F.[Fleet_Code] ,F.[IgnStatus] ,F.[comboActioned]
FROM [JammingEvents].[dbo].[EventLogExtended] AS F
WHERE
F.event_id IN
('302'
,'303'
,'304'
,'305'
,'309'
,'340'
,'341'
,'345'
,'962'
,'963'
,'973'
,'974'
,'975'
,'976'
,'985'
,'987'
,'989'
,'C220'
,'C222'
,'C224'
,'C227'
,'C228')
) AS A
ON
ELE.Unit_id = A.Unit_id
INNER JOIN
(
SELECT
G.Transdate ,G.[Unit_id] ,G.[event_id] ,G.[event_msg] ,G.[Latitude] ,G.[Longitude] ,G.[date_ack]
,G.[user_id_ack] ,G.[date_closed] ,G.[user_id_closed] ,G.[action] ,G.[EventSeqNo] ,G.[MsgSeqNo] ,G.[Speed] ,G.[Heading]
,G.[Status1] ,G.[Status2] ,G.[GeoLocation] ,G.[Reg_No] ,G.[Company] ,G.[Fleet_Code] ,G.[IgnStatus] ,G.[comboActioned]
FROM [JammingEvents].[dbo].[EventLogExtended] AS G
WHERE
G.event_id = '345'
) AS B
ON
A.Unit_id = B.Unit_id
WHERE (ELE.TransDate BETWEEN DATEADD(MINUTE,-24,A.Transdate) AND DATEADD(MINUTE,24,A.Transdate))
AND
(ELE.event_id IN
(
'302'
,'303'
,'304'
,'305'
,'309'
,'340'
,'341'
,'345'
,'962'
,'963'
,'973'
,'974'
,'975'
,'976'
,'985'
,'987'
,'989'
,'C220'
,'C222'
,'C224'
,'C227'
,'C228'
))
AND
(ELE.action = '0')
GROUP BY ELE.TransDate
,A.Unit_id
,ELE.[Unit_id]
,ELE.[event_id]
,ELE.[event_msg]
,ELE.[Latitude]
,ELE.[Longitude]
,ELE.[date_ack]
,ELE.[user_id_ack]
,ELE.[date_closed]
,ELE.[user_id_closed]
,ELE.[action]
,ELE.[EventSeqNo]
,ELE.[MsgSeqNo]
,ELE.[Speed]
,ELE.[Heading]
,ELE.[Status1]
,ELE.[Status2]
,ELE.[GeoLocation]
,ELE.[Reg_No]
,ELE.[Company]
,ELE.[Fleet_Code]
,ELE.[IgnStatus]
,ELE.[comboActioned]
ORDER BY ELE.TransDate, ELE.Unit_id DESC
so far this got me the closest but i have no idea what to do, i am very poor with SQL syntax and writing queries.
You could use an exists subquery to demand that there was another event for the same device within the preceeding 24 minutes:
select *
from Events e1
where exists
(
select *
from Events e2
where e2.Device_ID = e1.Device_ID
and e2.event_dt between
dateadd(minute, e1.event_dt, -24)
and e1.event_dt
)
Self join should work as well:
SELECT *, DATEDIFF(minute,b.transdate,a.transdate) diff FROM EventLogWxtended a
JOIN eventLogExtended b
ON (a.unit_id=b.unit_id AND b.transdate<a.transdate
AND DATEDIFF(minute,b.transdate,a.transdate)<24)
I have got the following query, which puts the data that I need (text in the 245, 260 and 300 tags of my table called "bib") into a single column (called "Title"). Instead I would like to have this data separated into three distinct columns, one for each tag. Any suggestions would be very welcome
SELECT DISTINCT isbnEX_inverted.isbn, ISNULL(bib.text, CONVERT(varchar(255), bib_longtext.longtext)) AS Title, top_circ_summary.ranking, bib.tag, bib.bib#
FROM bib INNER JOIN
item ON bib.bib# = item.bib# INNER JOIN
isbnEX_inverted ON bib.bib# = isbnEX_inverted.bib# INNER JOIN
top_circ_summary ON item.bib# = top_circ_summary.bib# LEFT OUTER JOIN
bib_longtext ON bib.bib# = bib_longtext.bib# AND bib.tag = bib_longtext.tag
WHERE (isbnEX_inverted.isbn LIKE '%978__________%') AND (top_circ_summary.collection_group = 'jfic') AND (bib.tag in ('245', '520', '300'))
order by top_circ_summary.ranking
Here's a sample of the (tab separated) output
isbn Title ranking tag bib# 9780143307334 a217 p. :bill. ;c21
cm. 1 300 962366 9780143307334 aDiary of a wimpy kid :bthe third
wheel /cby Jeff Kinney. 1 245 962366 9780143307334 aTrying to find
a partner for the Valentine's Day dance, Greg finds solace in the fact
that his best friend Rowley also doesn't have a date, but an
unexpected twist might turn his night around. 1 520 962366
I have the following xml data in a xml column (not typed) called xml_response
<SEIContent>
<Request>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>0</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.75</v:Rate>
<v:LowerBand>0</v:LowerBand>
<v:UpperBand>249999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.7</v:Rate>
<v:LowerBand>250000</v:LowerBand>
<v:UpperBand>499999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.6</v:Rate>
<v:LowerBand>500000</v:LowerBand>
<v:UpperBand>999999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
<v:TieredCharge>
<v:Term>0</v:Term>
<v:Rate>0.5</v:Rate>
<v:LowerBand>1000000</v:LowerBand>
<v:UpperBand>9999999.99</v:UpperBand>
<v:BandCurrency>GBP</v:BandCurrency>
</v:TieredCharge>
</eq:Charge>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>59</v:EndMonth>
<v:Rate>1.5</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
<eq:Charge>
<v:Type>MaintCharge</v:Type>
<v:Term>0</v:Term>
<v:StartMonth>60</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>0.5</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
<eq:Charge>
<v:Type>QAC</v:Type>
<v:Basis>FixedAmount</v:Basis>
<v:Term>0</v:Term>
<v:StartMonth>0</v:StartMonth>
<v:EndMonth>0</v:EndMonth>
<v:Rate>105</v:Rate>
<v:RebateRatio>0</v:RebateRatio>
<v:MaxRebate>0</v:MaxRebate>
</eq:Charge>
</Request>
<Response>
<eq:Ref>QV00000393</eq:Ref>
</Response>
</SEIContent>
So you'll notice that some charges contain a repeating element TieredCharge and some don't
I've written the following query:
WITH XMLNAMESPACES('http://lu/blah' AS eq, 'http://lu/blah2' AS v,
DEFAULT 'http://lu/blah3'
SELECT
nref.value('Response[1]/eqRef[1]', 'nvarchar(200)') Ref,
ncharge.value('v:Type[1]', 'nvarchar(50)') ChargeType,
ncharge.value('v:Basis[1]', 'nvarchar(50)') ChargeBasis,
ncharge.value('v:Term[1]', 'int') Term,
ncharge.value('v:StartMonth[1]', 'int') StartMonth,
ncharge.value('v:EndMonth[1]', 'int') EndMonth,
ncharge.value('v:Rate[1]', 'money') Rate,
ncharge.value('v:RebateRatio[1]', 'money') RebateRatio,
ncharge.value('v:MaxRebate[1]', 'money') MaxRebate,
tcharge.value('v:Term[1]', 'int') TieredTerm,
tcharge.value('v:Rate[1]', 'money') TieredRate,
tcharge.value('v:LowerBand[1]', 'money') TieredLowerBand,
tcharge.value('v:UpperBand[1]', 'money') TieredUpperBand
INTO #TempCharges
FROM xml_response CROSS APPLY response_body.nodes('//SEIContent') AS Quote(nref)
CROSS APPLY response_body.nodes('//Request//eq:Charge') AS Charge(ncharge)
CROSS APPLY response_body.nodes('//Request//eq:Charge//v:TieredCharge') AS TieredCharge(tcharge)
WHERE nref.value('Request[1]/eq:Product[1]', 'nvarchar(60)') <> 'AVL'
select * from #TempCharges
So although this flattens the xml, what I'm getting is repeated rows even if the eq:Charge element doesn't contain a v:TieredCharge repeating element?
For example I get 4 rows where the charge type is QAC even though there is only one element with that type?
How do I query this so that I only get one row for each Charge element, unless there are repeating child elements below it, in which case I'll only get the rows repeated for each element?
So I should get something like this:
MaintChange TieredCharge1 row
MaintChange TieredCharge2 row
MaintChange TieredCharge3 row
MaintChange TieredCharge4 row
MaintCharge
MaintCharge
QAC
So the trick is to use OUTER APPLY in the last bit of the query - its kind of like an inner join. Then it works.