Combining duplicate values into 1 row? - sql

please see attached the image (this is the table I am looking at replicated in excel called dbo.StaffDetails)
This row has been duplicated because the surgeon 1 code 127 has been incorrectly input in surgeon1code instead of surgeon2code.. is it possible to write a query that when the SourceID is the same and the surgeon1, surgeon2, surgeon3 codes are not on the same row to actually merge them all? (I hope this makes sense?)
So ideally, the attached image would have the 127 as Surgeon2code instead of being on its own row?
Many thanks for any help with this, I really appreciate it

You can use grouping. Something like:
SELECT SourceID,
MIN(Surgeon1Code) AS Surgeon1Code,
MAX(Surgeon1Code) AS Surgeon2Code,
MAX(Surgeon1Code) AS Surgeon3Code,
MAX(Anaesthetist1Code) AS Anaesthetist1Code,
....
FROM dbo.StaffDetails
GROUP BY SourceID

Related

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

Corresponding rows sql

Can we do this in SQL
Col1,Col2
Asset,1234
Date,1/1/2020
Date2,1/31/2020
Value,10
Asset,12344
Date,1/10/2020
Date2,1/21/2020
Value,45
Asset,12345
Date,1/15/2020
Date2,1/36/2020
Value,99
I have the asset numbers I want to query, but I would like to get corresponding rows as well.
So, when I write where Asset=12344, I need asset,Date, Date2, Value information for asset 12344.
Never came across this before. Is there any way we could achieve this.
Thanks.

Returns rows where the grouping a column returns more than one unique values in another column

Alright, so I'm trying to merge some fairly messy databases. I have a column of gvkeys that I matched with permnos. Some of the gvkeys were matched with more than one permno so I'm trying to look at the instances where this happened.
I've got something that looks like this:
df.groupby('gvkey').LPERMNO.nunique() > 1
which gets me a series that has "true" next to all the gvkeys I'm interested in.
I'm hoping to run something like
gvkeys = df.groupby('gvkey').LPERMNO.nunique() > 1
df.loc(gvkey in gvkeys)
but clearly that's not quite right. Thanks in adavance for your help!

how can I summarize different columns to make totals by row?

how can I summarize different columns to make totals by row?
on the picture below you can see my statement, definitely is something wrong there because is returning NULL value, but I don't know what it is. I want to create a TOTAL column summarizing WOSE, WO, SSSE and SS per row. Could someone help me with that?
It is because of null values in the columns -Use the following instead -
SUM(COALESCE(WOSE,0) +COALESCE(WO,0) + COALESCE(SSSE,0)+COALESCE(SS,0))

How to retrieve a part of a value in a column

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%'