Sum Decode statement SQL - sql

I am trying to sum a few Decode statements and column names, but am having difficulties.
currently it is showing as
rank | name | points
----------------------
0 | john | 0
0 | john | 40
1 | john | 30
2 | tom | 22
0 | tom | 0
I expect to have this result:
rank | name | points
----------------------
1 | john | 70
2 | tom | 22
Query:
Select Rank, Name, Code, Points
From
(select
decode(Table.name, 'condition1', Table.value) As Points,
decode(Table.name, 'Condition2', Table.value) As Rank,
Employee.name as Name,
Employee.GA1 as Code
from Table
inner Join Employee
on Empolyee.positionseq = name.positionseq
where Table.name IN ('Condition1', 'Condition2')
);

Select MAX(Rank), Name, Code, SUM(Points)
From
(select
decode(Table.name, 'condition1', Table.value) As Points
decode(Table.name, 'Condition2', Table.value) As Rank
,Employee.name as Name
,Employee.GA1 as Code
from Table
inner Join Employee
on Employee.positionseq = name.positionseq
where Table.name IN( 'Condition1', 'Condition2'))
GROUP BY Employee.id;
I added the SUM, MAX (for rank) and GROUP BY statements. Also corrected some misspellings (Empolyee)

I may be understanding your question incorrectly, however, it seems like you are trying to do the following (omitting inner join for simplicity):
Select MAX(rank), name, SUM(points)
FROM UserRanks
GROUP BY name
Based on your data set above, you should get the following results:
rank name points
1 john 70
2 tom 22

Related

select max group by without id for join

I have table like this :
id name value
1 roger 43
2 phil 12
3 zac 14
4 phil 42
5 maurice 450
...
and i'm trying to retrieve the max value for each name in order to do a join later.
I'm expecting the intermediate result to be something like this :
name value
roger 43
zac 14
phil 42
maurice 450
And this is easily achieved using and select name,max(value) from table group by name
My issue is that i NEED the id in order to later be able to do my join. but if i add my id to the aggregate/ group by it will mess up the result and will show all values since the id will be different.
So the true expect result is more like this :
id name value
1 roger 43
3 zac 14
4 phil 42
5 maurice 450
I have seen many question regarding similar issues but none where the id need to be retrieved but not included in the group by since i want uniqness for the name and only need the id for my join.
From what you describe, you just want the max of id:
select max(id) as id, name, max(value)
from table
group by name;
Or, what I think you want is the row with the max value:
select distinct on (name) t.*
from t
order by name, value desc;
With not exists:
select min(t.id) id, t.name, t.value
from tablename t
where not exists (
select 1 from tablename
where name = t.name and value > t.value
)
group by t.name, t.value
order by id
See the demo.
Results:
| id | name | value |
| --- | ------- | ----- |
| 1 | roger | 43 |
| 3 | zac | 14 |
| 4 | phil | 42 |
| 5 | maurice | 450 |
Try this:
WITH X (n, v) AS (
SELECT name, MAX(value) FROM tbl GROUP BY name
)
SELECT T.*
FROM tbl AS T INNER JOIN X ON T.name = X.n AND T.value = X.v

Count how many times a value appears in tables SQL

Here's the situation:
So, in my database, a person is "responsible" for job X and "linked" to job Y. What I want is a query that returns: name of person, his ID and he number of jobs it's linked/responsible. So far I got this:
select id_job, count(id_job) number_jobs
from
(
select responsible.id
from responsible
union all
select linked.id
from linked
GROUP BY id
) id_job
GROUP BY id_job
And it returns a table with id in the first column and number of occurrences in the second. Now, what I can't do is associate the name of person to the table. When i put that in the "select" from beginning it gives me all the possible combinations... How can I solve this? Thanks in advance!
Example data and desirable output:
| Person |
id | name
1 | John
2 | Francis
3 | Chuck
4 | Anthony
| Responsible |
process_no | id
100 | 2
200 | 2
300 | 1
400 | 4
| Linked |
process_no | id
101 | 4
201 | 1
301 | 1
401 | 2
OUTPUT:
| OUTPUT |
id | name | number_jobs
1 | John | 3
2 | Francis | 3
3 | Chuck | 0
4 | Anthony | 2
Try this way
select prs.id, prs.name, count(*) from Person prs
join(select process_no, id
from Responsible res
Union all
select process_no, id
from Linked lin ) a on a.id=prs.id
group by prs.id, prs.name
I would recommend aggregating each of the tables by the person and then joining the results back to the person table:
select p.*, coalesce(r.cnt, 0) + coalesce(l.cnt, 0) as numjobs
from person p left join
(select id, count(*) as cnt
from responsible
group by id
) r
on r.id = p.id left join
(select id, count(*) as cnt
from linked
group by id
) l
on l.id = p.id;
select id, name, count(process_no) FROM (
select pr.id, pr.name, res.process_no from Person pr
LEFT JOIN Responsible res on pr.id = res.id
UNION
select pr.id, pr.name, lin.process_no from Person pr
LEFT JOIN Linked lin on pr.id = lin.id) src
group by id, name
order by id
Query ain't tested, give it a shot, but this is the way you want to go

SQL - select distinct only on one column [duplicate]

This question already has answers here:
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
Closed 9 years ago.
I have searched far and wide for an answer to this problem. I'm using a Microsoft SQL Server, suppose I have a table that looks like this:
+--------+---------+-------------+-------------+
| ID | NUMBER | COUNTRY | LANG |
+--------+---------+-------------+-------------+
| 1 | 3968 | UK | English |
| 2 | 3968 | Spain | Spanish |
| 3 | 3968 | USA | English |
| 4 | 1234 | Greece | Greek |
| 5 | 1234 | Italy | Italian |
I want to perform one query which only selects the unique 'NUMBER' column (whether is be the first or last row doesn't bother me). So this would give me:
+--------+---------+-------------+-------------+
| ID | NUMBER | COUNTRY | LANG |
+--------+---------+-------------+-------------+
| 1 | 3968 | UK | English |
| 4 | 1234 | Greece | Greek |
How is this achievable?
A very typical approach to this type of problem is to use row_number():
select t.*
from (select t.*,
row_number() over (partition by number order by id) as seqnum
from t
) t
where seqnum = 1;
This is more generalizable than using a comparison to the minimum id. For instance, you can get a random row by using order by newid(). You can select 2 rows by using where seqnum <= 2.
Since you don't care, I chose the max ID for each number.
select tbl.* from tbl
inner join (
select max(id) as maxID, number from tbl group by number) maxID
on maxID.maxID = tbl.id
Query Explanation
select
tbl.* -- give me all the data from the base table (tbl)
from
tbl
inner join ( -- only return rows in tbl which match this subquery
select
max(id) as maxID -- MAX (ie distinct) ID per GROUP BY below
from
tbl
group by
NUMBER -- how to group rows for the MAX aggregation
) maxID
on maxID.maxID = tbl.id -- join condition ie only return rows in tbl
-- whose ID is also a MAX ID for a given NUMBER
You will use the following query:
SELECT * FROM [table] GROUP BY NUMBER;
Where [table] is the name of the table.
This provides a unique listing for the NUMBER column however the other columns may be meaningless depending on the vendor implementation; which is to say they may not together correspond to a specific row or rows.

SQL Select with multiple IDs

I have an Address History table with three fields: EmpID, Address, AddrID.
Every time I add a new address, I also increment the Address ID (AddrID) by 1 for that particular employee.
EmpID | AddrID | Address
-------------------------------
1 | 1 | 1234 First Ave
1 | 2 | 2145 First Ave
1 | 3 | 1111 First Ave
2 | 1 | 1001 Second St
2 | 2 | 1002 Second St
2 | 3 | 1003 Second St
2 | 4 | 2222 Second St
3 | 1 | 3332 Third Lane
3 | 2 | 3333 Third Lane
4 | 1 | 4444 Fourth Way
How do I get the most recent address (highest Address ID) for each employee? Ideally, I should be able to return:
EmpID | AddrID | Address
-------------------------------
1 | 3 | 1111 First Ave
2 | 4 | 2222 Second St
3 | 2 | 3333 Third Lane
4 | 1 | 4444 Fourth Way
So far I have either returned too many results (ie, every Employee, every AddrID 1, and every Address associated with the two), or too few results (ie, every Employee with an AddrID 4 - just Employee 2).
I have tried using Distinct, Group By, Order By, Having, and Self-Joins to no avail.
What am I missing?
You can use a subquery that gets the MAX() addrid for each empid:
select t1.empid,
t1.addrid,
t1.address
from table1 t1
inner join
(
select max(addrid) addrid, empid
from table1
group by empid
) t2
on t1.empid = t2.empid
and t1.addrid = t2.addrid
See SQL Fiddle With Demo
The inner query will return the max addrid and the empid then you join your table to that result on those two values this will limit the records that get returned.
The following should work:
SELECT *
FROM (SELECT empid,
Max(addrid) AS AddrID
FROM t1
GROUP BY empid) a
JOIN t1 b
ON b.empid = a.empid
AND b.addrid = a.addrid
See it in action
I would probably organize the table differently, but you could do something with a sub-join
select * from address_history
join
(select EmpID, max(AddrID) max_address from Address_History group by empId) as latest_addresses
on address_history.empid = latest_addresses
and address_history.addrId = latest_addresses.max_address

Collapse SQL rows

Say I have this table:
id | name
-------------
1 | john
2 | steve
3 | steve
4 | john
5 | steve
I only want the rows that are unique compared to the previous row, these:
id | name
-------------
1 | john
2 | steve
4 | john
5 | steve
I can partly achieve this by using this query:
SELECT *, (
SELECT `name` FROM demotable WHERE id=t.id-1
) AS prevName FROM demotable AS t GROUP BY prevName ORDER BY id ASC
But when I am using a query with multiple UNIONs and stuff, this gets way to complicated. Is there an easy way to do this (like GROUP BY, but than more specific)?
This should work, but I don't know if it's simpler :
select demotable.*
from demotable
left join demotable as prev on prev.id = demotable.id - 1
where demotable.name != prev.name