Combine data from one table to another table at multiple level - sql

I have following table in sheet1.
Project| Resource Name | Line Manager
----------------------------------
2345 David Peter
2345 Tam Peter
2345 Richard Peter
2358 Russell Adam
Sheet2 Has following table
Project| Month |Revenue
-----------------------
2345 Jan 10000
2345 Feb 15000
2358 Jan 8000
2358 Feb 12000
how to get the following result in sheet2
Project| Month| Revenue| Resource|Name Line Manager
------------------------------------------------
2345 Jan 10000 David Peter
2345 Jan 10000 Tam Peter
2345 Jan 10000 Richard Peter
2345 Feb 15000 David Peter
2345 Feb 15000 Tam Peter
2345 Feb 15000 Richard Peter
2358 Jan 8000 Russell Adam
2358 Feb 12000 Russell Adam

how about:
select s1.*, s2.* from sheet1 s1 join sheet2 s2 on s2.project=s1.project

Related

Employee monthly report including leaves and working days

Consider one week report start Dates: from 11/1/2022 to 11/5/2022
Table : Timesheet
timesheet_id
start_time_server
Login_by
user_id
1234
11/1/2022
16:20:00 AM
jon 101
1235
11/1/2022
12:20:100 AM
tom 102
1236
11/2/2022
18:40:00 AM
tom 102
1237
11/3/2022
18:40:00 AM
tom 102
Table : Leaves
timesheet_id
Leave_applied_date
Leave_start_time_server
user_id
user_name
1234
11/1/2022
16:20:00 AM #########
101
jon
1234
11/1/2022
16:20:00 AM #########
101
jon
1234
11/1/2022
16:20:00 AM #########
102
jon
1234
11/1/2022
16:20:00 AM #########
103
jon
1237
11/3/2022
18:40:00 AM #########
102
tom
1237
11/3/2022
18:40:00 AM #########
102
tom
final output DailyWorkReport :
user_name
11/1/2022
11/2/2022
11/3/2022
11/4/2022
11/5/2022
jon
8
Leave
Leave
Leave
Leave
tom
8
8
8
Leave
Leave
Please help how i can achieve this final Dailyworkreprot.

How to change the Row Number behavior in my T-SQL Query?

I am using SQL Server 2014 and I have the following T-SQL query running against a table (tbl1).
Extract of tbl1:
emp_code Name Address Company
---------------------------------------
100 Peter London ABC
125 Allan Cambridge DCE
125 Allan Cambridge DCE
115 John Suffolk ABC
115 John Suffolk XYZ
154 Mary Highlands ABC
154 Mary Bristol ABC
124 Mary Chester ABC
My T-SQL query stands as follows:
SELECT
[ID],
[Name],
[Address],
[Company],
ROW_NUMBER() OVER (PARTITION BY [emp_code] ORDER BY [Address]) AS RowNumber
FROM
[tbl1]
Output from above query:
emp_code Name Address Company RowNumber
--------------------------------------------------------
100 Peter London ABC 1
125 Allan Cambridge DCE 1
125 Allan Cambridge DCE 2
115 John Suffolk ABC 1
115 John Suffolk XYZ 2
154 Mary Highlands ABC 1
154 Mary Bristol ABC 2
154 Mary Chester ABC 3
Output I'm after:
emp_code Name Address Company RowNumber
---------------------------------------------------------
100 Peter London ABC 1
125 Allan Cambridge DCE 1
125 Allan Cambridge DCE 1
115 John Suffolk ABC 1
115 John Suffolk XYZ 1
154 Mary Highlands ABC 1
154 Mary Bristol ABC 2
154 Mary Chester ABC 3
I want my RowNumber (or change the column name if need be) to change based on the [Address] column for each [emp_code]. If the employee has the SAME address, it should have the same value (that is, 1). Else, it should give the values as in the case of employee "Mary" (above output).
I am assuming the Row_Number() function is not the right one to be used for what I'm after.
Any help would be appreciated.
I think you want DENSE_RANK here rather than ROW_NUMBER():
SELECT [ID], [Name], [Address], [Company],
DENSE_RANK() OVER (PARTITION BY [emp_code]
ORDER BY [Address]) AS DenseRank
FROM [tbl1];
Demo

How to find sum of count, grouped by the id?

I would like to get the output for the following problem.
I have the following datatype:
id start end count Time Train
001 Paris London 01 05:00 Yes
001 Paris London 01 05:00 Yes
002 Prague Vienna 15 15:00 No
003 Frankfurt London 01 17:00 Yes
015 Paris London 08 21:00 No
019 Barcelona Vienna 15 15:00 No
003 Frankfurt London 01 07:00 Yes
002 Prague Vienna 15 05:00 No
I would like to find the sum of count, grouped by the id. Also ignore the rows that has the same id, start and end . Also I have a data of 4 gb and I would like to find the start and end city of top 5 count. Thank you.
I could like to get output that gives data something similar to this,
Prague -> Vienna Count : 15
Barcelona -> Vienna count : 15
Paris --> london Count : 09
Frankfurt -> London Count: 02
.....
You can use drop_duplicates + groupby with aggregating sum:
df['count'] = df['count'].astype(int)
df = df.drop_duplicates(['id','start','end'])
print (df)
id start end count Time Train
0 001 Paris London 1 05:00 Yes
2 002 Prague Vienna 15 15:00 No
3 003 Frankfurt London 1 07:00 Yes
4 015 Paris London 8 21:00 No
5 019 Barcelona Vienna 15 15:00 No
df1 = df.groupby('id', as_index=False)['count'].sum()
print (df1)
id count
0 001 1
1 002 15
2 003 1
3 015 8
4 019 15
df11 = df.groupby(['id', 'start', 'end'], as_index=False)['count'].sum()
print (df11)
id start end count
0 001 Paris London 1
1 002 Prague Vienna 15
2 003 Frankfurt London 1
3 015 Paris London 8
4 019 Barcelona Vienna 15
df12 = df.groupby(['start', 'end'], as_index=False)['count'].sum()
print (df12)
start end count
0 Barcelona Vienna 15
1 Frankfurt London 1
2 Paris London 9
3 Prague Vienna 15
For top values use nlargest:
df2 = df.nlargest(5, 'count')[['start','end']]
print (df2)
start end
2 Prague Vienna
5 Barcelona Vienna
4 Paris London
0 Paris London
3 Frankfurt London
SELECT T.* FROM
(
SELECT *,COUNT(id) AS count FROM TABLE1 GROUP BY id,start,end
) T
GROUP BY id ORDER BY count DESC LIMIT 0,5

SQL server select from 3 tables

I have three tables in my database Books, Borrowers and Movement:
Books
BookID Title Author Category Published
----------- ------------------------------ ------------------------- --------------- ----------
101 Ulysses James Joyce Fiction 1922-06-16
102 Huckleberry Finn Mark Twain Fiction 1884-03-24
103 The Great Gatsby F. Scott Fitzgerald Fiction 1925-06-17
104 1984 George Orwell Fiction 1949-04-19
105 War and Peace Leo Tolstoy Fiction 1869-08-01
106 Gullivers Travels Jonathan Swift Fiction 1726-07-01
107 Moby Dick Herman Melville Fiction 1851-08-01
108 Pride and Prejudice Jane Austen Fiction 1813-08-13
110 The Second World War Winston Churchill NonFiction 1953-09-01
111 Relativity Albert Einstein NonFiction 1917-01-09
112 The Right Stuff Tom Wolfe NonFiction 1979-09-07
121 Hitchhikers Guide to Galaxy Douglas Adams Humour 1975-10-27
122 Dad Is Fat Jim Gaffigan Humour 2013-03-01
131 Kick-Ass 2 Mark Millar Comic 2012-03-03
133 Beautiful Creatures: The Manga Kami Garcia Comic 2014-07-01
Borrowers
BorrowerID Name Birthday
----------- ------------------------- ----------
2 Bugs Bunny 1938-09-08
3 Homer Simpson 1992-09-09
5 Mickey Mouse 1928-02-08
7 Fred Flintstone 1960-06-09
11 Charlie Brown 1965-06-05
13 Popeye 1933-03-03
17 Donald Duck 1937-07-27
19 Mr. Magoo 1949-09-14
23 George Jetson 1948-04-08
29 SpongeBob SquarePants 1984-08-04
31 Stewie Griffin 1971-11-17
Movement
MoveID BookID BorrowerID DateOut DateIn ReturnCondition
----------- ----------- ----------- ---------- ---------- ---------------
1 131 31 2012-06-01 2013-05-24 good
2 101 23 2012-02-10 2012-03-24 good
3 102 29 2012-02-01 2012-04-01 good
4 105 7 2012-03-23 2012-05-11 good
5 103 7 2012-03-22 2012-04-22 good
6 108 7 2012-01-23 2012-02-12 good
7 112 19 2012-01-12 2012-02-10 good
8 122 11 2012-04-14 2013-05-01 poor
9 106 17 2013-01-24 2013-02-01 good
10 104 2 2013-02-24 2013-03-10 bitten
11 121 3 2013-03-01 2013-04-01 good
12 131 19 2013-04-11 2013-05-23 good
13 111 5 2013-05-22 2013-06-22 poor
14 131 2 2013-06-12 2013-07-23 bitten
15 122 23 2013-07-10 2013-08-12 good
16 107 29 2014-01-01 2014-02-14 good
17 110 7 2014-01-11 2014-02-01 good
18 105 2 2014-02-22 2014-03-02 bitten
What is a query I can use to find out which book was borrowed by the oldest borrower?
I am new to SQL and am using Microsoft SQL Server 2014
Here are two different solutions:
First using two sub querys and one equi-join:
select Title
from Books b , Movement m
where b.BookID = m.BookID and m.BorrowerID = (select BorrowerID
from Borrowers
where Birthday = (select MIN(Birthday)
from Borrowers))
Using two equi-joins and one sub query:
select Title
from Books b, Borrowers r, Movement m
where b.BookID = m.BookID
and m.BorrowerID = r.BorrowerID
and Birthday = (select MIN(Birthday) from Borrowers)
Both above queries give the following answer:
Title
------------------------------
Relativity

SQL: Grouping by 2 columns

I have a table points:
event_time | name | points |
------------------------------------
2014-07-16 11:40 Bob 10
2014-07-16 10:00 Jim 20
2014-07-16 09:20 Jim 30
2014-07-15 11:20 Bob 5
2014-07-15 10:20 Anna 10
2014-07-15 09:40 Bob 30
2014-07-15 09:00 Anna 10
Is it possible to make a query that results with:
event_date | name | total_points |
------------------------------------
2014-07-16 Bob 10
2014-07-16 Jim 50
2014-07-15 Bob 35
2014-07-15 Anna 20
Where total_points is a sum of all points for the given name during the day?
select date(event_time) as event_date,
name,
sum(points) as total_points
from points
group by date(event_time), name