jknj72
asked on
SQL Syntax question
I have a sql question that Im hoping I can get answered here.
I created a database that holds timesheet info for users. There are 3 different "Items" that a user can submit(Work item, Travel Item and Receipt items). I have created 2 tables for each of these scenarios (ie, WorkItem and WorkItemDetails). The WorkItem Table would hold an entry and create a WorkItemID and that id would be the foreign key to my Detail table where multiple entries can be made for that ID. That ID is also saved into my master table as a foreign key.
So the Master table would be a 1-1 with the Work Item table and the WorkItem table would be a 1-many with the Detail table.
This is all done for a given week.
What I am trying to do is create a summary(Count) of each of the WorkItems, Travel Items and Receipt Items for a given week so an admin can click on one of the weekly groups to see the details and eventually he has to Approve.
So really what Id like to do is a Select statement where I can get a Count of WorkItems, TravelItems and ReceiptItems. I have the WorkWeek date info in the Master table so I want to show, by week, how many items are in each of these tables....
I initally wrote this but want to combine all 3 items for my front end to show the admin how many items, for a given week, the user has entered. Id like to get back all the info in 1 proc though...
Any help would be greatly appreciated
Thanks
JK
I created a database that holds timesheet info for users. There are 3 different "Items" that a user can submit(Work item, Travel Item and Receipt items). I have created 2 tables for each of these scenarios (ie, WorkItem and WorkItemDetails). The WorkItem Table would hold an entry and create a WorkItemID and that id would be the foreign key to my Detail table where multiple entries can be made for that ID. That ID is also saved into my master table as a foreign key.
So the Master table would be a 1-1 with the Work Item table and the WorkItem table would be a 1-many with the Detail table.
This is all done for a given week.
What I am trying to do is create a summary(Count) of each of the WorkItems, Travel Items and Receipt Items for a given week so an admin can click on one of the weekly groups to see the details and eventually he has to Approve.
So really what Id like to do is a Select statement where I can get a Count of WorkItems, TravelItems and ReceiptItems. I have the WorkWeek date info in the Master table so I want to show, by week, how many items are in each of these tables....
I initally wrote this but want to combine all 3 items for my front end to show the admin how many items, for a given week, the user has entered. Id like to get back all the info in 1 proc though...
Any help would be greatly appreciated
Thanks
JK
If @TypeID = 1
Select
COUNT(wid.WorkItemID) as 'Count',
wi.WorkItemID as 'ItemID',
CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101) as 'WeekRange',WeekNo as 'WeekNumber',
ApprovedBy, ApprovedDate
from WorkItem wi
Inner join [WorkWeek] ww on wi.WorkWeekID = ww.WorkWeekID
INNER JOIN WorkItemDetails wid ON wi.WorkItemID=wid.WorkItemID
Where UserID = 1
Group By wi.WorkItemID, WeekNo, CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101),
ApprovedBy, ApprovedDate
Order By Weekno, ItemID
else if @TypeID = 2
Select
COUNT(tid.TravelItemID) as 'Count',
ti.TravelItemID as 'ItemID',
CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101) as 'WeekRange',WeekNo as 'WeekNumber',
ApprovedBy, ApprovedDate
from TravelItem ti
Inner join [WorkWeek] ww on ti.WorkWeekID = ww.WorkWeekID
Inner JOIN TravelItemDetails tid ON ti.TravelItemID=tid.TravelItemID
Where UserID = 1
Group By ti.TravelItemID, WeekNo, CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101),
ApprovedBy, ApprovedDate
Order By Weekno, ItemID
else if @TypeID = 3
Select
COUNT(iid.ImageItemID) as 'Count',
ii.ImageItemID as 'ItemID',
CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101) as 'WeekRange', WeekNo as 'WeekNumber',
ApprovedBy, ApprovedDate
from ImageItem ii
Inner join [WorkWeek] ww on ii.WorkWeekID = ww.WorkWeekID
Inner JOIN ImageItemDetails iid ON ii.ImageItemID=iid.ImageItemID
Where UserID = 1
Group By ii.ImageItemID, WeekNo, CONVERT(varchar(50), StartDay, 101) + ' - ' + CONVERT(varchar(50), EndDay, 101),
ApprovedBy, ApprovedDate
Order By Weekno, ItemID
ASKER
This is what I am trying to do but its not working the way I need it to....If I run them individually I get the correct results. If I try to run them in one query I am getting wrong results
--This doesnt work
--It returns 4, 4, 4 for each of the counts respectively
But these run individually do...
So I am trying to get the results of the 3 last queries to run in 1 query. Something like the first query I just posted...just not the same!!!
PLEASE HELP!!!
--This doesnt work
--It returns 4, 4, 4 for each of the counts respectively
SELECT
COUNT(wid.WorkItemDetailID) as 'WorkCount',
COUNT(tid.TravelItemDetailID) as 'TravelCount'
COUNT(iid.ImageItemDetailID) as 'ImageCount'
From
WorkWeekTracker wwt
LEFT JOIN WorkItem wi ON wwt.WorkItemID = wi.WorkItemID
INNER JOIN WorkItemDetails wid ON wi.WorkItemID=wid.WorkItemID
LEFT JOIN TravelItem ti ON wwt.TravelItemID = ti.TravelItemID
INNER JOIN TravelItemDetails tid ON ti.TravelItemID = tid.TravelItemID
LEFT JOIN ImageItem ii ON wwt.ImageItemID = ii.ImageItemID
INNER JOIN ImageItemDetails iid ON ii.ImageItemID = iid.ImageItemID
INNER JOIN WorkWeek ww ON wwt.weekid = ww.WorkWeekID
But these run individually do...
--Returns 13
SELECT
COUNT(wid.WorkItemDetailID) as 'WorkCount'
From
WorkWeekTracker wwt
LEFT JOIN WorkItem wi ON wwt.WorkItemID = wi.WorkItemID
INNER JOIN WorkItemDetails wid ON wi.WorkItemID=wid.WorkItemID
INNER JOIN WorkWeek ww ON wwt.weekid = ww.WorkWeekID
--Returns 5
SELECT
COUNT(tid.TravelItemDetailID) as 'TravelCount'
From
WorkWeekTracker wwt
LEFT JOIN TravelItem ti ON wwt.TravelItemID = ti.TravelItemID
INNER JOIN TravelItemDetails tid ON ti.TravelItemID = tid.TravelItemID
INNER JOIN WorkWeek ww ON wwt.weekid = ww.WorkWeekID
--Returns 2
SELECT
COUNT(iid.ImageItemDetailID) as 'ImageCount'
From
WorkWeekTracker wwt
LEFT JOIN ImageItem ii ON wwt.ImageItemID = ii.ImageItemID
INNER JOIN ImageItemDetails iid ON ii.ImageItemID = iid.ImageItemID
INNER JOIN WorkWeek ww ON wwt.weekid = ww.WorkWeekID
I tried to find a reason with the results why this isnt working but cannot see any pattern??So I am trying to get the results of the 3 last queries to run in 1 query. Something like the first query I just posted...just not the same!!!
PLEASE HELP!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that was easy..... Worked like a charm...
Thanks again
JK
Thanks again
JK
ASKER
perfect
ASKER
So example
WorkItem
ID = 12
TravelItem
ID = 21
ImageItem
ID = 15
Master table
ID = 1
WI = 12
TI = 21
II = 15
Hope this helps!!