I have a table as below
ClientID AccountNumber BalanceOnDay0 BalanceOnDay1 BalanceOnDay2 BalanceOnDay3 BalanceOnDay4 BalanceOnDay5 BalanceOnDay6 BalanceOnDay7
ABC1 123 10 NULL NULL NULL NULL NULL NULL NULL
ABC1 123 NULL NULL NULL NULL NULL NULL NULL 3
I would like to see the result as beblow.
ClientID AccountNumber BalanceOnDay0 BalanceOnDay1 BalanceOnDay2 BalanceOnDay3 BalanceOnDay4 BalanceOnDay5 BalanceOnDay6 BalanceOnDay7
ABC1 123 10 NULL NULL NULL NULL NULL NULL 3
Please suggest!
You can use SUM() if you want to combine the balance values, if you have multiple records:
select clientid,
accountnumber,
sum(BalanceOnDay0) BalanceOnDay0,
sum(BalanceOnDay1) BalanceOnDay1,
sum(BalanceOnDay2) BalanceOnDay2,
sum(BalanceOnDay3) BalanceOnDay3,
sum(BalanceOnDay4) BalanceOnDay4,
sum(BalanceOnDay5) BalanceOnDay5,
sum(BalanceOnDay6) BalanceOnDay6,
sum(BalanceOnDay7) BalanceOnDay7
from table1
group by clientid, accountnumber
See SQL Fiddle with Demo
Related
Ihave the follwing data:
ID
Date1
Date2
Date3
1
2022/01/01
null
null
1
null
2021/04/01
null
2
2022/03/01
null
null
2
null
2021/06/01
null
3
2022/01/01
null
null
4
null
2021/04/01
null
and I'm trying to get the following result:
ID
Date1
Date2
Date3
1
2022/01/01
2021/04/01
null
2
2022/03/01
2021/06/01
null
3
2022/01/01
null
null
4
null
2021/04/01
null
trying group by ID, isn't working as I get the error expression not in group by key.
Any help is well received.
Aggregate by the ID column and then take the MAX() of the other three columns:
SELECT ID, MAX(Date1) AS Date1, MAX(Date2) AS Date2, MAX(Date3) AS Date3
FROM yourTable
GROUP BY ID;
Note that the MAX() aggregate function will by default ignore NULL values. Therefore, the non NULL values from each pair of similar ID records will be retained in the above query.
It's a bit confusing, so I'll try to exemplify in the table below:
example-table
id
data1
data2
data3
1
NULL
NULL
NULL
1
NULL
NULL
NULL
2
1
NULL
1
2
1
NULL
NULL
2
1
NULL
1
3
1
NULL
NULL
3
1
NULL
1
4
NULL
NULL
NULL
So, grouping by the same ID, I only want to display those IDs that all their data, from all columns, are null. In the table example above, only IDs 1 and 4.
The code I'm trying to use - just for reference:
select id
from example-table
group by id, data1, data2, data3
having data1 is null and data2 is null and data3 is null;
Any suggestions?
You can group by id only and set the conditions in the HAVING clause:
SELECT id
FROM tablename
GROUP BY id
HAVING COALESCE(MAX(data1), MAX(data2), MAX(data3)) IS NULL;
I am having a temp table with the following data:
tbl_t
id name_id date t1 t2 s1 s2
1 25 10/05/20 same same NULL NULL
2 23 11/05/21 same same home NULL
3 25 12/05/20 same NULL NULL NULL
4 25 13/06/20 NULL NULL NULL NULL
Desire output:
tbl_t
id name_id date t1 t2 s1 s2
2 23 11/05/21 same same home NULL
3 25 12/05/20 same NULL NULL NULL
I want to delete all rows where t1=t2 and s1=s1
I tried the following sql but i noticed that it is not working.
DELETE FROM tbl_t WHERE t1=t2 AND s1=s2
The problem are the NULL values. Use the NULL-safe comparison operator:
DELETE FROM tbl_t
WHERE t1 <=> t2 AND s1 <=> s2;
Almost any comparison with NULL results in NULL -- including NULL = NULL. And NULL values are treated as false in a WHERE clause (or equivalently WHERE clauses only keep rows where the condition evaluates unequivocally to true).
I have data in table like
ID desc Year pid
0006845503 tes1 null null
0006845503 null 2017 null
0006845503 null null 90
0006845503 tes2 null null
0006845503 null 2018 null
0006845503 null null 100
I want the result like
ID desc year pid
0006845503 tes1 2017 90
0006845503 tes2 2018 100
Used Max function it will return only one row. i want this to be working on a dynamic way. Please help
If you don't want any records with null value then
ex: select * from table where column_name is not null
If you don't want NULL value then use ISNULL function
ex: select isnull(col_name,'') from table
How do i return all rows from the timeslot table and also limit the rows from consultation table by employeeId
This query returns entries with the employeeId =1,
SELECT TimeSlot, conStart, firstName, lastName, phoneNumber
FROM TimeSlots
LEFT JOIN consultation
ON TimeSlots.TimeSlot = consultation.conStart
LEFT JOIN patient
ON consultation.patientId = patient.patientID
WHERE employeeId = 1
Which gives me this
11:00:00.0000000 11:00:00.0000000 chris wilson 6666666666
I would like to know how to show all the Timeslots but only show entries from
one employeeID
This is how i want it to turn out.
09:00:00.0000000 NULL NULL NULL NULL
09:20:00.0000000 NULL NULL NULL NULL
09:40:00.0000000 NULL NULL NULL NULL
10:00:00.0000000 NULL NULL NULL NULL
10:20:00.0000000 10:20 tim is 1111111111
10:40:00.0000000 NULL NULL NULL NULL
11:00:00.0000000 11:00 chris wilson 6666666666
11:20:00.0000000 NULL NULL NULL NULL
SELECT TimeSlot, conStart, firstName, lastName, phoneNumber
FROM TimeSlots
LEFT JOIN consultation
ON TimeSlots.TimeSlot = consultation.conStart
LEFT JOIN patient
ON consultation.patientId = patient.patientId AND employeeId = 1
`
Try this
SELECT TimeSlot, conStart, firstName, lastName, phoneNumber
FROM TimeSlots
LEFT JOIN (consultation
LEFT JOIN patient
ON consultation.patientId = patient.patientID)
ON TimeSlots.TimeSlot = consultation.conStart AND TimeSlots.employeeId = 1