Proper syntax for performing an intersection with geography - polygon

So, I am doing homework and I am unable to figure this step out.
Right-click the tables you just created, click Design, then add a new column in it, which is called GEOG, datatype is geography. Save the changes to your tables.
Fill the GEOG column with WKT column converted to Line/Polygon data.
For counties table build a new UPDATE query, convert WKT to Polygon and save it into GEOG column.
Table 1 is brazosStreet and table 2 is counties.
Table1 WKT is LINESTRING, and Table2 is POLYGON.
I am stuck on the Table2 Polygon step.
Validate the data and perform an intersection:
Geog.MakeValid();
Geog.STIntersects(Geog1) = 1;
The Validation:
Update [dbo].[Counties]
set Geog = Geog.MakeValid()
Perform the Selection:
SELECT *
FROM [dbo].[counties]
WHERE Geog.STIntersects(geography::STLineFromText(‘LINESTRING(-98.144094 31.915186, -93.914357 30.977930)’, 4326);
No matter what I type for the "Perform the selection" section, I get an error. I tried STPolyFromText and I tried deleting and starting over 3 times.
The only thing I can think of is that I skip over this section because I cannot get it to work:
Validate the data and perform an intersection
Geog.MakeValid();
Geog.STIntersects(Geog1) = 1;
The way I read that was as on overview of what is to come. Since it looks like Geog.MakeValid(); is part of the validation stage and Geog.STIntersects(Geog1) = 1; looks like part of the perform the selection stage.
Why is my syntax not working properly? I am unsure what SQL language exactly this is. Please let me know if I need to explain this situation more. Thanks for the help in advance!
UPDATE [dbo].[brazosStreets]
SET
[Geog] = geography::STLineFromText([WKT],4326);
UPDATE [dbo].[counties]
SET Geog = Geog.MakeValid;
select *
from [dbo].[counties] c
where c.STIntersect(geography::STLineFromText(‘LINESTRING(-98.144097
31.915186, -93.914357 30.977930)’,4326))=1;
I get error 102 when I try to run this.. Error with the 'single quote' placement involving a non-boolean operator

Related

Use case statement for parameter for column name in sql where clause

I have been looking all day for a solution that works for my situation. I have found some things that are very similar but don't work for my situation, I tried them.
Here is the scenario; I have two table base and partdetails. I have an asp website (internal ONLY) that has drop down lists to select the parameters for a SQL query that fills a data grid view.
My problem is this, I need to be able, based on the drop down list boxes on the page, assign the column name that the criteria that is entered to be searched for.
Here is the query that I am trying to define: (This one returns 0 rows)
sqlCmd.CommandText = ("Select ba.referenceid, ba.partnum, pd.width, pd.length, CONVERT(varchar(12), pd.dateentered, 101) As [dateentered], ba.partqty, ba.status, ba.material From tbl_dlbase ba Join tbl_partdetails pd On ba.referenceid = pd.referenceid Where Case #field1 When 'part #' Then 'ba.partnum' When 'Spacing' Then 'pd.spacing' When 'Surface' Then 'pd.surface' When 'Height' Then 'pd.height' When 'Thickness' Then 'pd.thickness' End Like '%' + #criteria1 + '%'")
sqlCmd.Parameters.AddWithValue("#field1", ddlSc1.SelectedItem.Text)
sqlCmd.Parameters.AddWithValue("#criteria1", txbCriteria1.Text)
This is the latest version of the SQL statement that I have tried. I need to be able to set the field/column name based on the selection from the drop down list ddlsc1 on the asp page.
I have also been trying the queries in Studio manager to see if maybe I have fat fingered something but it also returns 0 rows so I know something is wrong with the query.
So how can I set the column name field using a parameter for the name. I know this is a huge security concern with SQL injection but this is an internal only site, and more importantly my boss said he wants it done with variables.
I don't really see a problem with this other than you have single quotes around your THEN values. Does this fix it?
SELECT ba.referenceid
,ba.partnum
,pd.width
,pd.length
,CONVERT(VARCHAR(12), pd.dateentered, 101) AS [dateentered]
,ba.partqty
,ba.STATUS
,ba.material
FROM tbl_dlbase ba
JOIN tbl_partdetails pd ON ba.referenceid = pd.referenceid
WHERE CASE #field1
WHEN 'part #'
THEN ba.partnum
WHEN 'Spacing'
THEN pd.spacing
WHEN 'Surface'
THEN pd.surface
WHEN 'Height'
THEN pd.height
WHEN 'Thickness'
THEN pd.thickness
END LIKE '%' + #criteria1 + '%'

Join geometries to update in SpatiaLite

I am working on a project in SpatiaLite and I would like to join together two geometries to create one final geometry that contains both original polygons (Ex. Putting together two Legos). The tricky part is how to make this work in SpatiaLite with the 'Blob' geometry type. I have tried ST_Union but still cannot seem to get any result at all. The result of this will ultimately update a geometry in a table elsewhere.
Here's what I've done so far:
UPDATE table1
SET Shape = (
SELECT ST_Union (a.Shape, b.Shape)
FROM table2 as a
JOIN (
table3 as b
ON a.Shape = b.Shape)
WHERE a.ADDRESS = "" or b.ADDRESS = "");
I realize there are many errors in the syntax of this, I am only just getting used to SQLite and the sytanx versus that of Postgres. Please feel free to make edits.

Extract only required files in U-SQL

Is it possible to extract files only for 3 days, without extracting all the files.
DROP VIEW IF EXISTS dbo.Read;
CREATE VIEW IF NOT EXISTS dbo.Read AS
EXTRACT
Statements
FROM
"adl://Test/{date:yyyy}/{date:M}/{date:d}/Testfile.csv"
USING Extractors.Csv(silent:true,quoting : true, nullEscape : "/N");
#res =
SELECT * FROM dbo.Read
WHERE date BETWEEN DateTime.Parse("2015/07/01") AND DateTime.Parse("2015/07/03");
OUTPUT #res
TO "adl://test/Testing/loop.csv"
USING Outputters.Csv();
Partition elimination already ensures for your query that only files matching predicates will actually be read (you can confirm that in the job graph).
See also my previous answer for How to implement Loops in U-SQL
If you have remaining concerns about performance, the job graph can also help you nail down where they originate.
You can use the pattern identifiers in the fileset specification in parts of the path or even parts of the name (see https://msdn.microsoft.com/en-us/library/azure/mt771650.aspx). You can do lists of files, so if you only have one file in each directory you can do;
EXTRACT ...
FROM "adl://Test/2015/07/1/Testfile.csv"
, "adl://Test/2015/07/2/Testfile.csv"
USING ...;
If there is more than one file in each directory you can do individual extracts for each day and then union the result. Something like:
#a = EXTRACT ....
FROM "adl://Test/2015/07/1/{*}.csv"
USING ...;
#b = EXTRACT ....
FROM "adl://Test/2015/07/2/{*}.csv"
USING ...;
#fullset = SELECT * FROM #a UNION SELECT * FROM #b;
Unfortunately I believe there is no list of filesets at the moment allowing you to do above case in one EXTRACT statement.

MS Access use iif statement select query as alias

I am trying to build a query to get the student results for a specific exam as a table that can be merge to a word document. The following works fine but seems very ineficient since I need to call the same query twice in the same iif statement.
Test1: IIf(Round((SELECT tblMarks.Score FROM tblMarks WHERE tblMarks.Test = 'Test1' AND [tblMarks].[ID] = [tblStudents].ID AND [tblMarks].[Rewrite] = false)*100,0)<70,70,Round((SELECT tblMarks.Score FROM tblMarks WHERE tblMarks.Test = 'Test1' AND [tblMarks].[ID] = [tblStudents].ID AND [tblMarks].[Rewrite] = false)*100,0))
To get rid of the second query call I tried the following but StudentScore is not being recognized by the IIF false condition.
Test1: IIf(Round((SELECT tblMarks.Score AS StudentScore FROM tblMarks WHERE tblMarks.Test = 'Test1' AND [tblMarks].[ID] = [tblStudents].ID AND [tblMarks].[Rewrite] = false)*100,0)<70,70, StudentScore)
I have many of those test field (test2, test3 etc...) so even just removing the extra query per field would probably help speed things up quite a bit.
Does anyone has any idea if what I am trying to do even possible??? Any help appreciated.
Thanks.
UPDATE:
I am trying to create a table/query to be use to merge into an MS Word document with fields. This new query combines many tables into one. Here's and example of the table structure:
tblStudent: StudentID, Name etc... A lot of personal info.
tblScore: StudentID, Test, Score, Rewrite etc...
New Query field are:
DISTINCT tblStudent.StudentID, tblStudent.Name, tblScore.Test(as shown above) AS Test1, tblScore.Test(Same as above but with test2) AS Test2, ... Where CourseName.....
Hope this help people see what I am trying to do; which work fine I am just trying to eliminate the second query in the if statement. Sorry this is the best I can do right now since I am not at work right now and this is where all this stuff is stored.

Chart doesn't update when adding new rows to an existing Excel table (not without having to use named ranges)

I'm really new to the use of closedXMl and Excel too(at least for this purpose) so sorry if I'm asking silly questions.
I know that closedXML doesn't support charts yet so the only thing that came to mind to get around this was to create my chart using an excel table . That way I thought ClosedXML would update the ranges when I inserted new rows and the chart would pick up on it. Well , it didn't. At least not when I add the rows from code using the closedXML library.
What is curious is that adding new rows from inside excel automatically updates the chart but if I want to get that same result from code, I have to use OFFSET formula along with named ranges and then set the chart source data to these named ranges.
That's why I'd like to know if if there is anything wrong with the code I use to insert new rows:
Dim ruta As String = Server.MapPath("~/Templates/MyTemplate.xlsx")
Dim wb As New XLWorkbook(ruta)
Dim ws = wb.Worksheet(1)
Dim tblData = ws.Table("Table1")
Dim year As Integer = 2000
For i As Integer = 1 To 13
With tblData.DataRange.LastRow()
.Field("Year").SetValue(year)
.Field("Sales").SetValue(CInt(Math.Floor((2000 - 500 + 1) * Rnd())) + 500)
End With
tblData.DataRange.InsertRowsBelow(1)
year = year + 1
Next
tblData.LastRow.Delete()
As you can see the code is very simple and so is the template , that consists of only two columns : "Year"(table1[Year]) and "Sales"(Table1[Sales]
I don't think this has anything to do with my template because as I told you, adding new rows directly from excel works as expected and it is only when I generate the table from code that the chart series doesn't include the new row that were added
Being necessary to manually add the new ranges(Sheet1!Table1[Sales] and Sheet1!Table1[Year]) as it only includes the first row(the one added by default when you insert a table)
Any help would be much appreciated
P.S. Here is a link to a rar containing the full code as well as the excel template(\Templates\MyTemplate.xlsx)
If the problem is that your table doesn't recognise the additional rows, try adding this after the last row delete:
tblData.Resize tblData.Range(1, 1).CurrentRegion
That should resize the table. Then hopefully your table operations should work.

Resources