Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Server中从多个表生成视图_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

如何在SQL Server中从多个表生成视图

如何在SQL Server中从多个表生成视图,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我通过以下查询创建了视图,但当从该视图中选择数据时,它会为相同的学生生成多行,这意味着它必须根据[studentsledger]生成数据表不是来自currentacademicinformation表,因为如果1名学生通过了1节课,而当前他在2节课上阅读,则会生成2行。这不应该是因为studentledger是存储学生付费账单的表 CREATE VIEW [dbo].[StudentLedgerView] AS SELECT dbo.StudentsLedger.

我通过以下查询创建了视图,但当从该视图中选择数据时,它会为相同的学生生成多行,这意味着它必须根据[studentsledger]生成数据表不是来自currentacademicinformation表,因为如果1名学生通过了1节课,而当前他在2节课上阅读,则会生成2行。这不应该是因为studentledger是存储学生付费账单的表

CREATE VIEW [dbo].[StudentLedgerView] 
AS
   SELECT     
       dbo.StudentsLedger.StudentLedgerId, 
       dbo.StudentsLedger.FiscalYearId, 
       dbo.FiscalYears.FiscalYearName, 
       dbo.StudentsLedger.AcademicSessionId, 
       dbo.AcademicSessions.Name,
       dbo.StudentsLedger.StudentId, 
       dbo.StudentPersonalInformations.CodeNumber,
       dbo.StudentPersonalInformations.FirstName + ' ' + dbo.StudentPersonalInformations.MiddleName + ' ' + dbo.StudentPersonalInformations.LastName AS StudentName,
       dbo.StudentsLedger.SystemEntryDate, dbo.StudentsLedger.DateAD, 
       dbo.StudentsLedger.DateBS, dbo.StudentsLedger.ReceiptNumber, 
       dbo.StudentsLedger.ReceiptGeneratedBy, 
       dbo.UserDetails.Name AS ReceiptPreparedBy, 
       dbo.StudentsLedger.ReceivedAmount, dbo.StudentsLedger.ReturnAmount, 
       dbo.StudentsLedger.TotalAmount, 
       dbo.StudentsLedger.TotalDiscount, dbo.StudentsLedger.TotalFine, 
       dbo.StudentsLedger.TotalAdjustedFine, 
       dbo.StudentsLedger.IsCancelled,
       dbo.StudentsLedgerDetails.StudentLedgerDetailId, 
       dbo.StudentsLedgerDetails.FeeSetupId, 
       dbo.StudentsLedgerDetails.Particulars, 
       dbo.StudentsLedgerDetails.Amount, 
       dbo.StudentsLedgerDetails.TaxAmount,                        
       dbo.StudentsLedgerDetails.AdjustedAmount, 
       dbo.StudentsLedgerDetails.MonthName, dbo.LevelsName.LevelName, 
       dbo.Classes.ClassName, 
       dbo.Faculties.FacultyName, dbo.Programs.ProgramFullName, 
       dbo.CurrentAcademicInformations.Section, 
       dbo.CurrentAcademicInformations.RollNumber, 
       dbo.CurrentAcademicInformations.LevelNameId, 
       dbo.CurrentAcademicInformations.ProgramId, 
       dbo.CurrentAcademicInformations.YearSem, 
       dbo.CurrentAcademicInformations.ClassId, 
       dbo.CurrentAcademicInformations.FacultyId
   FROM         
       dbo.StudentsLedger 
   INNER JOIN
       dbo.AcademicSessions ON dbo.StudentsLedger.AcademicSessionId = dbo.AcademicSessions.AcademicSessionId  
   LEFT JOIN
       dbo.FiscalYears ON dbo.StudentsLedger.FiscalYearId = dbo.FiscalYears.FiscalYearId 
   LEFT JOIN 
       dbo.StudentsLedgerDetails ON dbo.StudentsLedger.StudentLedgerId = dbo.StudentsLedgerDetails.StudentLedgerId 
   LEFT JOIN 
       dbo.StudentPersonalInformations ON dbo.StudentsLedger.StudentId = dbo.StudentPersonalInformations.StudentPersonalInformationId 
   LEFT JOIN 
       dbo.UsersInfo ON dbo.StudentsLedger.ReceiptGeneratedBy = dbo.UsersInfo.UserId 
   LEFT OUTER JOIN
       dbo.UserDetails ON dbo.UsersInfo.UserId = dbo.UserDetails.UserId 
   LEFT JOIN 
       dbo.CurrentAcademicInformations ON dbo.StudentPersonalInformations.StudentPersonalInformationId = dbo.CurrentAcademicInformations.StudentId 
   LEFT OUTER JOIN
       dbo.LevelsName ON dbo.CurrentAcademicInformations.LevelNameId = dbo.LevelsName.LevelNameId 
   LEFT OUTER JOIN
       dbo.Classes ON dbo.CurrentAcademicInformations.ClassId = dbo.Classes.ClassId 
  LEFT OUTER JOIN
      dbo.Faculties ON dbo.CurrentAcademicInformations.FacultyId = dbo.Faculties.FacultyId 
  LEFT OUTER JOIN
      dbo.Programs ON dbo.CurrentAcademicInformations.ProgramId = dbo.Programs.ProgramId AND dbo.Faculties.FacultyId = dbo.Programs.FacultyId
GO

您可以在选择后立即使用
DISTINCT
。您的查询将从
CurrentAcademicInformation
返回一些数据。如您所说,如果该表每个学生包含两行或更多行,则需要决定要返回哪一行。我们不能替你决定。