我需要显示“计划与实际”报告,该报告显示 MySQL 数据库中保存学校出勤率的两个表之间的差异。
我有两个表,名为 Booking 和 Attendance。
表中存储的数据如下:
预订
Id Student Date IsAbsent
1 John 20160216 1 //NO
2 Bob 20160217 1 //NO
3 Zara 20160218 1 //NO
出勤
Id Student Date IsAbsent
1 John 20160216 0 //YES
2 Bob 20160217 0 //YES
3 Mary 20160217 1 //NO
基本上我想将输出显示为
**Id | Student | Day_1 | Day_2 | Day_3**
==== ========= ====== ======= ======
1 | John |ABSENT | NULL | NULL
2 | Bob |NULL |ABSENT | NULL
3 | Mary |NULL |NEW | NULL
4 | Zara |DELETED|NULL | NULL
缺席 约翰在预订表中被标记为 1(假),但在出勤表中他被标记为 0(是),所以我想显示为“缺席”
新 Mary 仅在 Attendance 表中有一个条目,但在 Booking 表中没有。
DELETED Zara 最初被预订并且在 Booking 表中但不在 Attendance 表中。
我创建了 SQL Fiddle模式和我正在使用的查询,但它总是返回空值。
我的 SQL 查询如下..
SELECT * FROM
((SELECT
a.Student as student,
MAX( case
when a.DropDate='20160216' && a.IsAbsent=0 && s.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160216')=0 && (select count(*) from staging where DropDate='20160216')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160216')>0 && (select count(*) from staging where DropDate='20160216')=0 then 'New Booking'
else ' ' end ) as 'day_1',
MAX( case
when a.DropDate='20160217' && a.IsAbsent=0 && s.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160217')=0 && (select count(*) from staging where DropDate='20160217')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160217')>0 && (select count(*) from staging where DropDate='20160217')=0 then 'New Booking'
else ' ' end ) as 'day_2',
MAX( case
when a.DropDate='20160218' && a.IsAbsent=0 && s.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160218')=0 && (select count(*) from staging where DropDate='20160218')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160218')>0 && (select count(*) from staging where DropDate='20160218')=0 then 'DELETED Booking'
else ' ' end ) as 'day_3'
FROM Attendance a LEFT JOIN Booking s on a.Student=s.Student
WHERE a.DropDate IN ('20160216','20160217','20160218')
AND NOT EXISTS
( SELECT 1
FROM Booking AS p
WHERE p.Student = a.Student
AND p.IsAbsent = a.IsAbsent
AND p.DropDate = a.DropDate
)
)
UNION
(SELECT
t.Student as student,
MAX( case
when t.DropDate='20160216' && a.IsAbsent=0 && t.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160216')=0 && (select count(*) from staging where DropDate='20160216')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160216')>0 && (select count(*) from staging where DropDate='20160216')=0 then 'New Booking'
else ' ' end ) as 'day_1',
MAX( case
when a.DropDate='20160217' && a.IsAbsent=0 && t.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160217')=0 && (select count(*) from staging where DropDate='20160217')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160217')>0 && (select count(*) from staging where DropDate='20160217')=0 then 'New Booking'
else ' ' end ) as 'day_2',
MAX( case
when a.DropDate='20160218' && a.IsAbsent=0 && t.IsAbsent=1 then 'Absent'
when (select count(*) from attendance where DropDate='20160218')=0 && (select count(*) from staging where DropDate='20160218')>0 then 'DELETED Booking'
when (select count(*) from attendance where DropDate='20160218')>0 && (select count(*) from staging where DropDate='20160218')=0 then 'DELETED Booking'
else ' ' end ) as 'day_3'
FROM Booking t LEFT JOIN attendance a on t.Student=a.Student
WHERE t.DropDate IN ('20160216','20160217','20160218')
AND NOT EXISTS
( SELECT 1
FROM Attendance AS u
WHERE u.Student = t.Student
AND u.IsAbsent = t.IsAbsent
AND u.DropDate = t.DropDate
)
)) tbl
ORDER BY student
如有任何帮助,我们将不胜感激。
最佳答案
我不确定您是否可以在 MySQL 查询中输出内联布局,除非您创建一个过程... 我不知道这对你是否合适,因为它的布局略有不同,但它有效:
(select s.Student,
s.ClassRoom,
s.DropDate,
if(s.IsAbsent=1&&a.IsAbsent=0,'ABSENT','PRESENT')
from Staging s inner join Attendance a on a.Student = s.Student and a.ClassRoom = s.ClassRoom and a.DropDate = s.DropDate)
UNION ALL
(Select s.Student,
s.ClassRoom,
s.DropDate,
'DELETED'
from Staging s left join Attendance a on a.Student = s.Student and a.ClassRoom = s.ClassRoom and a.DropDate = s.DropDate
where a.Student is null)
UNION ALL
(Select a.Student,
a.ClassRoom,
a.DropDate,
'NEW'
from Staging s right join Attendance a on a.Student = s.Student and a.ClassRoom = s.ClassRoom and a.DropDate = s.DropDate
where s.Student is null)
order by Student, DropDate;
关于php - MySQL 获取两张表的差异记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151672/
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象