Tough Sql Server Join Query - sql-server

I am building an application for my semestre exam which will take place in 20 days. My app should assist teachers to build more easily our faculty timetable.
I am working with a database approach (Sql Server 2008 and Delphi XE2). I have a few tables that describe the student formations structure. So I have Years, Series, Specializations, Groups and SemiGroups. Like in the image Years contains Series, Series contain Specializations, Specializations contains Groups and Groups may contain or not SemiGroups. I have also tables with Courses, Teachers, ClassRooms, Days and HourlyIntervals.
There are a few conditions:
A teacher may take a course with one or more Specializations, OR with one or more Groups, OR with one or more Semigroups.
The second condition is that Courses are of 3 types: TeachingCourse, Seminary, Laboratory (only two from three possible for each CourseName).(stored in the column Scheduler.CourseType char(3) )
The third one: Courses can be kept in all weeks of a semester, or in oddweek numbers,or in weeks number dividable by 2.(stored in the column Scheduler.Week char(3))
So I am storing the correlations in a SchedulerTable.
So if a Group has a course with a certain teacher I will introduce only the corresponding IDs.
I built almost all the data introducing forms and now I am at the Reporting part of the application. I am using Report Services from MSSQL 2008.
I want to list a scheduler that will include all the correlations for a certain Specialization (that one includes Groups and/or Semigroups). I have managed to show all correlations for Groups belonging to that certain Specializations but I can't manage to show alse the Specialization and Semigroups courses.
This is the query that returns me Groups correlations from a certain Specialization.
SELECT Days.DayName, HourlyIntervals.HourlyIntervalName, Scheduler.Week, Scheduler.CourseType, Courses.CourseName, ClassRooms.ClassRoomName, Teachers.TeacherName,Specializations.SpecName, Groups.GroupsName
FROM Scheduler INNER JOIN
Groups ON Scheduler.GroupID = Groups.GroupID INNER JOIN
Days ON Scheduler.DayID = Days.DayID INNER JOIN
HourlyIntervals ON Scheduler.HourlyIntervalID = HourlyIntervals.HourlyIntervalID INNER JOIN
Teachers ON Scheduler.TeacherID = Teachers.TeacherID INNER JOIN
Courses ON Scheduler.CourseID = Courses.CourseID INNER JOIN
ClassRooms ON Scheduler.ClassRoomID = ClassRooms.ClassRoomID INNER JOIN
Specializations ON Groups.IDSpec = Specializations.IDSpec
WHERE (Specializations.ID = #SpecID)
ORDER BY Days.DayID, HourlyIntervals.HourlyIntervalID
But I want it to return corelations for SemiGroups,Groups and Specialization for that certain Specialization. I tried to add a join with this one Scheduler.SemiGroupID=Semigroups.SemigroupID but the query returns 0 results then. I don't know if it can be done what I want but I will be thankful to anyone that points me an idea. Or should I use another structure for my correlations table (SchedulerTable).
This is a sample report (a PDF file) which I would like to get:
Edit Reason: Better explaining of the issue

So why isn't this the solution?
SELECT Days.DayName, HourlyIntervals.HourlyIntervalName, Scheduler.Week, Scheduler.CourseType, Courses.CourseName, ClassRooms.ClassRoomName, Teachers.TeacherName,Specializations.SpecName, SemiGroups.GroupsName
FROM Scheduler
INNER JOIN SemiGroups On Scheduler.SemiGroupId = SemiGroups.SemiGroupId
INNER JOIN Groups ON SemiGroups.GroupID = SemiGroups.Groups
INNER JOIN Days ON Scheduler.DayID = Days.DayID
INNER JOIN HourlyIntervals ON Scheduler.HourlyIntervalID = HourlyIntervals.HourlyIntervalID
INNER JOIN Teachers ON Scheduler.TeacherID = Teachers.TeacherID
INNER JOIN Courses ON Scheduler.CourseID = Courses.CourseID
INNER JOIN ClassRooms ON Scheduler.ClassRoomID = ClassRooms.ClassRoomID
INNER JOIN Specializations ON Groups.IDSpec = Specializations.IDSpec
WHERE (Specializations.ID = #SpecID)
ORDER BY Days.DayID, HourlyIntervals.HourlyIntervalID
Though I'll admit to a furrowed brow about whether GroupID in Schedules was the GroupID of the SemiGroupID in that row. Looks likes that bit isn't normalised.

Related

How to display the total number of products in 2 warehouses?

I have this database:
I have a problem, how to show the total number of products in 2 warehouse, if the products in warehouse 1 don't have in warehouse2 and vice versa
I have tried like this
select warehouse1.good_count+warehouse2.good_count
from warehouse1, warehouse2
join goods on (warehouse1.good_id = goods.id)
and (warehouse2.good_id=goods.id);
but it doesn't work
Either you misunderstood what your teacher wants, or your teacher asked the question in a wrong manner.
There should be only one WAREHOUSE table with additional column (e.g. WAREHOUSE_TYPE_ID whose values would then be "remote" or "nearby" or whichever comes next), possibly making a foreign key constraint to WAREHOUSE_TYPE table.
You'd then simply
select t.warehouse_type_name,
sum(w.good_count) summary
from warehouse w join warehouse_type t on t.warehouse_type_id = w.warehouse_type_id
group by t.warehouse_type_name;
I read comments you wrote. The fact that these are physically different warehouses (one in the city and one in its outskirts) doesn't change the fact that data model is wrong. Data they represent should be contained in the same table, a single table (as I explained earlier).
Because, what will you do when company builds yet another warehouse in some other city? Add another table? WRONG!
Anyway, if you insist, something like this might be what you're looking for in this wrong data model of yours:
select g.name,
sum(a.good_count + b.good_count) total_count
from goods g left join warehouse1 a on a.good_id = g.id
left join warehouse2 b on b.good_id = g.id
group by g.name

Searching in more than one table, SQL

Results I'd like to achieve: One record for each customer.
Number of tables: 3 (Account Master, MeterStatus, Codes.MeterStatus)
Hello, I'm trying to find one record for each customer in the following tables, Account Master, MeterStatus, Codes.MeterStatus. Account Master contains customers who are active (need to find here if its available), Meterstatus table contains customers who have been enrolled in our system, but might not have made it to active status, and codes.meterstatus is simply used to decipher codes that are located in meterstatus table.
I've already tried the following variations, but they have not provided the results I expected.
Select *
FROM [xx].[dbo].[AccountMaster] EAM
full join [xx].[Link].[MeterStatus] MS on ms.UAN = eam.UAN
left join xx.codes.MeterStatus on ms.MeterStatus = MeterStatus.MeterStatus
where eam.UAN in ('055013920') or ms.UtilityAccountNumber in ('055013920')
and ms.MeterStatus not in ('4','5')
FROM [xx].[Link].[MeterStatus] MS
left join xx.dbo.AccountMaster EAM on ms.UAN = eam.UAN
left join xx.codes.MeterStatus on ms.MeterStatus = MeterStatus.MeterStatus
where ms.UANin ('055013920')
and ms.MeterStatus not in ('4','5')

How do I do a SQL Group By with an aggregate?

I have a pair of linked tables, the first with job information and the second with material information (each job has 1-10 materials). I am also joining a couple other tables (with customer info and additional job info). I want a total sum of all material of the same type within the same job. Thus job 78100 has three entries of the same material with quantities of 800, 800, and 900. I want the final table to say "78100, etc. . . 2500."
I get the results I want with the following code but I also need the material name, which is in another table that is joined only to CJF.
SELECT
OJ.JobN, C.CustomerName, OJ.JobDescription, OJ.Quantity,
OJ.JobCloseDate, CJ.FSC,
(SELECT SUM(CJF.MT_NETSHEETS)
FROM CT_JobForm CJF
WHERE CJF.JobN = OJ.JobN)
FROM
OpenJob OJ
JOIN
Customer C ON OJ.CustomerN = C.CustomerN
JOIN
CT_Job2 CJ ON OJ.JobN = CJ.JobN
WHERE
JobDescription LIKE '%FSC%'
AND JobCloseDate BETWEEN '01-01-2016' AND '12-31-2016'
ORDER BY
OJ.JobN ASC
I can get the material name using this code, but doing so causes be to lose the grouping (the new code is inside the double-asterisks).
SELECT
OJ.JobN, C.CustomerName, OJ.JobDescription, OJ.Quantity,
OJ.JobCloseDate, CJ.FSC,
(SELECT SUM(CJF.MT_NETSHEETS)
FROM CT_JobForm CJF
WHERE CJF.JobN = OJ.JobN),
**(SELECT MCC.MCCDescription
FROM MatlCostCntr MCC
WHERE CJF.MCCN = MCC.MCCN)**
FROM
OpenJob OJ
JOIN
Customer C ON OJ.CustomerN = C.CustomerN
JOIN
CT_Job2 CJ ON OJ.JobN = CJ.JobN
**JOIN
CT_JobForm CJF ON CJF.JobN = OJ.JobN**
WHERE
JobDescription LIKE '%FSC%'
AND JobCloseDate BETWEEN '01-01-2016' AND '12-31-2016'
ORDER BY
OJ.JobN ASC
I tried nesting the MCCN Select statement in the Netsheet one but couldn't make that work at all. Is there a way to do this that isn't more work than just manually fixing it in Excel?

How do i merge multiple views in SQL with one observation per record?

I have three views that i need to merge so i can only have one record per beneficiary ID. See the views below:
![Views][1]
Farmers View
(Service_Farmer,Beneficiary_ID,Household_ID,FirstName,LastName,FullName,Sex
,Status)
Lead Farmers View
(Service_Lead_Farmer,Household_ID,Beneficiary_ID,FirstName,LastName,Sex,Status)
SILC Members View
(Service_SILC,Beneficiary_ID,Household_ID,FirstName,LastName,Sex)
How can i write a SQL Server code to merge these views so i can tell if someone is a farmer, lead farmer or silc member,etc?
Assuming you have another table, Beneficiaries perhaps, something like the following:
select b.Beneficiary_ID, f.Service_Farmer, lf.Service_Lead_Farmer, sm.Service_SILC, other_columns_as_needed
from Beneficiaries b
left join Farmers f on b.Beneficiary_ID = f.BeneficiaryID
left join LeadFarmers lf on b.Beneficiary_ID = lf.Beneficiary_ID
left join SILCMembers sm on b.Beneficiary_ID = sm.Beneficiary_ID
where whatever_conditions_are_relevant

How can I convert a view containing a START WITH...CONNECT BY sub-query to SQL Server?

I am trying to convert a view from an Oracle RDBMS to SQL Server. The view looks like:
create or replace view user_part_v
as
select part_region.part_id, users.id as users_id
from part_region, users
where part_region.region_id in(select region_id
from region_relation
start with region_id = users.region_id
connect by parent_region_id = prior region_id)
Having read about recursive CTE's and also about their use in sub-queries, my best guess at translating the above into SQL Server syntax is:
create view user_part_v
as
with region_structure(region_id, parent_region_id) as (
select region_id
, parent_region_id
from region_relation
where parent_region_id = users.region_id
union all
select r.region_id
, r.parent_region_id
from region_relation r
join region_structure rs on rs.parent_region_id = r.region_id
)
select part_region.part_id, users.id as users_id
from part_region, users
where part_region.region_id in(select region_id from region_structure)
Obviously this gives me an error about the reference to users.region_id in the CTE definition.
How can I achieve the same result in SQL Server as I get from the Oracle view?
Background
I am working on the conversion of a system from running on an Oracle 11g RDMS to SQL Server 2008. This system is a relatively large Java EE based system, using JPA (Hibernate) to query from the database.
Many of the queries use the above mentioned view to restrict the results returned to those appropriate for the current user. If I cannot convert the view directly then the conversion will be much harder as I will need to change all of the places where we query the database to achieve the same result.
The tables referenced by this view have a structure similar to:
USERS
ID
REGION_ID
REGION
ID
NAME
REGION_RELATIONSHIP
PARENT_REGION_ID
REGION_ID
PART
ID
PARTNO
DESCRIPTION
PART_REGION
PART_ID
REGION_ID
So, we have regions, arranged into a hierarchy. A user may be assigned to a region. A part may be assigned to many regions. A user may only see the parts assigned to their region. The regions reference various geographic regions:
World
Europe
Germany
France
...
North America
Canada
USA
New York
...
If a part, #123, is assigned to the region USA, and the user is assigned to the region New York, then the user should be able to see that part.
UPDATE: I was able to work around the error by creating a separate view that contained the necessary data, and then have my main view join to this view. This has the system working, but I have not yet done thorough correctness or performance testing yet. I am still open to suggestions for better solutions.
I reformatted your original query to make it easier for me to read.
create or replace view user_part_v
as
select part_region.part_id, users.id as users_id
from part_region, users
where part_region.region_id in(
select region_id
from region_relation
start with region_id = users.region_id
connect by parent_region_id = prior region_id
);
Let's examine what's going on in this query.
select part_region.part_id, users.id as users_id
from part_region, users
This is an old-style join where the tables are cartesian joined and then the results are reduced by the subsequent where clause(s).
where part_region.region_id in(
select region_id
from region_relation
start with region_id = users.region_id
connect by parent_region_id = prior region_id
);
The sub-query that's using the connect by statement is using the region_id from the users table in outer query to define the starting point for the recursion.
Then the in clause checks to see if the region_id for the part_region is found in the results of the recursive query.
This recursion follows the parent-child linkages given in the region_relation table.
So the combination of doing an in clause with a sub-query that references the parent and the old-style join means that you have to consider what the query is meant to accomplish and approach it from that direction (rather than just a tweaked re-arrangement of the old query) to be able to translate it into a single recursive CTE.
This query also will return multiple rows if the part is assigned to multiple regions along the same branch of the region heirarchy. e.g. if the part is assigned to both North America and USA a user assigned to New York will get two rows returned for their users_id with the same part_id number.
Given the Oracle view and the background you gave of what the view is supposed to do, I think what you're looking for is something more like this:
create view user_part_v
as
with user_regions(users_id, region_id, parent_region_id) as (
select u.users_id, u.region_id, rr.parent_region_id
from users u
left join region_relation rr on u.region_id = rr.region_id
union all
select ur.users_id, rr.region_id, rr.parent_region_id
from user_regions ur
inner join region_relation rr on ur.parent_region_id = rr.region_id
)
select pr.part_id, ur.users_id
from part_region pr
inner join user_regions ur on pr.region_id = ur.region_id;
Note that I've added the users_id to the output of the recursive CTE, and then just done a simple inner join of the part_region table and the CTE results.
Let me break down the query for you.
select u.users_id, u.region_id, rr.parent_region_id
from users u
left join region_relation rr on u.region_id = rr.region_id
This is the starting set for our recursion. We're taking the region_relation table and joining it against the users table, to get the starting point for the recursion for every user. That starting point being the region the user is assigned to along with the parent_region_id for that region. A left join is done here and the region_id is pulled from the user table in case the user is assigned to a top-most region (which means there won't be an entry in the region_relation table for that region).
select ur.users_id, rr.region_id, rr.parent_region_id
from user_regions ur
inner join region_relation rr on ur.parent_region_id = rr.region_id
This is the recursive part of the CTE. We take the existing results for each user, then add rows for each user for the parent regions of the existing set. This recursion happens until we run out of parents. (i.e. we hit rows that have no entries for their region_id in the region_relationship table.)
select pr.part_id, ur.users_id
from part_region pr
inner join user_regions ur on pr.region_id = ur.region_id;
This is the part where we grab our final result set. Assuming (as I do from your description) that each region has only one parent (which would mean that there's only one row in region_relationship for each region_id), a simple join will return all the users that should be able to view the part based on the part's region_id. This is because there is exactly one row returned per user for the user's assigned region, and one row per user for each parent region up to the heirarchy root.
NOTE:
Both the original query and this one do have a limitation that I want to make sure you are aware of. If the part is assigned to a region that is lower in the heirarchy than the user (i.e. a region that is a descendent of the user's region like the part being assigned to New York and the user to USA instead of the other way around), the user won't see that part. The part has to be assigned to either the user's assigned region, or one higher in the region heirarchy.
Another thing is that this query still exhibits the case I mentioned above about the original query, where if a part is assigned to multiple regions along the same branch of the heirarchy that multiple rows will be returned for the same combination of users_id and part_id. I did this because I wasn't sure if you wanted that behavior changed or not.
If this is actually an issue and you want to eliminate the duplicates, then you can replace the query below the CTE with this one:
select p.part_id, u.users_id
from part p
cross join users u
where exists (
select 1
from part_region pr
inner join user_regions ur on pr.region_id = ur.region_id;
where pr.part_id = p.part_id
and ur.users_id = u.users_id
);
This does a cartesian join between the part table and the users table and then only returns rows where the combination of the two has at least one row in the results of the subquery, which are the results that we are trying to de-duplicate.

Resources