Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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
MySQL交叉表查询中缺少列_Mysql_Crosstab_Dynamicquery - Fatal编程技术网

MySQL交叉表查询中缺少列

MySQL交叉表查询中缺少列,mysql,crosstab,dynamicquery,Mysql,Crosstab,Dynamicquery,所以我有一个非常复杂的交叉表查询给我的客户,他负责管理各大学的学生资助。 查询是动态的,因此它在存储过程中构建为一个长字符串,然后准备、执行和释放 我已经能够提取出最终的查询,大约2000个字符长 select concat(s.LastName, ', ', s.FirstName, ' ', ifnull(s.MiddleInit, '')) as StudentName, p.SSN, case @fullSSN when 0 then concat('xxxx-xx-', right(

所以我有一个非常复杂的交叉表查询给我的客户,他负责管理各大学的学生资助。 查询是动态的,因此它在存储过程中构建为一个长字符串,然后准备、执行和释放

我已经能够提取出最终的查询,大约2000个字符长

select concat(s.LastName, ', ', s.FirstName, ' ', ifnull(s.MiddleInit, '')) as StudentName, p.SSN, 
case @fullSSN when 0 then concat('xxxx-xx-', right(p.ssn, 4)) else concat(left(p.ssn, 3), '-', 
substr(p.ssn, 4, 2), '-', right(p.ssn, 4)) end as formattedSSN, 
sp.AwardYear, c.CampusName, es.EnrollmentStatus, sp.LoanPeriodFrom, sp.LoanPeriodTo, pg.ProgramName, 
concat(s.Address1, ', ', s.City, ', ', s.State, ' ', s.Zip) as Address, s.Email, s.DOB, 
sp.StartDate, sp.EndDate as LDA, sp.VerifyType,  

sum(case when FedPgmName = 'DL Subsidized' then Amount end) as 'DL Subsidized', 
sum(case when FedPgmName = 'DL Unsubsidized' then Amount end) as 'DL Unsubsidized', 
sum(case when FedPgmName = 'Pell' then Amount end) as 'Pell', 
sum(case when FedPgmName = 'DL PLUS' then Amount end) as 'DL PLUS', 
sum(case when FedPgmName = 'FSEOG' then Amount end) as 'FSEOG', 
sum(case when FedPgmName = 'FWS' then Amount end) as 'FWS' 

from Payments as p inner join Student as s on p.SchoolID = s.SchoolID and p.SSN = s.SSN inner join                     
(select SchoolID, SSN, Max(StudentProfileID) as MaxProfileID from StudentProfile group by SchoolID,     
SSN) as max on p.SchoolID = max.SchoolID and p.ssn = max.ssn inner join StudentProfile as sp on 
max.SchoolID = sp.SchoolID and max.SSN = sp.SSN and max.MaxProfileID = sp.StudentProfileID inner 
join Program as pg on sp.SchoolID = pg.SchoolID and sp.ProgramID = pg.ProgramID inner join     as fp 
on p.FedPgmID = fp.FedPgmID inner join Campuses as c on sp.SchoolID = c.SchoolID and sp.CampusID = 
c.CampusID inner join EnrollmentStatus as es on sp.EnrollmentStatusID = es.EnrollmentStatusID where 
p.SchoolID = 'cbd' and CkDate between '2018-01-01' and '2018-12-31' and sp.ProgramID in(161, 24, 25, 
168, 165, 166, 14, 159, 160, 13, 150, 151, 17, 23, 10, 15, 2, 16, 3, 26, 9, 21, 22, 1, 11, 12, 19, 
20, 4, 18, 8, 5, 6, 7) and p.FedPgmID in(1, 8, 9, 10, 5, 6, 23) and sp.EnrollmentStatusID in(1, 10, 
2, 3, 4, 5, 6, 7, 8, 9) and ifnull(CkNo, 0) > 0 group by StudentName, SSN, formattedSSN, AwardYear, 
CampusName, EnrollmentStatus, LoanPeriodFrom, LoanPeriodTo, ProgramName
知道这里到底发生了什么并不重要,但我的问题涉及上面的交叉表代码,其中有几个不同联邦程序名的情况。如果底层数据包含所有提到的不同程序,那么一切正常。如果数据中缺少上述任何一种情况(例如没有带“FWS”的行),那么该列将不会显示在结果中,并且我的代码有问题,因为它希望返回所有列

因此,我试图找出如何确保返回所有列。如果将以下内容添加到生成的SQL代码的开头,则其工作正常:

select '' as StudentName, '' as SSN, '' as formattedSSN, '' as AwardYear, '' as CampusName, '' as EnrollmentStatus, 
'' as LoanPeriodFrom, 
'' as LoanPeriodTo, '' as ProgramName, '' as Address, '' as Email, '' as DOB, '' as StartDate, '' as LDA, '' as VerifyType, 
select 0 as 'DL Subsidized', 0 as 'DL Unsubsidized', 0 as 'Pell', 0 as 'DL PLUS', 0 as 'FSEOG', 0 as 'FWS'
union
使用上述代码使查询成为联合查询是可行的。所有列都返回,但在顶部有一个“空”行,我必须对其进行编码

请记住,查询非常复杂,必须使用大量concat进行构建,另外,我必须进行一次group_concat,以获得可能的联邦程序的名称。这为我创建了case语句,它被注入到主SQL语句中

那么,除了执行联合查询之外,还有什么方法可以强制所有列都显示吗? 希望我把问题说清楚

谢谢

编辑:
事实证明,添加联合查询根本不起作用。我正在运行的数据已经包括所有联邦计划的记录。如果我使用不包含某些联邦程序的数据集运行该联合查询,我会得到一个关于两个查询不匹配列计数的错误。因此,我仍然无法返回交叉表部分中所有可能的列。

无法解决这个问题,所以我用另一种方法解决了这个问题。由于交叉表查询不会在交叉表中提供任何在基础数据中无效的列,因此我创建了一个包含所有可能列的表,然后从交叉表查询中截断并填充该表。最后,从该表中进行选择。现在将显示所有可能的列