Update several rows simultaneously SQL server - tsql

I'm in the process of creating a table that lists cities, state, zip, and long/lat. I've created another column (called GeoLoc) in my table that uses the geography data type. I would like to combine my latitude and longitude columns into the GeoLoc column so I can create SSRS map reports.
I can simply use an update statement to do this. For example:
UPDATE us_loc_data SET GeoLocation = 'POINT(-71.013202 43.005895)' WHERE Zip = '210'
The problem is, I have over 40,000 rows all having varying coordinates. Can I somehow perform this update on every row, simultaneously?

Something like this?
Update us_loc_data SET GeoLocation = geography::Point(B.lat,b.Lng,4326 )
From us_loc_data A
Join YourLatLngTable B on (A.Zip=B.Zip)

Related

Update column in a dataset only if matching record exists in another dataset in Tableau Prep Builder

Any way to do this? Basically trying to do a SQL UPDATE SET function if matching record for one or more key fields exists in another dataset.
Tried using Joins and Merge. Joins seems like more steps and the Merge appends records instead of updating the correlating rows.

How to aggregate jsonb object?

I have jsonb field in table with values like that:
{
"1":[{"start":64800,"finish":68400},{"start":61200,"finish":64800},{"start":75600,"finish":79200},{"start":79200,"finish":82800}],
"2":[{"start":68400,"finish":72000},{"start":72000,"finish":75600},{"start":75600,"finish":79200},{"start":79200,"finish":82800}],
"3":[{"start":46800,"finish":50400},{"start":50400,"finish":54000}],
"4":[{"start":50400,"finish":54000}],
"5":[{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}],
"6":[{"start":68400,"finish":72000},{"start":72000,"finish":75600},{"start":79200,"finish":82800}]
}
0...6 - day of week, it's array of working time.
So I need to aggregate working time by days of week. For example, one row has
"5":[{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}]
another row has
"5":[{"start":75600,"finish":79200}]
and I want to get
"5":[{"start":75600,"finish":79200},{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}]
If you need this report to be run manually & irregularly, you can do this within PostgreSQL itself. But if performance matters to you, you should normalize your schema (f.ex. if your jsonb column's structure is that simple, there is literally no need to use JSON at all: you don't have any unstructured data).
SELECT jsonb_object_agg(dow, working_times_agg)
FROM (SELECT dow, jsonb_agg(working_time) working_times_agg
FROM table t,
jsonb_each(t.jsonb_column) o(dow, working_times),
jsonb_array_elements(working_times) working_time
GROUP BY dow) working_times_agg_by_dow

PostGIS update geo column with ST_GeogFromText and get lon/lat from another column

I started to use PostGIS recently and i have a table with a column longitudes and a column latitudes.
I want to know if it's possible (and how) to put these lon/lat columns informations into the new geographical column (with possibly ST_GeogFromText)
I already tried something like :
UPDATE my_table SET geo = ST_GeogFromText('SRID=4267;POINT(lon.my_table lat.my_table)');
Thanks for your reading and you help.
ST_GeogFromText takes a formatted text of EWKT, which is a lossy and inefficient method.
Try using ST_MakePoint instead:
UPDATE my_table SET geo = ST_SetSRID(ST_MakePoint(lon, lat), 4267)::geography;

How to assign foreign keys in Access within imported table from Excel

I will use Access database instead of Excel. But I need to import data from one huge Excel sheet into several pre-prepared normalized tables in Access. In the core Access table I have mainly the foreign keys from other tables (of course some other fields are texts or dates).
How should I perform the import in the easiest way? I cannot perform import directly, because there is NOT, for example, "United States" string in the Access field 'Country'; there must be foreign key no. 84 from the table tblCountries. I think about DLOOKUP function in the Excel and replace strings for FK... Do you know any more simple method?
Thank you, Martin
You don’t mention how you will get the Excel data into several Access tables, so I will assume you will import the entire Excel file into ONE large table then break out the data from there. I assume the imported data may NOT match with existing Access keys (i.e. misspellings, new values, etc.) so you will need to locate those so you can make corrections. This will involve creating a number of ‘unmatched queries’ then a number of ‘Update queries’, finally you can use Append queries to pull data from your import table into the final resting place. Using your example, you have imported ‘Country = United States’, but you need to relate that value to key “84”?
Let’s set some examples:
Assume you imported your Excel data into one large Access table. Also assume your import has three fields you need to get keys for.
You already have several control tables in Access similar to the following:
a. tblRegion: contains RegionCode, RegionName (i.e. 1=Pacific, 2=North America, 3=Asia, …)
b. tblCountry: contains CountryCode, Country, Region (i.e. 84 | United States | 2
c. tblProductType: contains ProdCode, ProductType (i.e. VEH | vehicles; ELE | electrical; etc.)
d. Assume your imported data has fields
Here are the steps I would take:
If your Excel file does not already have columns to hold the key values (i.e. 84), add them before the import. Or after the import, modify the table to add the columns.
Create ‘Unmatched query’ for each key field you need to relate. (Use ‘Query Wizard’, ‘Find Unmatched Query Wizard’. This will show you all imported data that does not have a match in your key table and you will need to correct those valuse. i.e.:
SELECT tblFromExcel.Country, tblFromExcel.Region, tblFromExcel.ProductType, tblFromExcel.SomeData
FROM tblFromExcel LEFT JOIN tblCountry ON tblFromExcel.[Country] = tblCountry.[CountryName]
WHERE (((tblCountry.CountryName) Is Null));
Update the FK with matching values:
UPDATE tblCountry
INNER JOIN tblFromExcel ON tblCountry.CountryName = tblFromExcel.Country
SET tblFromExcel.CountryFK = [CountryNbr];
Repeat the above Unmatched / Matched for all other key fields.

stored procedure returns data of varying dimensions Entity Framework 4

for the reporting of a survey system I am working on, we developed a stored procedure that returns data with varying number of columns.
we show the operator all the columns from these tables: Users, Questions, Answers.
the user selects the columns from each of the tables that the report should show.
for example:
User: Name, Age, zipcode.
Questions: question2, question 4
Answers: answer2, answer3, answer4.
we then pass the parameters to the stored procedure and the stored procedure returns:
one column for each user property, question or answer.
and a row for each user in the DB.
example:
as you can see, the stored procedure can return anything between 3 rows of 2 columns to 500 rows of 50 columns. Is there a way to use the stored procedure with entity framework? at first I tried with a complex return type, but it appears that that approach will not work in this case.
EF supports only stored procedures with fixed number of columns defined at design time. To execute this procedure you need to use plain old ADO.NET.
EDIT: If you have fixed total nuber of colums (you mentioned 50) you can create single class containing all these columns and use it as result for execution. EF will fill only properties existing in the result set.