Combine codes so that results can display in one table - sql

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

Related

using aggregate function on a calculated column

Trying to use sum() on a newly created calculated column. The calc column name is not recognized in the sum function. Here's my code
select name, is_open,
CASE WHEN text like '%perfect%' or '%amazing%' or '%happy%' or '%delicious%'or '%fabulous%'or '%fantastic%'or '%kind%' THEN 1
WHEN text like '%hate%'or '%horrible%'or '%bad%'or '%angry%'or '%fantastic%'or '%expensive%'or '%disgusting%' THEN -1
END sentiment_rating,
sum(sentiment_rating) as sum
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name
order by name ASC
It makes no sense to select both the unaggregated column and the aggregated column. So, just sum() the expression:
select name, is_open,
sum(CASE WHEN text like '%perfect%' or text like '%amazing%' or text like '%happy%' or text like '%delicious%' or text like '%fabulous%' or text like '%fantastic%' or text like '%kind%' THEN 1
WHEN text like '%hate%' or text like '%horrible%' or text like '%bad%' or text like '%angry%' or text like '%fantastic%'or '%expensive%' or text like '%disgusting%' THEN -1
END) as sum_sentiment_rating
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name, is_open
order by name ASC;
I'm not sure if you want one row per name or one row per name/is_open. I assumed the latter and added is_open to the GROUP BY.
All your LIKE conditions are syntactically wrong.
You can't have:
text like '%perfect%' or '%amazing%'....
the correct syntax is:
text like '%perfect%' or text like '%amazing%' ....
Then you must sum directly over the calculated with CASE column and include is_open in the grouping columns:
select name, is_open,
SUM(CASE
WHEN text like '%perfect%' or text like '%amazing%' or text like '%happy%' or text like '%delicious%' or text like '%fabulous%' or text like '%fantastic%' or text like '%kind%' THEN 1
WHEN text like '%hate%' or text like '%horrible%' or text like '%bad%' or text like '%angry%' or text like '%fantastic%' or text like '%expensive%' or text like '%disgusting%' THEN -1
END) as sumrating
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name, is_open
order by name ASC

SQL order by clause customized

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

Microsoft Access - Group By Feature

I'm wondering if there is a way to modify the results of one of my queries. Right now, I have a query in which the output shows the number of items in certain type of category by date, by using the Group By feature. So just as an example, if I enter 9/15/13 as the date the output would be as follows:
Apples 1
Bannas 5
Pears 16
Is there a way for it to just show Apples and total all other items into one category so it would output:
Apples 1
All Others 21
Thanks in advance for all of your help!
A bit difficult without table names or column names, but something along the lines of this (you'll have to enter it in SQL view)
Select
IIf([Product] = 'Apples', 'Apples', 'All Others'),
Count(*)
From
[Inventory]
Where
[InventoryDate] = [Enter Date]
Group By
IIf([Product] = 'Apples', 'Apples', 'All Others')
You need to replace Inventory with your table name, and Product with the column that contains the category and InventoryDate with the column that has the date. Count(*) might be sum([Quantity]), it depends on your structure.

SQL query: create category column based on a varchar column in table containing specific values

I have a table similar to the following:
Date Description Value1 Value2
01/01/2012 shiny colour 2 0
01/01/2012 yellow colour 2 2
03/01/2012 matt colour 2 2
03/01/2012 matt 4 1
03/01/2012 shiny 2 2
I want to write a SELECT SQL query (T-SQL) that will output all of the above columns but also display an extra column as the output of the SELECT statement whose value depends on the presence of the word "colour" in the Description (if "colour" is present it would be one value, if not it would show a different value).
(I would also want to display another extra column on top of that whose value depends on the presence of the words "matt" or "shiny" in the Description column. But I assume the method of doing this would be similar).
I believe I should be able to do this using the COALESCE function but I'm not familiar with this and am struggling to get anything working?
EXTENSION
Hey, thanks for your answers. They're really helpful. I have one more extension to the question. My second generated column relies on info in the first generated column. So something like:
SELECT *,
CASE
WHEN Description LIKE '%colour%' THEN 'SomeValue'
ELSE 'Unclassified'
END AS Category1,
CASE
WHEN AnotherColumn LIKE 'Something' THEN 'SomeValue'
WHEN Category1='Unclassified' THEN 'Unclassified'
ELSE 'Generic'
END AS Category2
FROM table_name
How do I get the output of Category2 to rely on output of Category1? I'm trying something like the above but it's not working.
My extension question was answered here: T-SQL CASE statement relies on another CASE statement in same SELECT query
SELECT *,
CASE WHEN Description LIKE '%colour%' THEN
1
ELSE
0
END AS HasColour,
CASE WHEN Description LIKE '%matt%' THEN
1
ELSE
0
END AS HasMatt,
CASE WHEN Description LIKE '%shiny%' THEN
1
ELSE
0
END AS HasShiny
FROM table_name
You would just add more columns for all the different words that you want to search for. Obviously you can change the return type of the columns to whatever you want, but I thought a boolean would be suitable in this situation.
Unless I misunderstand what you are asking, you could use a case statement:
SELECT Date,
Description,
Value1,
Value2,
Case when Description like '%colour%' then OTHERCOL else OTHERCOL2 end as Colourful,
Case when Description like '%matt%' then OTHERCOL else OTHERCOL2 end as Matt,
Case when Description like '%shiny%' then OTHERCOL else OTHERCOL2 end as Shiny,
FROM yourTable

SQL (SQLite) count for null-fields over all columns

I've got a table called datapoints with about 150 columns and 2600 rows. I know, 150 columns is too much, but I got this db after importing a csv and it is not possible to shrink the number of columns.
I have to get some statistical stuff out of the data. E.g. one question would be:
Give me the total number of fields (of all columns), which are null. Does somebody have any idea how I can do this efficiently?
For one column it isn't a problem:
SELECT count(*) FROM datapoints tb1 where 'tb1'.'column1' is null;
But how can I solve this for all columns together, without doing it by hand for every column?
Best,
Michael
Building on Lamak's idea, how about this idea:
SELECT (N * COUNT(*)) - (
COUNT(COLUMN_1)
+ COUNT(COLUMN_2)
+ ...
+ COUNT(COLUMN_N)
)
FROM DATAPOINTS;
where N is the number of columns. The trick will be in making the summation series of COUNT(column), but that shouldn't be too terrible with a good text editor and/or spreadsheet.
i don't think there is an easy way to do it. i'd get started on the 150 queries. you only have to replace one word (column name) each time.
Well, COUNT (and most aggregations funcions) ignore NULL values. In your case, since you are using COUNT(*), it counts every row in the table, but you can do that on any column. Something like this:
SELECT TotalRows-Column1NotNullCount, etc
FROM (
SELECT COUNT(1) TotalRows,
COUNT(column1) Column1NotNullCount,
COUNT(column2) Column2NotNullCount,
COUNT(column3) Column3NotNullCount ....
FROM datapoints) A
To get started it's often helpful to use a visual query tool to generate a field list and then use cut/paste/search/replace or manipulation in a spreadsheet program to transform it into what is needed. To do it all in one step you can use something like:
SELECT SUM(CASE COLUMN1 WHEN NULL THEN 1 ELSE 0 END) +
SUM(CASE COLUMN2 WHEN NULL THEN 1 ELSE 0 END) +
SUM(CASE COLUMN3 WHEN NULL THEN 1 ELSE 0 END) +
...
FROM DATAPOINTS;
With a visual query builder you can quickly generate:
SELECT COLUMN1, COLUMN2, COLUMN3 ... FROM DATAPOINTS;
You can then replace the comma with all the text that needs to appear between two field names followed by fixing up the first and last fields. So in the example search for "," and replace with " WHEN NULL 1 ELSE 0 END) + SUM(CASE " and then fix up the first and last fields.