Update statement in SQL when two different search items present - sql-update

I've got a small issue when updating a column. When a db needs restored on a testdb, I need to change some fields in order to prevent issues on the production db.
Most of the time it only is adjusting the e-mail address and web address.
Now I walked on this one:
In a table there is a column filled with URL's. One URL starts with fairworks... and the other starts with www.fairworks...
Both URL need to be changed to: Test.fairworks...
So fairw..... and www.fairw... need to be changed to: TEST.fairw...
I tried to update with this code but somehow it doesn't do what I intended
Update URL
SET Url = CASE
WHEN (LEFT(Url,3) = 'fai' THEN REPLACE(Url,'fair','test.fair')
ELSE
WHEN (LEFT(Url,6) = 'www.fa THEN REPLACE(Url,'www.fair', 'test.fair')
END
I can't see where I go wrong.
Because I haven't enough points I cannot add the complete code. Both URL starts with HTTP://

I haven't tested this, but you could use something like this:
UPDATE [YourTable]
SET Url = 'www.fairworks' + SUBSTRING(Url, 4, LEN(Url)-4)
WHERE LEFT(Url,4) = 'fair'
UPDATE [YourTable]
SET Url = 'www.fairworks' + SUBSTRING(Url, 8, LEN(Url)-8)
WHERE LEFT(Url,8) = 'www.fair'
By the way, you missed an apostrophe (just before THEN) in your code:
WHEN (LEFT(Url,6) = 'www.fa THEN REPLACE(Url,'www.fair', 'test.fair')

Related

Power BI Advanced Editor - API Pagination Querying Resulting in Many Duplicate Results?

Ok I have been stuck on this for a few weeks now. I'm using the Front Email API for a business use case and have created an iterative function to (attempt to) get multiple pages of query results.
A quick overview of the API (endpoint) I'm using for context:
The "events" endpoint returns a list of records based on the parameters given in the query (like before/after/between certain times, types of events, etc.)
If the query results in more than 100 records, a "next" pagination link is generated for the next page(s) of results. There is no "page=n" parameter in the query URL, you only get the "next page" link from the response of the previous query (which is fairly unique)
A side note, the initial base_url for the first query, and the base_url of the "next page" link are two different urls (i.e. the initial call is https://api2.frontapp.com and the second is https://companynamehere-inc.api.frontapp.com), so this is taken into consideration in my querying function.
My environment looks like this:
As you can see, I query the initial URL using the external Func_Query_Front_API function, then begin the iteration; While the next page link is not null, keep feeding the next links returned from the previous calls back into the function to get the next page of results. I deconstruct the links given to the function into a base, relative path and body/params so that I can use this logic in both Desktop and Online Service (don't ask).
It's difficult to describe the results I get, because sometimes in the preview window, it just clocks and clocks and doesn't return any results with the API not being queried at all (confirmed from Postman and the rate limit remaining number in the response headers). When I do get results back, it's a massive list of records (way more than what I'm querying for/expecting to receive) that contains lots of duplicates. It's almost like the initial (or the second) query URL is being looped over and over again, but not the "next" page's links? Like it's somehow stuck in a semi-infinite loop while the "next" link is not null from the initial response only, so it just repeats the same query over and over again re-using the same "next" page link.
Now, unfortunately I cannot provide my bearer token for this API as it's private company info returned by the API, but I'm wondering if the issue is with my syntax in the code that someone can spot and steer me in the right direction. The querying function itself works when invoked on its own, and everything looks like it SHOULD work, but it's just not doing what I think the code is saying it should do.
I understand it's not much to go on, but I'm out of options in trying to debug this thing. Thanks!
UPDATE
I think what MIGHT help here is a working code written in Python that might help to translate what I'm looking for into Power BI, so I've provided a working code in Python below (again though, the bearer token is not provided, but the syntax should make things a bit clearer). The code closely resembles what I've already made in Power BI as well so hopefully this helps things a bit?
import requests
from time import sleep
########################################
# GLOBAL VARIABLES
########################################
_event_types_filter = "assign&q[types]=archive&q[types]=comment&q[types]=inbound&q[types]=outbound&q[types]=forward&q[types]=tag&q[types]=out_reply"
_after = 1667506000
_page_limit = 100
_bearer_token = "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX"
_init_base_url = "https://api2.frontapp.com"
_relative_path = "events"
_init_body = "limit=" + str(_page_limit) + "&q[after]=" + str(_after) + "&q[types]=" + _event_types_filter
_headers = {'authorization': _bearer_token}
_init_query_url = _init_base_url + "/" + _relative_path + "?" + _init_body
########################################
# FUNCTION - Query the API
########################################
def Func_Query_Front_API(input_url):
#print(input_url)
# Deconstruct the url into its separate parts
splitted_input_url = input_url.split("?")
input_url_base = splitted_input_url[0].replace("/events", "")
input_url_body_params = splitted_input_url[1]
# Query the API
response = requests.request("GET",
input_url_base + "/" + _relative_path + "?" + input_url_body_params,
headers=_headers)
# Get the "next" link from the response
next = response.json()["_pagination"]["next"]
# Return the response and the "next" link
return response, next
########################################
# MAIN PROGRAM START
########################################
# List to add the response data's to
Source = []
# Make the initial request and add the results to the list
init_response, next = Func_Query_Front_API(_init_query_url)
Source.append(init_response)
# While the "next" link(s) are not None, query the current
# "next" link for the next page(s) of the query
while next != None:
response, next = Func_Query_Front_API(next)
Source.append(response)
sleep(1)
print(Source)
print("Done!")

Power BI only returns first pagination when using hash key

I'm trying to get data from an API that uses a hash key to get the next page, the key comes from the first request and in order to get the next page I need to append the key and add to the URL like this:
BASEURL & "&next=" & KEY.
The first page and 2nd page loads fine but when it reaches the 3rd page it keeps giving me the 2nd page the key does not change based on the page you are in.
I have run it in postman and I do get the different pages by manually copying and pasting the hash key.
Here is my code:
let
iterations = 6, // iterations just for testing
BaseUrl = Text.Combine({"URL"}),
Headers0 = [Headers=[header1="header"]],
GeneratedList =
List.Generate( ()=>
[ i= 0, URL = BaseUrl,
Result = Json.Document(Web.Contents(URL, Headers0))],
each [i]<iterations,
each [ i=[i]+1,
(where "next" is the field of the 1st request that contain the "key" for the next page)
URL = "URL"&[Result][next],
Result = Function.InvokeAfter(()=> Json.Document(Web.Contents(URL,Headers0)), #duration(0,0,0,5))
]),
This is how my result looks like
In case anyone has the same problem, if you call the same URL you are going top receive a cached response, my solution was passing a random value in the headers different from the previous call

How to run a combination of query and filter in elasticsearch?

I am experimenting using elasticsearch in a dummy project in django. I am attempting to make a search page using django-elasticsearch-dsl. The user may provide a title, summary and a score to search for. The search should match all the information given by the user, but if the user does not provide any info about something, this should be skipped.
I am running the following code to search for all the values.
client = Elasticsearch()
s = Search().using(client).query("match", title=title_value)\
.query("match", summary=summary_value)\
.filter('range', score={'gt': scorefrom_value, 'lte': scoreto_value})
When I have a value for all the fields then the search works correctly, but if for example I do not provide a value for the summary_value, although I am expecting the search to continue searching for the rest of the values, the result is that it comes up with nothing as a result.
Is there some value that the fields should have by default in case the user does not provide a value? Or how should I approach this?
UPDATE 1
I tried using the following, but it returns every time no matter the input i am giving the same results.
s = Search(using=client)
if title:
s.query("match", title=title)
if summary:
s.query("match", summary=summary)
response = s.execute()
UPDATE 2
I can print using the to_dict().
if it is like the following then s is empty
s = Search(using=client)
s.query("match", title=title)
if it is like this
s = Search(using=client).query("match", title=title)
then it works properly but still if i add s.query("match", summary=summary) it does nothing.
You need to assign back into s:
if title:
s = s.query("match", title=title)
if summary:
s = s.query("match", summary=summary)
I can see in the Search example that django-elasticsearch-dsl lets you apply aggregations after a search so...
How about "staging" your search? I can think if the following:
#first, declare the Search object
s = Search(using=client, index="my-index")
#if parameter1 exists
if parameter1:
s.filter("term", field1= parameter1)
#if parameter2 exists
if parameter2:
s.query("match", field=parameter2)
Do the same for all your parameters (with the needed method for each) so only the ones that exist will appear in your query. At the end just run
response = s.execute()
and everything should work as you want :D
I would recommend you to use the Python ES Client. It lets you manage multiple things related to your cluster: set mappings, health checks, do queries, etc.
In its method .search(), the body parameter is where you send your query as you normally would run it ({"query"...}). Check the Usage example.
Now, for your particular case, you can have a template of your query stored in a variable. You first start with, let's say, an "empty query" only with filter, just like:
query = {
"query":{
"bool":{
"filter":[
]
}
}
}
From here, you now can build your query from the parameters you have.
This is:
#This would look a little messy, but it's useful ;)
#if parameter1 is not None or emtpy
#(change the if statement for your particular case)
if parameter1:
query["query"]["bool"]["filter"].append({"term": {"field1": parameter1}})
Do the same for all your parameters (for strings, use "term", for ranges use "range" as usual) and send the query in the .search()'s body parameter and it should work as you want.
Hope this is helpful! :D

Why is Django Paramaterized query not working

I have been struggling with this simple query in django. I have checked a lot over internet. I tried using similar syntax - yet no luck.
In my application on html page I need to display some specific record. And for that i want to use parameterized select statement. But the query is not working..
I have checked id2 is getting correct value from previous html page..
And I know I can use APIs but for my databases classes I need to use raw queries in my application.
Can someone please help me here...
def details(request, id2):
temp = 'test3'
data = Posts.objects.raw('''select * from posts_posts where posts_posts.id = %s ''', id2)
context ={
'post' : data,
If you run that code you will see an error, since that is not the correct format for a call to raw. If you can't see the error output anywhere, then you have yet another problem for another post.
The correct format for raw is: .raw(sql, iterable-values), like this:
posts = Posts.objects.raw("select * ..", [id2, ]) # or use (id2,) for a tuple
<RawQuerySet: select * from ... where id = 5>
To get the actual list, since that just gives you a Query, you need to evaluate it somehow:
posts_list = list(posts)
first_post = posts[0]
Be careful, if you don't evaluate the QuerySet then it can be re-run a second time. Please convert it to a list() before doing further operations on it.

Mapping urls in routes.py

I am using web2py and builing a REST api and have one of my URLs set up like this:
routes_in (
('/myapp/something/{?P<id>.*)/myfunction', /myapp/default/myfunction/\g<id>')
)
routes_out = (
('/myapp/default/myfunction/\g<id>', '/myapp/something/{?P<id>.*)/myfunction')
)
If my app is setup this way my function is not even entered into and I get an invalid request if I remove the id argument from the url that my url is being mapped to i.e. remove g<id> from above, I enter my function but the argument is not being captured.
I cannot change the structure of the URL as per my requirements and I am not sure how to go about this.
I would appreciate any pointers.
Thanks,
nav
The above does work in web2py I found that some other area of my code was breaking.