How to use group by with null values - sql

I have a SQL query which results as
personCount toatlMinute Meal QPWaiting NoOfCR CRNo
0 2 NULL NULL NULL NULL
1 7 NULL NULL NULL NULL
2 8 NULL NULL NULL NULL
3 16 NULL NULL NULL NULL
4 128 NULL NULL NULL NULL
0 NULL NULL 14 2 5555, 6666
3 NULL NULL NULL NULL NULL
4 NULL NULL NULL NULL NULL
2 NULL NULL 14 2 5555, 6666
3 NULL NULL 14 2 5555, 6666
4 NULL NULL 14 2 5555, 6666
0 NULL 2 NULL NULL NULL
Now I want to use group by 1st column (personcount) and want to sum 2nd and 3rd column
but don't want to sum 4th, 5th and 6th column. But output should show all data in one for unique person count.
SELECT
personCount,
SUM(toatlMinute) toatlMinute,
SUM(Meal) Meal,
QPWaiting,
NoOfNCR AS NoOfCR,
NCRNo AS CRNo
FROM
#OperatorData
GROUP BY
personCount, QPWaiting, NoOfNCR, NCRNo
and it gives output as
personCount toatlMinute Meal QPWaiting NoOfCR CRNo
0 2 2 NULL NULL NULL
0 NULL NULL 14 2 5555, 6666
1 7 NULL NULL NULL NULL
2 8 NULL NULL NULL NULL
2 NULL NULL 14 2 5555, 6666
3 16 NULL NULL NULL NULL
3 NULL NULL 14 2 5555, 6666
4 128 NULL NULL NULL NULL
4 NULL NULL 14 2 5555, 6666
A you can see there are two rows for 0 person count. However I want only one row.
How can get this output? Kindly advise

If you want to get values from the last three columns, so the NULL values go away, then use max() or min():
SELECT personCount, SUM(toatlMinute) as toatlMinute, SUM(Meal) as Meal,
max(QPWaiting) as QPWaiting, max(NoOfNCR) AS NoOfCR, max(NCRNo) AS CRNo
FROM #OperatorData
GROUP BY personCount;

Related

how can extract one row having not null values from a few of rows

for example
tableA has
id
condition_A
condition_B
condition_C
condition_D
AA
NULL
NULL
10
NULL
AA
15
NULL
NULL
NULL
AA
NULL
NULL
10
5
AA
NULL
20
NULL
NULL
AA
NULL
20
NULL
5
every condition has same value with same id
then i want to extract result like
id
condition_A
condition_B
condition_C
condition_D
AA
15
20
10
5
how can extract without null and only one rows?
Thank you :)
select max(condition_A), max(condition_B), max(Condition_C), max(Condition_D)
from tableA where id = 'AA'

Compare the column values where there are null and not null values

I want to compare the values from a table where there are null and not null values for the same ID ( If not null then I want minimum of two not null values). IF all the values are null for the given ID, I want the values to be displayed as null for the ID.
Input
ID Amount
1 Null
1 Null
1 Null
1 500
1 600
1 700
2 Null
2 Null
2 Null
2 Null
2 Null
3 Null
3 Null
3 300
3 600
3 200
Expected output
ID Amount
1 500 (min Not null value)
2 Null
3 200 (Min Not null value)
Simple grouping will do the trick:
select t.id, min(t.amount)
from table t
group by t.id

SQL merge and sum multiple rows into one

Essentially I have a SQL table that looks like this;
Name Week 1 Week 2 Week 3 Week 4 Week 5
James NULL 5 NULL NULL NULL
James 10 NULL NULL NULL NULL
James NULL NULL NULL 5 NULL
James NULL NULL NULL 20 NULL
Tom NULL NULL 5 NULL NULL
Tom NULL 10 NULL NULL NULL
Tom 25 NULL NULL NULL NULL
Tom NULL NULL NULL 5 NULL
Tom NULL NULL NULL 5 NULL
And I would want to combine together the rows (while also summing the values) to get something more like this;
Name Week 1 Week 2 Week 3 Week 4 Week 5
James 10 5 20 25 NULL
Tom 25 10 5 10 NULL
But I can't think of a good way of doing so. My data has quite the number of columns and rows so I'm looking for something which ideally doesn't require listing out all the individual column or row names.
Thanks
Just Sum the Rows and Group by name
select
Name
, Week1 = sum(Week 1)
, Week2 = sum(Week 2)
, Week3 = sum(Week 3)
, Week4 = sum(Week 4)
, Week5 = sum(Week 5)
from Table
group by Name

SQL nested loops

I have a problem, I have added a column to one of my database tables and now i need to populate this column with data.
Here are the tables:
Table QUESTIONNAIRE_QUESTIONS
with columns:
ID,
QUESTION_NUMBER,
PARENT_QUESTION_ID,
PARENT_QUESTIONNAIRE_ID,
QUESTION_CODE
Table QUESTIONNAIRE
with columns:
ID,
INTRODUCTION
What I first need to do is to find all unique QUESTIONNAIRE.ID and for each of those I need to find all QUESTIONNAIRE_QUESTIONS.ID that have this id as PARENT_QUESTIONNAIRE_ID and set the QUESTION_CODE as QUESTION_CODE=QUESTION_NUMBER*100.
Then, before moving on to the next QUESTIONNAIRE_ID i need to find all QUESTIONNAIRE_QUESTIONS.ID that have the current QUESTIONNAIRE_QUESTIONS.ID as PARENT_QUESTION_ID and set the QUESTION_CODE as
QUESTION_CODE=(parents question_code)+'.'+QUESTION_CODE.QUESTION_NUMBER*100.
How to perform this, complicated, nested loop? Seems like I have to use several loops?
Sample Data:
QUESTIONNAIRES
ID
1869359
1876176
QUESTIONNAIRE_QUESTIONS
ID QUESTION_NUMBER PARENT_QUESTIONNAIRE_ID PARENT_QUESTION_ID QUESTION_CODE
1869360 1 1869359 null null
1869362 2 1869359 null null
1869364 3 1869359 null null
1869367 1 null 1869364 null
1869369 1 1876176 null null
1869371 2 1876176 null null
1869372 3 1876176 null null
1869374 4 1876176 null null
1869377 1 null 1869372 null
And after i want it to look like this:
QUESTIONNAIRE_QUESTIONS
ID QUESTION_NUMBER PARENT_QUESTIONNAIRE_ID PARENT_QUESTION_ID QUESTION_CODE
1869360 1 1869359 null 100
1869362 2 1869359 null 200
1869364 3 1869359 null 300
1869367 1 null 1869364 300.100
1869368 1 null 1869367 300.100.100
1869369 2 null 1869367 300.100.200
1869370 1 1876176 null 100
1869371 2 1876176 null 200
1869372 3 1876176 null 300
1869374 4 1876176 null 400
1869377 1 null 1869371 200.100
Note that it can be more levels of "child questions"
Thanks!
UPDATE QUESTIONNAIRE_QUESTIONS
SET QUESTION_CODE = (QUESTION_NUMBER * 100)
WHERE QUESTIONNAIRE_QUESTIONS.ID IN (SELECT DISTINCT QUESTIONNAIRE.ID
FROM QUESTIONNAIRE, QUESTIONNAIRE_QUESTIONS
WHERE QUESTIONNAIRE_QUESTIONS.ID = PARENT_QUESTION_ID)
Not so sure about the 2nd question. Is that a typo?QUESTION_CODE.QUESTION_NUMBER*100

Merging of rows based on data

In my application I am using SQL Server. The output of one of the queries is:
Name countA countB countC countD countE countF
Name1 NULL NULL 1 NULL NULL NULL
Name2 NULL NULL 1 NULL NULL NULL
Name2 NULL NULL NULL NULL NULL 1
Name3 NULL NULL NULL NULL NULL 1
Name3 NULL NULL 6 NULL NULL NULL
I am expecting the output to be a single row representing the count data as is shown below.
Name countA countB countC countD countE countF
Name1 NULL NULL 1 NULL NULL NULL
Name2 NULL NULL 1 NULL NULL 1
Name3 NULL NULL 6 NULL NULL 1
What do I need to change to fix this query?
Looking at the data, you should just be able to apply a GROUP BY to the query based on Name, and apply a SUM aggregate to countA, countB, etc.