Invalid Object name 'Subject_Marks' - sql

CREATE TABLE Subject_Marks
(
Marks_ID int primary key,
Maths_Score int,
Science_Score int,
Social_Score int,
English_Score int,
SUPW_Score int,
Student_ID int not null
);
INSERT into Subject_Marks VALUES
(1,1001, 50, 99,98,45,57);
NOT ABLE TO JOIN THIS TABLE WITH OTHER TABLE SHOWN BELOW.
CREATE TABLE Student_Data
(
Student_ID int IDENTITY(1001,1) PRIMARY KEY,
Student_Name VARCHAR(20) NOT NULL,
Student_Address VARCHAR(100),
Student_Phone bigint,
Student_Email VARCHAR(30),
college_id int NOT NULL
);

Since Student_ID is the key providing the relationship, you need to use this in a join. So, you need to construct a join like below for SQL Server:
select sd.* , sm.*
from Student_Data sd
inner join Subject_Marks sm on sm.Student_ID = sd.Student_ID

Related

Select the row with the max value from a table where sum is calculated

I'm trying to find out how can I extract the rows with the highest value on Suma column from a table which I did with this script:
SELECT specializare,
Count(codprocedura) AS NrProceduri,
Sum(pret) AS Suma
FROM (SELECT p.pret AS Pret,
fp.specializaredoctor AS Specializare,
fp.codprocedura AS CodProcedura
FROM proceduri p
INNER JOIN fisepacienti fp
ON fp.codpacienti = p.codproc)intermediar
GROUP BY intermediar.specializare
The DB tables can be created with this:
create table Pacienti(
CodP int not null primary key,
Nume varchar(50),
Prenume varchar(50),
Descriere varchar(50),
Varsta int,
DataNasterii varchar(50)
);
create table Proceduri(
CodProc int not null primary key,
Nume varchar(50),
Descriere varchar(50),
Pret int
);
create table FisePacienti(
PRIMARY KEY(CodPacienti, CodProcedura),
CodPacienti int FOREIGN KEY REFERENCES Pacienti(CodP),
CodProcedura int FOREIGN KEY REFERENCES Proceduri(CodProc),
Data varchar(50),
NumeDoctor varchar(50),
PrenumeDoctor varchar(50),
SpecializareDoctor varchar(50),
VechimeDoctor varchar(50)
);
You can use TOP (1) WITH TIES:
SELECT TOP (1) WITH TIES fp.specializare,
Count(fp.codprocedura) AS NrProceduri,
Sum(p.pret) AS Suma
FROM proceduri p JOIN
fisepacienti fp
ON fp.codpacienti = p.codproc
GROUP BY fp.specializare
ORDER BY SumA DESC;
Note that a subquery is not needed for the aggregation.

SQL Join 3 tables, add together duplicate rows

I'm having some trouble figuring this out.
"Choose all people and their orders, it's important that you show their name, id - the products name and the amount. If a person has made 2 orders with the same item, you want to only show 1 order, but add together the amounts from both orders."
This is what I have so far, but I can't wrap my head around how to get the multiple orders only show up as 1 with the amount being added together:
SELECT p.navn, p.id, v.varenavn, o.antal
FROM Vare as v
INNER JOIN Ordre as o
ON v.vareid = o.v_id
INNER JOIN Person as p
ON o.p_id = p.id
These are the tables:
Vare:
create table Vare(
vareid int identity(1,1) primary key,
nummer int,
varenavn nvarchar(100),
højde int,
vægt int,
pris money,
dato nvarchar(20),
datotil nvarchar(20),
)
Ordre:
create table Ordre(
ordreid int identity(1,1) primary key,
dato date,
tid int,
antal int,
totalpris money,
betalingsfrist date,
rabatgruppe int,
v_id int foreign key references Vare(vareid) not null,
p_id int,
)
Person:
create table Person(
id int identity(1,1) primary key,
navn nvarchar(30),
vejnavn nvarchar(50),
postnr varchar(10),
bynavn nvarchar(50),
tlf int,
kategori int,
)
You can try.
sum all antal and group by orders.
SELECT p.navn, p.id, v.varenavn, SUM(o.antal)
FROM Vare as v
INNER JOIN Ordre as o
ON v.vareid = o.v_id
INNER JOIN Person as p
ON o.p_id = p.id
GROUP BY
p.navn, p.id, v.varenavn

Sql command that show the cost greater than

I have 3 tables
CREATE TABLE Hotell
(
hotelNum int primary key,
hotelName varchar(50),
city varchar(50)
)
CREATE TABLE Roomm
(
roomNum int primary key,
hotelNum int ,
type varchar(50),
price int
)
CREATE TABLE Bookingg
(
hotelNum int,
guestNum int primary key,
dateFrom int,
dateTo int,
roomNum int
)
I need to list the guests who stay at hotels costing more than 250 per night, and I also want to include the hotel name and city in my screen
I hope this could help you
Select
A.guestNum,B.hotelName,C.price
From
Bookingg as [A] Inner Join Hotell as [B] On A.hotelNum = B.hotelNum
Inner join Roomm as [C] B.hotelNum = C.hotelNum
Where
C.price > 250

SQL Server self referencing query

Please consider the following:
CREATE DATABASE TEST
USE TEST
CREATE TABLE Student
(
StudentID INT IDENTITY
PRIMARY KEY ,
FirstName NVARCHAR(50) ,
LastName NVARCHAR(50)
)
CREATE TABLE StudentComponent
(
StudentComponentID INT IDENTITY
PRIMARY KEY ,
StudentID INT FOREIGN KEY REFERENCES dbo.Student(StudentID) ,
ComponentName NVARCHAR(50) ,
ComponentRef NVARCHAR(50) ,
ComponentType NCHAR(2)
)
CREATE TABLE Component
(
ComponentID INT IDENTITY
PRIMARY KEY ,
StudentComponentID INT FOREIGN KEY REFERENCES dbo.StudentComponent(StudentComponentID) ,
ComponentName NVARCHAR(50) ,
ComponentRef NVARCHAR(50) ,
ComponentType NCHAR(2)
)
I have 3 tables
Student
StudentComponent
Component
split out into dimensions
CREATE TABLE DimClass
(ClassDwKey INT,
ClassName NVARCHAR(50),
ClassRef NVARCHAR(50))
CREATE TABLE DimCollege
(CollegeDwKey INT,
CollegeName NVARCHAR(50),
CollegeRef NVARCHAR(50))
CREATE TABLE DimSubject
(SubjectDwKey INT,
SubjectName NVARCHAR(50),
SubjectRef NVARCHAR(50))
CREATE TABLE DimStudent
(StudentDwKey INT,
StudentName NVARCHAR(50))
INSERT INTO
CREATE TABLE FactAcademicEvent
(StudentDwKey int, ClassDwKey int, CollegeDwKey int, SubjectDwKey INT)
With relationship in same order between them (Student --> StudentComponent --> Component). I have split the components out into their own dimensions, DimClass, DimCollege, DimSubject (split by componenttype field, = 'CL' for class, 'SU' for subject, 'CO' for college etc. I am trying to load a fact table getting my surrogate keys from my dimensions and produce a row with the following
StudentDwKey (from dimstudent), ClassDwKey, CollegeDwKey, SubjectDwKey. There is a link between the dimensions and the component table ComponentKey, which links to student through studentcomponent table.
StudentComponent and Component are basically the same, except StudentComponent references students so has more rows.
Any ideas?
EDIT :
Complete change based on OP's edits and comments.
SELECT
s.StudentID,
MAX(dim_s.StudentDwKey ) AS StudentDwKey,
MAX(dim_cl.ClassDwKey ) AS ClassDwKey,
MAX(dim_su.SubjectDwKey) AS SubjectDwKey,
MAX(dim_co.CollegeDwKey) AS CollegeDwKey
FROM
Student AS s
LEFT JOIN
StudentComponent AS sc
ON sc.StudentID = s.StudentID
LEFT JOIN
dimStudent AS dim_s
ON dim_s.StudentName = s.StudentName -- or whatever is a reliable join
LEFT JOIN
dimClass AS dim_cl
ON dim_cl.ClassRef = sc.ComponentRef
AND sc.ComponentType = 'CL'
LEFT JOIN
dimSubject AS dim_su
ON dim_su.SubjectRef = sc.ComponentRef
AND su.ComponentType = 'SU'
LEFT JOIN
dimCollege AS dim_co
ON dim_co.CollegeRef = sc.ComponentRef
AND sc.ComponentType = 'CO'
GROUP BY
s.StudentID
Keep the student details in the Student table (which you have done) and the component details in the Component table (which you have not done) and use the StudentComponent table to link them together. Something like this:
CREATE DATABASE TEST
USE TEST
CREATE TABLE Student
(
StudentID INT IDENTITY
PRIMARY KEY ,
FirstName NVARCHAR(50) ,
LastName NVARCHAR(50)
)
CREATE TABLE StudentComponent
(
StudentComponentID INT IDENTITY
PRIMARY KEY ,
StudentID INT FOREIGN KEY REFERENCES dbo.Student(StudentID) ,
ComponentID INT FOREIGN KEY REFERENCES dbo.Component(ComponentID),
)
CREATE TABLE Component
(
ComponentID INT IDENTITY
PRIMARY KEY ,
ComponentName NVARCHAR(50) ,
ComponentRef NVARCHAR(50) ,
ComponentType NCHAR(2)
)

Sql query join and aggragation at the same time

How can I Select the Sum of the ProjectPossibilityRatio column from the ProjectCompletion table given the ProjectID? I couldnt find sum:
SELECT pp.ProjectID,
pp.ProjectAlias,
Sum(pd.projectpossibilityratio)
FROM project pp
INNER JOIN projectcompletion pc
ON pp.projectId = pc.projectID
JOIN projectprocedure pd
ON pd.projectprocedureID = pc.projectprocedureID
GROUP BY pd.projectpossibilityratio
Here are the table definitions:
Create TABLE ProjectType(
ProjectTypeID int identity(1,1),
ProjectTypeName nvarchar(100),
Description nvarchar(200),
primary key(ProjectTypeID)
)
CREATE TABLE Project(
ProjectID int identity(1,1),
ProjectAlias nvarchar(100),
ProjectTypeID int foreign key references ProjectType(ProjectTypeID),
MandatedCompanyID int foreign key references Company(CompanyID),
Iscurrent bit,
BuySide bit,
TeamID int foreign key references WorkTeam(TeamID),
ProjectTurnOver varchar(100),
ProjectStartDate Datetime
primary key(ProjectID))
CREATE TABLE ProjectProcedure(
ProjectProcedureID int identity(1,1),
ProjectProcedureName nvarchar(100),
ProjectProcedureDescription nvarchar(200),
ProjectType int foreign key references ProjectType(ProjectTypeID),
ProjectProcedurePosition int,
ProjectProcedureTime smallint,
ProjectPossibilityRatio int,
Primary Key(ProjectProcedureID))
CREATE TABLE ProjectCompletion(
ProjectID int foreign key references Project(ProjectID),
ProjectProcedureID int foreign key references ProjectProcedure(ProjectProcedureID),
StartDate Datetime,
IsCompletedDate Datetime
Primary Key(ProjectID,ProjectProcedureID)
)
Add all the column names that you are selecting to the group by list.
Try this:
SELECT pp.ProjectID,pp.ProjectAlias,Sum(pd.projectpossibilityratio)
FROM project pp INNER JOIN projectcompletion pc ON pp.projectId=pc.projectID
JOIN projectprocedure pd ON pd.projectprocedureID=pc.projectprocedureID
GROUP BY pp.ProjectID,pp.ProjectAlias
And I agree with LeftyX, you should go back and accept answers