Joins in Stored Procedure
Below is my code, which returns billable and non billable total hours for their projects. I want to show in single row, but i am getting two result sets like below.
DECLARE @UserName NVARCHAR(50);
select @UserName = UserName
from UserRegistration
where UserID = @UserID;
SET @sqlText =
N'SELECT''' + @UserName + ''' AS UserName, isnull(
SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 1
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
SET @sqlText4 =
N'SELECT''' + @UserName + '''AS UserName,
isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Non Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 2
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
set @sqlText = REPLACE(@sqlText,',SUM','+SUM')
set @sqlText4 = REPLACE(@sqlText4,',SUM','+SUM')
My result is:
UserName | BillableHours
------------------------------
myName | 20
-----------------------------
UserName | NonBillableHours
-------------------------------
myName | 30
-------------------------------
Expected result should be like this:
UserName | BillableHours | NonBillableHours
-----------------------------------------------------
myName | 20 | 30
-----------------------------------------------------
sql-server t-sql stored-procedures join
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Below is my code, which returns billable and non billable total hours for their projects. I want to show in single row, but i am getting two result sets like below.
DECLARE @UserName NVARCHAR(50);
select @UserName = UserName
from UserRegistration
where UserID = @UserID;
SET @sqlText =
N'SELECT''' + @UserName + ''' AS UserName, isnull(
SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 1
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
SET @sqlText4 =
N'SELECT''' + @UserName + '''AS UserName,
isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Non Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 2
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
set @sqlText = REPLACE(@sqlText,',SUM','+SUM')
set @sqlText4 = REPLACE(@sqlText4,',SUM','+SUM')
My result is:
UserName | BillableHours
------------------------------
myName | 20
-----------------------------
UserName | NonBillableHours
-------------------------------
myName | 30
-------------------------------
Expected result should be like this:
UserName | BillableHours | NonBillableHours
-----------------------------------------------------
myName | 20 | 30
-----------------------------------------------------
sql-server t-sql stored-procedures join
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34
add a comment |
Below is my code, which returns billable and non billable total hours for their projects. I want to show in single row, but i am getting two result sets like below.
DECLARE @UserName NVARCHAR(50);
select @UserName = UserName
from UserRegistration
where UserID = @UserID;
SET @sqlText =
N'SELECT''' + @UserName + ''' AS UserName, isnull(
SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 1
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
SET @sqlText4 =
N'SELECT''' + @UserName + '''AS UserName,
isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Non Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 2
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
set @sqlText = REPLACE(@sqlText,',SUM','+SUM')
set @sqlText4 = REPLACE(@sqlText4,',SUM','+SUM')
My result is:
UserName | BillableHours
------------------------------
myName | 20
-----------------------------
UserName | NonBillableHours
-------------------------------
myName | 30
-------------------------------
Expected result should be like this:
UserName | BillableHours | NonBillableHours
-----------------------------------------------------
myName | 20 | 30
-----------------------------------------------------
sql-server t-sql stored-procedures join
Below is my code, which returns billable and non billable total hours for their projects. I want to show in single row, but i am getting two result sets like below.
DECLARE @UserName NVARCHAR(50);
select @UserName = UserName
from UserRegistration
where UserID = @UserID;
SET @sqlText =
N'SELECT''' + @UserName + ''' AS UserName, isnull(
SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 1
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
SET @sqlText4 =
N'SELECT''' + @UserName + '''AS UserName,
isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' as [Non Billable Hours]
FROM dbo.timesheet
where month ='''+ @strMonth+'''
and [year] =' + Cast(@year1 AS VARCHAR(max))+ '
and [Task Type] = 2
and [Task ID] in(
select TaskID
from ManageTasks
where TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
set @sqlText = REPLACE(@sqlText,',SUM','+SUM')
set @sqlText4 = REPLACE(@sqlText4,',SUM','+SUM')
My result is:
UserName | BillableHours
------------------------------
myName | 20
-----------------------------
UserName | NonBillableHours
-------------------------------
myName | 30
-------------------------------
Expected result should be like this:
UserName | BillableHours | NonBillableHours
-----------------------------------------------------
myName | 20 | 30
-----------------------------------------------------
sql-server t-sql stored-procedures join
sql-server t-sql stored-procedures join
edited Sep 3 '15 at 11:51
Paul White♦
52.9k14281457
52.9k14281457
asked Sep 3 '15 at 9:01
user74216user74216
6
6
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 10 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34
add a comment |
3
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34
3
3
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34
add a comment |
3 Answers
3
active
oldest
votes
Depending what the colum is for your userID (i've declared it as TeamMemberUserID which you are using later)
Simply Join the timesheet table to the User Registration table on the TeamMemberUserID and select the user.name field
End result looks something like:
SET @sqlText = N'SELECT user.name as [Name], isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' FROM dbo.timesheet as ts LEFT JOIN UserRegistrations user ON user.TeamMemberUserID = ts.TeamMemberUserID where month ='''+
@strMonth+''' and [year] =' + Cast(@year1 AS VARCHAR(max))+
'and [Task ID] in(select TaskID from ManageTasks where
TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
I'm not sure what your dynamic columns are so I cant 100% promise this will work, but if you get any column errors you should just be able to put ts. infront of it to declare its from that table
Ste
add a comment |
I have assumed the ID field that would be used to join the two tables. Let me know what field you would use to join these tables and I will update the answer. You should also specify the correct alias in your @columns variable.
I also assumed the UserRegistration table schema to be dbo.
SET @sqlText = N' SELECT ISNULL(SUM(ISNULL(['+REPLACE(@columns,',','],0))
,SUM(isnull([') + '],0)), 0)
,ur.Name
FROM dbo.timesheet t
JOIN dbo.UserRegistration ur ON t.TimeSheetID = ur.TimeSheetID
WHERE month ='''+ @strMonth +'''
AND [year] =' + Cast(@year1 AS VARCHAR(max))+ '
AND [Task ID] IN (
SELECT TaskID
FROM ManageTasks
WHERE TeamMemberUserID ='+ CAST(@UserID AS VARCHAR(MAX)) +'
)'
add a comment |
The question is really unclear. It's not obvious the database structure that you are working with. Your problem looks as though it would be solved with a self-join but am only guessing.
Here's one possible (much simplified to show general principles) solution. In sqlite as I don't have SQL Server.
CREATE TABLE `timesheet` (
`user_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_name` TEXT,
`task_type` INTEGER,
`hours` INTEGER
);
delete from timesheet;
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 1, 10, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 2, 6, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 1, 7, 2);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 2, 9, 2););
select a.user_name, a.hours as billable_hours, b.hours as non_billable_hours
from timesheet a
join timesheet b
on a.user_id = b.user_id
and b.task_type = 2
where a.task_type = 1
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f113052%2fjoins-in-stored-procedure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Depending what the colum is for your userID (i've declared it as TeamMemberUserID which you are using later)
Simply Join the timesheet table to the User Registration table on the TeamMemberUserID and select the user.name field
End result looks something like:
SET @sqlText = N'SELECT user.name as [Name], isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' FROM dbo.timesheet as ts LEFT JOIN UserRegistrations user ON user.TeamMemberUserID = ts.TeamMemberUserID where month ='''+
@strMonth+''' and [year] =' + Cast(@year1 AS VARCHAR(max))+
'and [Task ID] in(select TaskID from ManageTasks where
TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
I'm not sure what your dynamic columns are so I cant 100% promise this will work, but if you get any column errors you should just be able to put ts. infront of it to declare its from that table
Ste
add a comment |
Depending what the colum is for your userID (i've declared it as TeamMemberUserID which you are using later)
Simply Join the timesheet table to the User Registration table on the TeamMemberUserID and select the user.name field
End result looks something like:
SET @sqlText = N'SELECT user.name as [Name], isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' FROM dbo.timesheet as ts LEFT JOIN UserRegistrations user ON user.TeamMemberUserID = ts.TeamMemberUserID where month ='''+
@strMonth+''' and [year] =' + Cast(@year1 AS VARCHAR(max))+
'and [Task ID] in(select TaskID from ManageTasks where
TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
I'm not sure what your dynamic columns are so I cant 100% promise this will work, but if you get any column errors you should just be able to put ts. infront of it to declare its from that table
Ste
add a comment |
Depending what the colum is for your userID (i've declared it as TeamMemberUserID which you are using later)
Simply Join the timesheet table to the User Registration table on the TeamMemberUserID and select the user.name field
End result looks something like:
SET @sqlText = N'SELECT user.name as [Name], isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' FROM dbo.timesheet as ts LEFT JOIN UserRegistrations user ON user.TeamMemberUserID = ts.TeamMemberUserID where month ='''+
@strMonth+''' and [year] =' + Cast(@year1 AS VARCHAR(max))+
'and [Task ID] in(select TaskID from ManageTasks where
TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
I'm not sure what your dynamic columns are so I cant 100% promise this will work, but if you get any column errors you should just be able to put ts. infront of it to declare its from that table
Ste
Depending what the colum is for your userID (i've declared it as TeamMemberUserID which you are using later)
Simply Join the timesheet table to the User Registration table on the TeamMemberUserID and select the user.name field
End result looks something like:
SET @sqlText = N'SELECT user.name as [Name], isnull(SUM(isnull(['+REPLACE(@columns,',','],0)),
SUM(isnull([') + '],0)), 0)' +' FROM dbo.timesheet as ts LEFT JOIN UserRegistrations user ON user.TeamMemberUserID = ts.TeamMemberUserID where month ='''+
@strMonth+''' and [year] =' + Cast(@year1 AS VARCHAR(max))+
'and [Task ID] in(select TaskID from ManageTasks where
TeamMemberUserID ='+ Cast(@UserID AS VARCHAR(max)) +')'
I'm not sure what your dynamic columns are so I cant 100% promise this will work, but if you get any column errors you should just be able to put ts. infront of it to declare its from that table
Ste
answered Sep 3 '15 at 9:16
Ste BovSte Bov
1,548411
1,548411
add a comment |
add a comment |
I have assumed the ID field that would be used to join the two tables. Let me know what field you would use to join these tables and I will update the answer. You should also specify the correct alias in your @columns variable.
I also assumed the UserRegistration table schema to be dbo.
SET @sqlText = N' SELECT ISNULL(SUM(ISNULL(['+REPLACE(@columns,',','],0))
,SUM(isnull([') + '],0)), 0)
,ur.Name
FROM dbo.timesheet t
JOIN dbo.UserRegistration ur ON t.TimeSheetID = ur.TimeSheetID
WHERE month ='''+ @strMonth +'''
AND [year] =' + Cast(@year1 AS VARCHAR(max))+ '
AND [Task ID] IN (
SELECT TaskID
FROM ManageTasks
WHERE TeamMemberUserID ='+ CAST(@UserID AS VARCHAR(MAX)) +'
)'
add a comment |
I have assumed the ID field that would be used to join the two tables. Let me know what field you would use to join these tables and I will update the answer. You should also specify the correct alias in your @columns variable.
I also assumed the UserRegistration table schema to be dbo.
SET @sqlText = N' SELECT ISNULL(SUM(ISNULL(['+REPLACE(@columns,',','],0))
,SUM(isnull([') + '],0)), 0)
,ur.Name
FROM dbo.timesheet t
JOIN dbo.UserRegistration ur ON t.TimeSheetID = ur.TimeSheetID
WHERE month ='''+ @strMonth +'''
AND [year] =' + Cast(@year1 AS VARCHAR(max))+ '
AND [Task ID] IN (
SELECT TaskID
FROM ManageTasks
WHERE TeamMemberUserID ='+ CAST(@UserID AS VARCHAR(MAX)) +'
)'
add a comment |
I have assumed the ID field that would be used to join the two tables. Let me know what field you would use to join these tables and I will update the answer. You should also specify the correct alias in your @columns variable.
I also assumed the UserRegistration table schema to be dbo.
SET @sqlText = N' SELECT ISNULL(SUM(ISNULL(['+REPLACE(@columns,',','],0))
,SUM(isnull([') + '],0)), 0)
,ur.Name
FROM dbo.timesheet t
JOIN dbo.UserRegistration ur ON t.TimeSheetID = ur.TimeSheetID
WHERE month ='''+ @strMonth +'''
AND [year] =' + Cast(@year1 AS VARCHAR(max))+ '
AND [Task ID] IN (
SELECT TaskID
FROM ManageTasks
WHERE TeamMemberUserID ='+ CAST(@UserID AS VARCHAR(MAX)) +'
)'
I have assumed the ID field that would be used to join the two tables. Let me know what field you would use to join these tables and I will update the answer. You should also specify the correct alias in your @columns variable.
I also assumed the UserRegistration table schema to be dbo.
SET @sqlText = N' SELECT ISNULL(SUM(ISNULL(['+REPLACE(@columns,',','],0))
,SUM(isnull([') + '],0)), 0)
,ur.Name
FROM dbo.timesheet t
JOIN dbo.UserRegistration ur ON t.TimeSheetID = ur.TimeSheetID
WHERE month ='''+ @strMonth +'''
AND [year] =' + Cast(@year1 AS VARCHAR(max))+ '
AND [Task ID] IN (
SELECT TaskID
FROM ManageTasks
WHERE TeamMemberUserID ='+ CAST(@UserID AS VARCHAR(MAX)) +'
)'
answered Sep 3 '15 at 9:23
James AndersonJames Anderson
5,06021839
5,06021839
add a comment |
add a comment |
The question is really unclear. It's not obvious the database structure that you are working with. Your problem looks as though it would be solved with a self-join but am only guessing.
Here's one possible (much simplified to show general principles) solution. In sqlite as I don't have SQL Server.
CREATE TABLE `timesheet` (
`user_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_name` TEXT,
`task_type` INTEGER,
`hours` INTEGER
);
delete from timesheet;
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 1, 10, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 2, 6, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 1, 7, 2);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 2, 9, 2););
select a.user_name, a.hours as billable_hours, b.hours as non_billable_hours
from timesheet a
join timesheet b
on a.user_id = b.user_id
and b.task_type = 2
where a.task_type = 1
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
add a comment |
The question is really unclear. It's not obvious the database structure that you are working with. Your problem looks as though it would be solved with a self-join but am only guessing.
Here's one possible (much simplified to show general principles) solution. In sqlite as I don't have SQL Server.
CREATE TABLE `timesheet` (
`user_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_name` TEXT,
`task_type` INTEGER,
`hours` INTEGER
);
delete from timesheet;
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 1, 10, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 2, 6, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 1, 7, 2);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 2, 9, 2););
select a.user_name, a.hours as billable_hours, b.hours as non_billable_hours
from timesheet a
join timesheet b
on a.user_id = b.user_id
and b.task_type = 2
where a.task_type = 1
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
add a comment |
The question is really unclear. It's not obvious the database structure that you are working with. Your problem looks as though it would be solved with a self-join but am only guessing.
Here's one possible (much simplified to show general principles) solution. In sqlite as I don't have SQL Server.
CREATE TABLE `timesheet` (
`user_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_name` TEXT,
`task_type` INTEGER,
`hours` INTEGER
);
delete from timesheet;
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 1, 10, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 2, 6, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 1, 7, 2);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 2, 9, 2););
select a.user_name, a.hours as billable_hours, b.hours as non_billable_hours
from timesheet a
join timesheet b
on a.user_id = b.user_id
and b.task_type = 2
where a.task_type = 1
The question is really unclear. It's not obvious the database structure that you are working with. Your problem looks as though it would be solved with a self-join but am only guessing.
Here's one possible (much simplified to show general principles) solution. In sqlite as I don't have SQL Server.
CREATE TABLE `timesheet` (
`user_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_name` TEXT,
`task_type` INTEGER,
`hours` INTEGER
);
delete from timesheet;
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 1, 10, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Billy', 2, 6, 1);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 1, 7, 2);
insert into timesheet (user_name, task_type, hours, user_id) values ('Johnny', 2, 9, 2););
select a.user_name, a.hours as billable_hours, b.hours as non_billable_hours
from timesheet a
join timesheet b
on a.user_id = b.user_id
and b.task_type = 2
where a.task_type = 1
answered Sep 3 '15 at 11:28
gnuchugnuchu
101
101
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
add a comment |
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
Though this is true as it stands, the OP's using a stored procedure. It has different mechanics to a table.
– Michael Green
Sep 3 '15 at 11:39
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
As I said, explaining the general principle...
– gnuchu
Sep 3 '15 at 11:41
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f113052%2fjoins-in-stored-procedure%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Please stop asking new questions and go and edit one of the many you have already asked on this topic...
– Mark Sinkinson
Sep 3 '15 at 10:34