I am trying to SUM my UNUSED column (named UNUSED). The challenging part in this context here is, I have a COUNT function. The results are calculated correctly. But how can I add a SUM function in this statement? Appreciate any kind advice.
Following is this SQL statement:
SELECT COUNT(ADMIN_NO)*Courses.PricePerPax AS USED
FROM Student_Prog_Course
INNER JOIN COURSES ON COURSES.CID=Student_Prog_Course.Course_ID
GROUP BY COURSES.PricePerPax
Result:
284.94
356.92
1000
5203.6
Expected Result: 6845.46
COURSES
CREATE TABLE [dbo].[Courses] (
[CID] INT IDENTITY (1, 1) NOT NULL,
[PFID] INT NOT NULL,
[Start] DATE NULL,
[EndDate] DATE NULL,
[Title] VARCHAR (50) NULL,
[PricePerPax] FLOAT (53) NULL,
[Claim] FLOAT (53) NULL,
[NoOfPax] INT NULL,
[Status] VARCHAR (50) NULL,
[Date_Last_Action] DATE NULL,
[Exam] NCHAR (1) NULL,
[PreApproved] NCHAR (1) NULL,
[Comments] VARCHAR (50) NULL,
[Provider] VARCHAR (50) NULL,
[School] NCHAR (10) NULL,
[Remarks] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([CID] ASC),
CONSTRAINT [FK_Courses_ToTable] FOREIGN KEY ([PFID]) REFERENCES [dbo].[Programme_Funding] ([PID])
);
INSERT INTO [dbo].[Courses] ([CID], [PFID], [Start], [EndDate], [Title], [PricePerPax], [Claim], [NoOfPax], [Status], [Date_Last_Action], [Exam], [PreApproved], [Comments], [Provider], [School], [Remarks]) VALUES (200, 4, N'2020-05-03', N'2020-10-03', N'Accounting Analytics', 89.23, 89.23, 29, N'Pending', N'2020-07-08', N'N', N'N', N'', N'NCS', N'SIT ', N'')
INSERT INTO [dbo].[Courses] ([CID], [PFID], [Start], [EndDate], [Title], [PricePerPax], [Claim], [NoOfPax], [Status], [Date_Last_Action], [Exam], [PreApproved], [Comments], [Provider], [School], [Remarks]) VALUES (201, 7, N'2020-05-04', N'2020-10-04', N'Understanding ASP.NET c#', 500, 250, 20, N'Pending', N'2020-07-08', N'Y', N'Y', N'NIL', N'NYP', N'SIT ', NULL)
STUDENT_PROG_COURSE
CREATE TABLE [dbo].[Student_Prog_Course] (
[Admin_No] VARCHAR (7) NOT NULL,
[Course_ID] INT NOT NULL,
[Course_Status] TEXT NOT NULL,
[Claim_ID] INT NULL,
[Personal_Email] VARCHAR (50) NULL
);
INSERT INTO [dbo].[Student_Prog_Course] ([Admin_No], [Course_ID], [Course_Status], [Claim_ID], [Personal_Email]) VALUES (N'189097X', 112, N'Completed ', 1, N'AAA#BBB.COM')
INSERT INTO [dbo].[Student_Prog_Course] ([Admin_No], [Course_ID], [Course_Status], [Claim_ID], [Personal_Email]) VALUES (N'194567C', 112, N'Approved ', NULL, NULL)
INSERT INTO [dbo].[Student_Prog_Course] ([Admin_No], [Course_ID], [Course_Status], [Claim_ID], [Personal_Email]) VALUES (N'190234A', 112, N'Cancelled ', NULL, NULL)
Assuming it is USED that is required to be summed, what about:
SELECT SUM(USED)
FROM
(
SELECT COUNT(ADMIN_NO)*Courses.PricePerPax AS USED
FROM Student_Prog_Course
INNER JOIN COURSES ON COURSES.CID=Student_Prog_Course.Course_ID
GROUP BY COURSES.PricePerPax
) u
I want to implement a limited time period membership in asp.net MVC. For that, I manually introduced a new column ExpiryDate in the AspNetUsers table. When a new user registers itself this field will automatically generate an expiry date with the help of the getdate() method.
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[EmailConfirmed] BIT NOT NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT NOT NULL,
[TwoFactorEnabled] BIT NOT NULL,
[LockoutEndDateUtc] DATETIME NULL,
[LockoutEnabled] BIT NOT NULL,
[AccessFailedCount] INT NOT NULL,
[UserName] NVARCHAR (256) NOT NULL,
[RegisterDate ] DATETIME DEFAULT (getdate()) NULL,
[ExpiryDate] DATETIME DEFAULT (getdate()+(60)) NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Now, I want to check that expiry date value everytime when that user tries to login. Where and what logic should I implement in my code?
My create table code like :
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
)
When I use linq to sql to insert a values this messages come out
The primary key column of type 'VarChar(8)' cannot be generated by the server.
what should i do ?
you have to designate the primary key, setting identity is not enough
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
constraint PK_DeTail_ID primary key (ID)
)
I have created a junction table between two of my tables to create a many to many relationship between them.I am able to save data to them, but can't access that data again. This is written in MVC ASP.NET by the way.
My first table:
CREATE TABLE [dbo].[UserInfo] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[FirstName] NVARCHAR (50) NULL,
[LastName] NVARCHAR (50) NULL,
[Email] NVARCHAR (256) NOT NULL,
[Image] VARBINARY (MAX) NULL,
[Approved] BIT NOT NULL,
[Color] NVARCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
My second table:
CREATE TABLE [dbo].[Events] (
[Id] NVARCHAR (128) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[StartDate] DATETIME NULL,
[EndDate] DATETIME NULL,
[Approved] BIT NOT NULL,
[room_id] INT NULL,
[color] NVARCHAR (10) NOT NULL,
[Owner] INT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Events_Rooms] FOREIGN KEY ([room_id]) REFERENCES [dbo].[Rooms] ([_key])
);
My junction table:
CREATE TABLE [dbo].[UserToEvent] (
[UserId] INT NOT NULL,
[EventId] NVARCHAR (128) NOT NULL,
CONSTRAINT [UserId_EventId_pk] PRIMARY KEY CLUSTERED ([UserId] ASC, [EventId] ASC),
CONSTRAINT [FK_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserInfo] ([Id]),
CONSTRAINT [FK_Event] FOREIGN KEY ([EventId]) REFERENCES [dbo].[Events] ([Id])
);
Code to add new relationship:
Event e = await db.Events.FindAsync(id);
string email = User.Identity.Name;
UserInfo user = db.UserInfoes.Where(x => x.Email == email).First();
user.Events.Add(e);
e.UserInfoes.Add(user);
db.Entry(user).State = EntityState.Modified;
db.Entry(e).State = EntityState.Modified;
await db.SaveChangesAsync();
Code to access relationship:
UserInfo user = await db.UserInfoes.FindAsync(id);
List<Events> events = user.Events;
I would expect events to be filled with all events associated with user, but it never has any events.
I have found a work-around which is sufficient to my needs. I added another attribute to my junction table, which then allowed me to access it within my controllers (Since there weren't only two primary keys). From here I was able to gather the data from it just like any other table. Not sure why the above method does not work however. I have used that method before without a hitch. If anyone has an answer, I would love to hear it.
I need a way to get column definition from query result. Right now I'm using SQL Server 2012.
Here is the example scenario, I have two tables which are Event and Attendant whose definitions are below :
CREATE TABLE [dbo].[Event] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Description] NVARCHAR (50) NULL,
[StartDate] DATETIME NULL,
[EndDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Attendant] (
[Id] INT NOT NULL,
[EventId] INT NOT NULL,
[Name] NVARCHAR (50) NULL,
[Company] NVARCHAR (50) NULL
PRIMARY KEY CLUSTERED ([Id] ASC)
);
And then I have query such as :
SELECT Event.Name as EventName, Attendant.Name as GuestName
FROM Event
INNER JOIN Attendant ON Event.Id = Attendant.EventId
How Can I get the column definition for above example query result? My objective is to generate poco class to represent each record of any query result using c#.
you can use sp_columns sproc to retrieve information about the column definition of a specified table ... like this:
exec sp_columns Attendant
Use sp_columns, it returns column information for the specified objects(tables).
Refer this link for details.
select * from information_schema.columns where table_name = '<tablename>'