merge two rows into one sql - sql

this is my table schema, total_hours column is the result of a sum function.
Id name client total_hours
1 John company 1 100
1 John company 2 200
2 Jack company 3 350
2 Jack company 2 150
I want to merge the rows with similar ID into one row, looking like this.
Id name client_a total_hours_a client_b total_hours_b
1 John company 1 100 company 2 200
2 Jack company 3 350 company 2 150
I tried to use pivot but this function does not seem to exist in Dbeaver. Here is my query
SELECT
client
,name
,sum(hours) AS total_hours
FROM pojects
GROUP BY client, name;
Thanks in advance if anyone could be of any help.

Related

select only one row in sum and group

i have a table TB_Orders , i save orders in thatl ,ike below :
userid
factorid
productname
economy
vip
count
1
A-123
name1
13000
18000
3
1
A-123
name2
9000
13000
4
2
A-124
name2
2000
5000
2
For example, user 1: has registered two orders with invoice number A-123.
And user 2 has only registered one order
I want to report as follows:
userid
factorid
total economy
total vip
total count
1
A-123
21000
31000
7
2
A-124
2000
5000
2
thanks for your help
This is an aggregation query:
select userid, factorid, sum(economy), sum(vip), sum(count)
from tb_orders
group by userid, factorid;
Aggregation is a very basic part of SQL functionality. If you don't recognize it, I would suggest that you brush up on your SQL skills with a tutorial, book, or something like that.

SQL for joining two tables and grouping by shared column

I want to join two tables and use the column they both share to group the results, including a null result for those accountIds which only appear in one table.
Table a
AccountId
productApurchases
Steve
1
Jane
5
Bill
10
Abed
2
Table b
AccountId
productApurchases
Allan
1
Jane
10
Bill
2
Abed
1
Mike
2
Desired output
AccountId
productApurchases
productBpurchases
Steve
1
0
Jane
5
10
Bill
10
2
Abed
2
1
Mike
0
2
I've been trying with various joins but cannot figure out how to group by all the account ids.
Any advice much appreciated, thanks.
Use full join:
select accountid,
coalesce(productApurchases, 0) as productApurchases,
coalesce(productBpurchases, 0) as productBpurchases
from a full join
b
using (accountid);

MS SQL Query to sum fields of duplicats

Hi please assist to write a query that will take the duplicate companies and result it as one row but sum the Duration - (time) spend at the company into one Duration field for each Employee
Company Duration Employee
Company 1 2 Koos
Company 2 6 Piet
Company 3 3 Piet
Company 1 3 Koos
Company 1 1 Koos
Company 1 7 Piet
Company 3 2 Koos
24
after query result
Company Duration Employee
Company 1 6 Koos
Company 2 6 Piet
Company 3 3 Piet
Company 1 7 Piet
Company 3 2 Koos
24
This should be a simple group by statement, you can just sum your duration based on your other two fields: company and employee:
SELECT c.company
,sum(c.Duration)
,c.Employee
FROM company c
GROUP BY c.company
,c.Employee
SQL Fiddle Demo

SQL - count without group by? I need to use two ids for a join

I thought I could count a column and add it as a column as I can with a sum but I get an error about having to group by / having. An example of what I want...
Initial table...
Global ID Local ID Name Role
100 1 Andy Manager
100 2 Andy Manager
100 1 John Co-Manager
200 1 Andy Co-Manager
200 2 John Manager
200 2 Mike Manager
I then want to add a column that counts the number of Manager in each group / local pairing...
Global ID Local ID Name Role Manager Count
100 1 Andy Manager 1
100 2 Andy Manager 1
100 1 John Co-Manager 0
200 1 Andy Co-Manager 0
200 2 John Manager 2
200 2 Mike Manager 2
I tried just joining the two tables on the global / local ID and then adding a column for count of the Role. The problem is that I get an error about grouping / having by but I don't want to group anything. I just want to add the column and still have the same number of rows. Any way around this?
FYI - for the last two rows, the last column has 2 because John and Mike are on the same group / local ID
It looks like your definition of a group is a unique combination of [global_id, local_id]. If that is the case you do want to group by those two values, and do a count, where the role is Manager. But because you want other columns from the original table, you must do that count within an inline view, and then join back to the original table, like so:
select t.*, v.mgr_in_grp
from tbl t
left join (select global_id, local_id, count(*) as mgr_in_grp
from tbl
where role = 'Manager'
group by global_id, local_id) v
on t.global_id = v.global_id
and t.local_id = v.local_id
Fiddle: http://sqlfiddle.com/#!2/fb3ace/2/0
Notice that there is a difference on row 3, as compared to your expected output.
John, Co-manager at global_id and local_id 100 and 1, respectively, belongs to a pair that has a manager in that combination (Andy, your first row). So the count appears should be 1.

SQL Sum count based on an identifier

Assume I have a table with the following data:
Name TransID Cost
---------------------------------------
Susan 1 10
Johnny 2 10
Johnny 3 9
Dave 4 10
I want to find a way to sum the Costs per name (assume the Names are unique) so that I get a table like this:
Name Cost
---------------------------------------
Susan 10
Johnny 19
Dave 10
Any help is appreciated.
This is relatively straightforward: you need to use a GROUP BY clause in your query:
SELECT Name,SUM(Cost)
FROM MyTable
GROUP BY Name