Sql query join and aggragation at the same time - sql

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

Related

What is the mistake in this SQL statement? - SQL

SQL query:
insert into Transaction_Details (Trans_Date, Trans_Type)
values (GetDate(), 'WITHDRWAL')
from Transaction_Details t
inner join Acc_Details a on t.UserID = a.UderId
where a.Card_No = '1234567890123456';
Transaction table columns:
Trans_ID int PRIMARY KEY IDENTITY(1,1),
Trans_Date datetime,
Trans_Type varchar(20),
UserID int foreign key references User_Details(UserID)
Card_Id varchar(16) foreign key references User_Card_Details,
Acc_No int foreign key references Acc_Details(Acc_No)
Acc_Details columns:
Acc_Id int,
Acc_No int PRIMARY KEY,
Card_No varchar(16) foreign key references User_Card_Details(Card_No),
UserId int foreign key references User_Details(UserID),
Balance int,
BankName varchar(15)
I get this error:
Incorrect syntax near the keyword 'from'
To insert based on a select it's this:
insert into Transaction_Details (Trans_Date, Trans_Type)
select GetDate(),
'WITHDRWAL'
from Transaction_Details t
inner join Acc_Details a on t.UserID = a.UderId
where a.Card_No = '1234567890123456';

ALTER TABLE statement conflicted with the FOREIGN KEY constraint

When creating the foreign key I came across this error. Below is my code.
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int
)
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
alter table tblPerson add constraint tblPerson_GenderId_FK
foreign key (GenderId) references tblGender(ID)
You want to identify any "do not align" rows....
I have made the below.
You won't be able to add the FK constraint, if any rows come back from the SELECT query.
I have also removed the hungarian notation for "tbl". I would advise against it.
create table dbo.Person(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int )
create table dbo.Gender (
ID int not null primary key,
Gender varchar(50) not null
)
/* any rows below? your FK creation will fail */
Select *, p.GenderId as 'HoustonWeHaveAProblemValue' from dbo.Person p Where Not Exists (Select 1 from dbo.Gender g where g.ID = p.GenderId)
alter table dbo.Person add constraint Person_GenderId_FK
foreign key (GenderId) references dbo.Gender(ID)
Hi please use the following code to achieve your goal :
1-First create tblGender:
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
2-Then create table tblPerson with the relationship between 2 tables since the beginning:
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int references tblGender(ID)
)
works fine.

Invalid Object name 'Subject_Marks'

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

SQL Can't create Foreign key

I'm sure my question would be kinda silly, but I can't solve it. Here is my issue:
create table Product
(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageName varchar(16)
)
create table Storage
(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int
)
And of course I want to link them
alter table Product
add FOREIGN KEY (StorageName) references Storage(StorageName)
But I can't do it, I'm wrong somewhere :/
You have to reference the PK from the Storage table. StorageName is a poor choice for a FK. What if there are Storages with the same name in different cities?
Replace the StorageName in the Product table with StorageID
create table Product(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageID int) --this has chnaged, it will hold the id/PK of the Storage
create table Storage(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int)
And then:
alter table Product
add FOREIGN KEY (StorageID) references Storage(StorageID)

SQL Server 2012 creating tables and values

For some odd reason I can't create a table and because if that I can't insert any values into those tables. This is a new database and I am having a bit of a brain fart.. any help??? thanks
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY (Agencies)
)
Example of a working code
create table Agencies
(
AgencyID nvarchar(20) primary key,
AgName nvarchar(40),
AgAddress nvarchar(40),
AgPhone int
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(20) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Hope it helps!
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(Your_Agencies_ID_COLUMN)
)
There two ways of creating foreign key constrain:
creating a foreign key constraint within the table definition
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
, CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID])
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
,CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID])
);
creating a foreign key constraint for existing table
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
,CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID] )
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
);
ALTER TABLE [dbo].[DataSourceComments]
ADD CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID]);
Note, that in both cases I am specifying the FK constraint name. It's is a recommended to have some naming convention which to apply to all new FK.
I believe your foreign key is incorrect... maybe try this?
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Supposing "AgencyID" is the ID in your Agencies Table.
Try the below code
CREATE TABLE Agency
(
AgencyID INT PRIMARY KEY IDENTITY,
Name NVARCHAR(20)
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID INT FOREIGN KEY REFERENCES Agency(AgencyID)
)
First you have to create the parent table ie, Agency table. Then create the child (Customers) and refer the parent.