How to retrieve a part of a value in a column - sql

Correction - I only need to Pick the WORK value every result set in the column will contain comma seperated values like below..
"SICK 0.08, WORK 0.08" or "SICK 0.08,WORK 0.08"
I only need to pick WORK 0.08 from this.
I am quite new to SQL
I am using the following script to get some results;
select Work.Work_summary, Work.emp_id
from Work
all work fine. but the first column has values like the following :
WORK 08.57, SICK 08.56 (Some columns)
SICK 07.80, WORK 06.80 , OT 02.00 (Some columns)
How can i only retrieve the column with only the WORK% value, if there is no WORK value the results shall be empty.

select Work_summary, emp_id
from Work
where Work_summary like '%WORK%'
This will return the rows in the Work table where Work_summary column contains the word WORK. See the documentation for more details.

Contains is faster than like.
SELECT Work_summary, emp_id FROM Work WHERE CONTAINS(Work_summary, 'WORK');

then use: this will give only the result where work summary contains work content.
select Work.Work_summary, Work.emp_id
from Work where contains(work.Work_summary ,'work');

select replace(Work_summary,",","") as work_summary
from Work
where upper(Work_summary) like '%WORK%'

Related

Stuck on column was specified multiple times...can't find the workaround for no alias on WHERE

So I have the following query that returns "The column 'ReceiverNo' was specified multiple times for location_4" error. I'm not an SQL expert but I know you cannot use an alias in a WHERE clause but I need to ensure those 2 similarly named columns match values in order to update the correct Estim row. Is there a work around for this that anyone is aware of? I know this has been asked but I found nothing quite this similar...
With location_4 as (
SELECT TOP (1) ReceiverDET.ReceiverNo, ReceiverDET.PartNo, Estim.User_Text4,
Receiver.ReceiverNo, Receiver.ReceiveDate
FROM Estim, ReceiverDET, Receiver
WHERE Receiver.ReceiverNo = ReceiverDET.ReceiverNo
AND Estim.PartNo = ReceiverDet.PartNo
ORDER BY Receiver.ReceiveDate DESC
)
UPDATE location_4
SET User_Text4 ='Not in Location'
WHERE User_Text4 = ''
If the above columns from the different tables do not match values it does not work, and there has to be a way... I've tried 100 different ways so far. When I remove the update statement it works fine and displays the proper row every time, it just breaks trying to actually use update. Any help would be greatly appreciated, thank you.

How to combine rows in BigQuery that share a similar name

i'm having trouble creating a query that'll group together responses from multiple rows that share a similar name and count the specific response record in them.
the datatable i currently have looks like this
test_control
values
test
selected
control
selected
test us
not selected
control us
selected
test mom
not selected
control mom
selected
what i'd like, is an output like the below that only counts the number of "selected" responses and groups together the rows that have either "control" or "test" in the name"
test_control
values
test
3
control
1
The query i have below is wrong as it doesn't give me an output of anything. The group by section is where im lost as i'm not sure how to do this. tried to google but couldn't seem to find anything. appreciate any help in advance!!!
SELECT distinct(test_control), values FROM `total_union`
where test_control="%test%" and values="selected"
group by test_control, values
use below
SELECT
REGEXP_EXTRACT(test_control, r'^(TEST|CONTROL) ') AS test_control,
COUNTIF(values = 'selected') AS values
FROM `total_union`
GROUP BY 1
As mentioned by #Mikhail Berlyant, you can use REGEX_EXTRACT to match the expression and COUNTIF to get the count of the total number of matching expressions according to the given condition. Try below code to get the expected output :
Code
SELECT
REGEXP_EXTRACT(test_control, r'^(test|control)') AS test_control,
COUNTIF(values = "selected") AS values
FROM `project.dataset.testvalues`
group by 1
Output

SQL ignore all blank fields in calculation

I am using the following query in SQL within PhpRunner:
SELECT
[Date],
(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance) + (MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance) AS Total
FROM Running
When I run the query I get a blank for an answer. Some of the fields will not necessarily be filled in for every record. The example above is just a snippet of all the fields that is in the table and in the complete calculation, there will be almost 90 fields in total. All the fields are from the same table.
What can I add to the query to treat the blank fields as blanks and not as zeros in order to still calculate the total despite some fields not being filled in? If there is anything that will do it automatically for all the fields it would be great.
I am aiming for something like this:
It looks like you need something like:
SELECT
[Date],
isnull(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance, 0)
+ isnull(MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance, 0)
AS Total
FROM Running

How to select all data from table but only display date-specific rows within DATE-data type column, in Oracle SQL?

I'm experiencing trouble returning a query to return all columns within a table but limited to the DATE-data-type "enroll_date" column containing '30-Jan-07'; the closest solution is with the below query but neither data is displayed nor the entire workbook-just the column-which leads me to believe that this is not just an issue with approach but perhaps a formatting issue as well.
SELECT TO_DATE(enroll_date, 'DD-MM-YY')
FROM student.enrollment
WHERE enroll_date= '30-Jan-07';
Again, I need to display all columns but only rows only specific to the date '30-Jan-07'. I'm sure a nested solution is ideal and somehow the right solution, but unfortunately my chops aren't there yet but I'm working on it! :D
UPDATE
Please see attached screenshot of output. The query/solution should retrieve all columns and rows enclosed within the red-rectangle mark-up-thank you!
One possible problem is that the date column has a time component (this is hidden in SQL). One method is to use trunc():
SELECT e.*
FROM student.enrollment e
WHERE TRUNC(e.enroll_date) = DATE '2007-01-30';
You can specify whichever columns you want in the following query:
SELECT col1, col2, col3, ...
FROM student.enrollment
WHERE TO_CHAR(enroll_date, 'DD-MON-YY') = '30-JAN-07';

Counting the no. of commas across multiple fields using SQL

I've 3 fields which contain only text. However, i want to add a calculated field which counts the number of commas in each of these 3 fields and displays it separately in the adjacent column. The snippet of SQL i use is shown below. How can i build the calculated field?
SELECT week, client_I, client_II, client_III
FROM quality_control_test;
Please advise!
well, you can "count" the number of a given character in a string, by using this:
length(c) - length(replace(c,',',''))
I'm assume you can figure out how to leverage that for your own query ;)