I wonder how to combine these two into one so that I can have one table displaying both number of "loves" and "hates" together
select count(id) as number
from review
where text like "%love%"
select count(id) as number
from review
where text like "%hate%"
Use case expressions to do conditional aggregation:
select sum(case when text like '%love%' then 1 else 0 end) as love_number,
sum(case when text like '%hate%' then 1 else 0 end) as hate_number
from review
where text like '%love%' or text like '%hate%'
(The WHERE clause isn't really needed, but will keep read-set size down etc.)
Use union all
select count(id) as number
from review
where text like "%love%"
union all
select count(id) as number
from review
where text like "%hate%"
It is not really clear what you want to achieve but perhaps the following SQL is good for you. It shows the count of the reviews:
select count(id) as number
from review
where text like '%love%' or text like '%hate%';
I believe this will fix the issue
SELECT COUNT(SELECT ID FROM REVIEW WHERE text like %Love%) AS LOVE,
COUNT(SELECT ID FROM REVIEW WHERE text like %Hate%) AS HATE
FROM REVIEW
I have a similar table as below:
product
01 apple
02 orange
banana 10
I am trying to exclude only rows which start with a number. If the number is not in the beginning then it should not be excluded. The desired table output should be like this:
product
banana 10
However with my current query, it excludes everything as soon as there is a number in the row:
SELECT *
FROM table
WHERE product NOT LIKE '%0%'
Could anyone please suggest me on how to tackle this? Much appreciated.
Something like this maybe:
SELECT *
FROM table
WHERE left(product, 1) NOT IN ('0','1','2','3','4','5','6','7','8','9')
regex to match lines that don't start with number is
^[^0-9].*
An sql query in mysql would look like
SELECT *
FROM table
WHERE product RLIKE '^[^0-9].*'
I would recommend regular expressions. In Redshift, this looks like:
where product ~ '^[^0-9]'
I might also suggest:
where left(product, 1) not between '0' and '9'
I have a column "numbers" with array values. If I select the column in a query, the result looks like:
["40432","83248","1"]
["40432","8923","7723"]
["2340","837","20309"]
["290348","83248","20309","187"]
["98184897","98234","20309"]
["40432","83248"]
["2340"]
Now, I'd like to group the results on only the first value in the array and count them. The result should look like:
value amount
40432 3
2340 2
290348 1
98184897 1
How do I arrange this? How should the query look like?
I tried things like:
SELECT.... WHERE split(TO_JSON_STRING(numbers), ',')[ordinal(1)] as firstNumber ......
But this did not result in the desired data.
I want to run a query that fetches me all the names that have for say "ana" in it but i want the result like this
Anna
Brianna
not like
Brianna
Anna
which means the name starting with "an" should come first and then anything containing "an" in them
My SQL query is like this but this dpes not give me the desired results. I checked some other stuff but not quite sure how to use and does not give the desired results.
SELECT *
FROM TABLE_NAME
WHERE Name LIKE 'ana%'
AND Name LIKE '%ana%'
You need to select the records only once and then order them and according to your requirements i understand that you need to order something like this by using ORDER BY CASE
SELECT *
FROM TABLE_NAME
WHERE Name LIKE '%ana%'
ORDER BY CASE
WHEN Name LIKE 'an%' THEN 1
WHEN Name LIKE '%an%' THEN 2
ELSE 3
END
From my research online I have discovered two answers to this question which I am trying to stay away from.
I cannot modify the table or add a new table because the software is third party and needs the table to remain unmodified.
I am trying to stay away from using temporary tables or extra user defined functions.
Here is my issue.
There is a column in the database that is a list of comma-delimited numbers representing days of the week, i.e. (1,2,4,5,7).
I am trying to find a way to read that data and find out if there are any rows where that column represents days that are 3 consecutive days.
It should return anything with
1,2,3
2,3,4
3,4,5
5,6,7
1,,,,,6,7
1,2,,,,,7
But if the column has 1,2,3,4 it should not return twice. There are a lot of rows that have 2,3,4,5,6 and any solution I've come up with will return that 3 times.
Preferably, I would like to create a stored procedure to pass in a number and look for that number of consecutive days. So if 5 is passed in, it will look for anything that is marked for 5 consecutive days.
Is there another option other than using extra tables? If so can you show me how to do make this work? I am not new to SQL but there are a lot of more advanced querying techniques I am not familiar with.
The following brute force method will work in all databases:
select (case when col like '%1%' and col like '%2%' and col like '%3%' then 1
when col like '%2%' and col like '%3%' and col like '%4%' then 1
when col like '%3%' and col like '%4%' and col like '%5%' then 1
when col like '%4%' and col like '%5%' and col like '%6%' then 1
when col like '%5%' and col like '%6%' and col like '%7%' then 1
when col like '%6%' and col like '%7%' and col like '%1%' then 1
when col like '%7%' and col like '%1%' and col like '%2%' then 1
else 0
end) as HasThreeConsecutiveDays
It returns a 0/1 flag if three days are consecutive.
So if 5 is passed in, it will look for anything that is marked for 5 consecutive days.
You won't be able to do that without dynamic sql, because you want to support wrapping from 7 back to 1. I could write a query that would do it for you in a single statement if you didn't care about wrapping from the end of the week back to the beginning, but with that requirement I don't see how to do it without building a dynamic sql string in the procedure, which I don't have time to play with right now (maybe someone else will take that idea and run with it).
With that option defeated for now, I can do this instead:
WHERE
( col like '1,2,3%'
OR col like '%2,3,4%'
OR col like '%3,4,5%'
OR col like '%4,5,6%'
OR col like '%5,6,7'
OR col like '1%6,7'
OR col like '1,2%7'
)
This should be better than checking individual numbers as shown in another answer, because there are fewer pattern matches to complete. However, it only works if we can guarantee the sort order. We also need to know in advance how the commas are spaced between numbers, but we can fix that issue if necessary by replacing all commas and/or spaces with an empty string (and adjusting the patterns accordingly).
One more thought here: I realized that I can support a day count argument, if you can manage sneaking an additional table into the db somewhere. The table would look something like this:
create Table DayPatterns (Days int, Pattern varchar(13) )
and the data in the table would look like this:
1 1%
1 %2%
1 %3%
...
2 1,2%
2 %2,3%
2 %3,4%
2 %4,5%
...
2 1%7
...
3 1,2,3%
3 %2,3,4%
...
3 1%6,7
3 1,2%7
...
7 1,2,3,4,5,6,7
Hopefully you get the idea on how to fill that out. With that table in hand, you can JOIN against the table with a query like this:
INNER JOIN DayPatterns p ON p.Days = #ConsecutiveDays AND col LIKE p.Pattern
The key to making that work (aside from needing to be able to create that table somewhere) is also doing a GROUP BY on the correct columns. Otherwise, you'll end up with the same problem you have right now, where matching multiple possible consecutive day patterns will duplicate your results.
Finally, of course you know that most any schema that includes csv data is broken, but since you can't seem to fix this, hopefully one of these ideas will help.