Rails query works then creates an error - ruby-on-rails-4

I have a query to retrieve all the spots (times) available on a given date.
#takenspots = Spot.joins(:reservations).where('reservations.date = ?', params[:date])
if #takenspots.any?
#availablespots = Spot.where('id != ?', #takenspots.map)
I use #availablespots to populate a select_list.
I have 4 different spots per day and the query works only 2 times. (2/4 spots)
If a try to book a day where i have 2 spots taken out of 4, it says:
SQLite3::SQLException: near ",": syntax error: SELECT "spots".* FROM "spots" WHERE (id != 1,2)
But I can't understand why it works for the first 2 spots and not the 2 remaining...
Someone got an idea ?
Thank you,

I have no idea why this works actually, but it does, so this might help someone:
#availablespots = Spot.where('id not in (?)',#takenspots.map(&:id))

Related

Power BI Advanced Editor - How to run query if current date is NOT within a list of values?

I want to run a query in Advanced Editor, only if the current date is NOT one of the following - 1, 5, 10, 15, 20, 25. Otherwise I dont want the query to run.
I have written the below but I am not getting anywhere very fast:
let
Output = if Date.Day (DateTime.LocalNow()) in (1,5,10,15,20,25) then null else
let
Source = PBILifts
in
#"PBILifts"
What am I doing wrong?
Cheers for all help
Edit: So based on suggestion by #Aleksei I tried below but it doesnt work:
let
output= if List.Contains({1,5,10,15,20,25}, Date.Day(DateTime.LocalNow())) then #table({},{}) else
Source = PBILifts
in
#"PBILifts"
I get the below error:
An error occurred in the ‘’ query. Expression.Error: The name 'Source' wasn't recognized. Make sure it's spelled correctly
Any further help please?
It's PQ, not SQL, so in (1,5,...) syntax is incorrect. Try something like this (assuming, your query's output is table):
= if List.Contains({1,5,10,15,20,25}, Date.Day(DateTime.LocalNow())) then #table({},{}) else YourQuery
or:
let
output = if List.Contains({1,5,10,15,20,25}, Date.Day(DateTime.LocalNow())) then #table({},{}) else PBILifts
in
output

Exact match of string in pandas python

I have a column in data frame which ex df:
A
0 Good to 1. Good communication EI : tathagata.kar#ae.com
1 SAP ECC Project System EI: ram.vaddadi#ae.com
2 EI : ravikumar.swarna Role:SSE Minimum Skill
I have a list of of strings
ls=['tathagata.kar#ae.com','a.kar#ae.com']
Now if i want to filter out
for i in range(len(ls)):
df1=df[df['A'].str.contains(ls[i])
if len(df1.columns!=0):
print ls[i]
I get the output
tathagata.kar#ae.com
a.kar#ae.com
But I need only tathagata.kar#ae.com
How Can It be achieved?
As you can see I've tried str.contains But I need something for extact match
You could simply use ==
string_a == string_b
It should return True if the two strings are equal. But this does not solve your issue.
Edit 2: You should use len(df1.index) instead of len(df1.columns). Indeed, len(df1.columns) will give you the number of columns, and not the number of rows.
Edit 3: After reading your second post, I've understood your problem. The solution you propose could lead to some errors.
For instance, if you have:
ls=['tathagata.kar#ae.com','a.kar#ae.com', 'tathagata.kar#ae.co']
the first and the third element will match str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
And this is an unwanted behaviour.
You could add a check on the end of the string: str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')
Like this:
for i in range(len(ls)):
df1 = df[df['A'].str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')]
if len(df1.index != 0):
print (ls[i])
(Remove parenthesis in the "print" if you use python 2.7)
Thanks for the help. But seems like I found a solution that is working as of now.
Must use str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
This seems to solve the problem.
Although thanks to #IsaacDj for his help.
Why not just use:
df1 = df[df['A'].[str.match][1](ls[i])
It's the equivalent of regex match.

regexp_similar '^.$' issues in teradata

For data scrubbing I have lot of hard coded values in my program. I am trying to put those values into a table. One of the conditions for this scrubbing is to find the length of the character and code (character_length(name) = 1).
But when I try to emulate the this by using ^.$, it is not catching values like ¿, ¥, Ã
please let me know if I am doing something wrong .
When I run below code and I see this 3 values ¿, ¥, Ã
select name from email_table
where character_length(name) = 1
and name not in
(select name from email_table
where regexp_similar(translate(name USING LATIN_TO_UNICODE WITH ERROR),'^.$', 'i') = 1)
It seems like the issue is due to version.
We have TD14 and TD 15 on different servers and I did following query
select case when regexp_similar('¥','^.$', 'i')=1
then 'Y'
else 'N'
end as output;
In case of TD 14, I get output as 'N' and in case of TD 15 answer is 'Y'.

Doctrine Query from Mysql

I want to create a Doctrine Query: (Doctrine 2.3)
SELECT * FROM `car` WHERE `plate` like '%' AND (`datetime` BETWEEN '2013-03-13 22:20:18' AND '2013-03-13 22:20:20') OR (`datetime` BETWEEN '2013-03-13 15:10:18' AND '2013-03-13 15:10:16')
I tried the following but its not working:
$qry = $this->manager()->createQueryBuilder()
->from($this->entity, 'e')
->select('e');
$qry->where('e.plate like :plate');
$qry->setParameter('plate', $plate);
$qry->andWhere(
qry->expr()->between(
'e.datetime',
':dateFrom',
':dateTo'
)
)
->setParameter('dateFrom', $fromdate)
->setParameter('dateTo', $todate);
$qry->orWhere(
$qry->expr()->between(
'e.datetime',
':dateFrom',
':dateTo'
)
)
->setParameter('dateFrom1', $fromdate1)
->setParameter('dateTo1', $todate1);
OutPut of above query:
SELECT e FROM user e WHERE (e.plate like :plate AND (e.datetime BETWEEN :dateFrom AND :dateTo)) OR (e.datetime BETWEEN :dateFrom AND :dateTo)
I want to check two dates in same column how can I check? Is the syntax correct?
Currently It is like this:
(Plate AND (Date)) OR Date)
Case 1
But it should come like the following for good output.
(Plate) AND ((Date) OR (Date))
Case 2
In other case it should come like this:
((Plate) or (Plate)) AND ((Date) OR (Date))
Can some one help me I am not an expert in Doctrine I am a learner!
After some search and advice from many individual I finally found some logic and understood the expression in Doctrine. Below I have given all the expression with an example. Code to be tested.
$ex1 = $qry->expr()->like('e.user', "'".$user."'");
$ex2 = $qry->expr()->orX(
$qry->expr()->between('e.datetime', "'".$datetimefrom."'", "'".$datetimeto."'")
);
$ex3 = $qry->expr()->in('e.country', $country);
$final_expression = $qry->expr()->andX($ex1,$ex2,$ex3);
You can also refer the below issue and solution which helped me to solve the above Question by me.
REF Multiple Query in Doctrine with NAND,NOR,NOT,AND Operators

Doctrine2: Doctrine query returns different results from raw query?

I have come across this multiple times in the past, and I just ended up writing a raw SQL query to overcome it, but I really want to find out why this is happening. Look at the statement below
$q = $this->entityManager->createQuery('SELECT um,s,st
FROM Dashboard\Entity\Metric um
JOIN um.stat st
JOIN um.site s
JOIN s.clients c
WHERE c.id = ?1
AND s.competitor = 0
AND s.ignored = 0
AND st.id IN (?2)
GROUP BY s.id, st.id
ORDER BY st.response_field, s.id')
->setParameter(1, $params['c_id'])
->setParameter(2, $statId);
$sql = $q->getSql();
$rs = $q->getResult();
If I take the contents of $sql and paste them into a mySQL tool and run the raw query, it returns 18 results which is correct.
However, $rs only contains 3 results. $statId is a comma-separated string of 6 numbers: (1,2,3,4,5,6). So I am grouping by st.id, and s.id. There will be 3 s.id elements for every st.id element, working out to the 18 results I expected. What's happening is Doctrine is only returning the first st.id which is the group of 3 s.id 's
Any idea what could be causing this?
I got my answer from IRC, but posting here in case it helps someone else. Smart WHERE IN statements aren't planned for support until 2.1 version.
http://groups.google.com/group/doctrine-dev/browse_thread/thread/fbf70837293676fb
But I can accomplish the same goal with query builder.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/query-builder.html