关于SQL:从首次出现某些值以来的行数和sql查询首次出现的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于jquery–首次出现选择、MSSQL:选择在另一个表中出现两次以上的行、MySQ
关于SQL:从首次出现某些值以来的行数和sql查询首次出现的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于jquery – 首次出现选择、MSSQL:选择在另一个表中出现两次以上的行、MySQL在每个唯一值的首次出现时选择行、MySQL忽然出现某些记录无法更新等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- SQL:从首次出现某些值以来的行数(sql查询首次出现)
- jquery – 首次出现选择
- MSSQL:选择在另一个表中出现两次以上的行
- MySQL在每个唯一值的首次出现时选择行
- MySQL忽然出现某些记录无法更新
SQL:从首次出现某些值以来的行数(sql查询首次出现)
在SQL Server中,我试图计算自过去5天首次观察到与今天相同的天气(今天假设是2018年8月6日)以来的天数。每个镇。
数据如下:
+---------+---------+--------+--------+--------+| Date | Toronto | Cairo | Zagreb | Ankara |+---------+---------+--------+--------+--------+| 1.08.18 | Rain | Sun | Clouds | Sun || 2.08.18 | Sun | Sun | Clouds | Sun || 3.08.18 | Rain | Sun | Clouds | Rain || 4.08.18 | Clouds | Sun | Clouds | Clouds || 5.08.18 | Rain | Clouds | Rain | Rain || 6.08.18 | Rain | Sun | Sun | Sun |+---------+---------+--------+--------+--------+
这需要执行得很好,但到目前为止,我只想针对每个镇进行单个查询(并且将会有数十个镇,而不仅仅是四个镇)。这行得通,但不会扩展。
这是多伦多的那个…
SELECT DATEDIFF(DAY, MIN([Date]), GETDATE()) + 1 FROM (SELECT TOP 5 * FROM Weather WHERE [Date] <= GETDATE() ORDER BY [Date] DESC) aWHERE Toronto = (SELECT TOP 1 Toronto FROM Weather WHERE DataDate = GETDATE())
…正确返回4,因为今天有雨,而过去5天内第一次下雨是8月3日。
但是我想要返回的是一个像这样的表:
+---------+-------+--------+--------+| Toronto | Cairo | Zagreb | Ankara |+---------+-------+--------+--------+| 4 | 5 | 1 | 5 |+---------+-------+--------+--------+
这怎么可能?
答案1
小编典典您确实不想尝试对数据透视表执行此操作,尽管您声明数据不是以这种方式存储的,但您没有向我们展示如何以列为中心到达城市的透视图。耻辱。
因此,我已经在一个公用表表达式中“取消透视”了该样本,然后使用applyoperator
来对前5天相同天气的先前发生次数进行计数。看来您知道如何旋转,我将其留给您来旋转最终结果。
with cte as ( select date, city, weather FROM ( SELECT * from mytable ) AS cp UNPIVOT ( Weather FOR City IN (Toronto, Cairo, Zagreb, Ankara) ) AS up )select date, city, weather, ca.priorfrom ctecross apply ( select count(*) as prior from cte as prev where prev.city = cte.city and prev.date between dateadd(day,-6,cte.date) and dateadd(day,-1,cte.date) and prev.weather = cte.weather ) ca
使用此样本数据:
CREATE TABLE mytable( Date date NOT NULL ,Toronto VARCHAR(9) NOT NULL ,Cairo VARCHAR(9) NOT NULL ,Zagreb VARCHAR(9) NOT NULL ,Ankara VARCHAR(9) NOT NULL);INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180801'',''Rain'',''Sun'',''Clouds'',''Sun'');INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180802'',''Sun'',''Sun'',''Clouds'',''Sun'');INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180803'',''Rain'',''Sun'',''Clouds'',''Rain'');INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180804'',''Clouds'',''Sun'',''Clouds'',''Clouds'');INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180805'',''Rain'',''Clouds'',''Rain'',''Rain'');INSERT INTO mytable(Date,Toronto,Cairo,Zagreb,Ankara) VALUES (''20180806'',''Rain'',''Sun'',''Sun'',''Sun'');
上面的查询产生了以下结果:
+----+---------------------+---------+---------+-------+| | date | city | weather | prior |+----+---------------------+---------+---------+-------+| 1 | 01.08.2018 00:00:00 | Ankara | Sun | 0 || 2 | 02.08.2018 00:00:00 | Ankara | Sun | 1 || 3 | 03.08.2018 00:00:00 | Ankara | Rain | 0 || 4 | 04.08.2018 00:00:00 | Ankara | Clouds | 0 || 5 | 05.08.2018 00:00:00 | Ankara | Rain | 1 || 6 | 06.08.2018 00:00:00 | Ankara | Sun | 2 || 7 | 01.08.2018 00:00:00 | Cairo | Sun | 0 || 8 | 02.08.2018 00:00:00 | Cairo | Sun | 1 || 9 | 03.08.2018 00:00:00 | Cairo | Sun | 2 || 10 | 04.08.2018 00:00:00 | Cairo | Sun | 3 || 11 | 05.08.2018 00:00:00 | Cairo | Clouds | 0 || 12 | 06.08.2018 00:00:00 | Cairo | Sun | 4 || 13 | 01.08.2018 00:00:00 | Toronto | Rain | 0 || 14 | 02.08.2018 00:00:00 | Toronto | Sun | 0 || 15 | 03.08.2018 00:00:00 | Toronto | Rain | 1 || 16 | 04.08.2018 00:00:00 | Toronto | Clouds | 0 || 17 | 05.08.2018 00:00:00 | Toronto | Rain | 2 || 18 | 06.08.2018 00:00:00 | Toronto | Rain | 3 || 19 | 01.08.2018 00:00:00 | Zagreb | Clouds | 0 || 20 | 02.08.2018 00:00:00 | Zagreb | Clouds | 1 || 21 | 03.08.2018 00:00:00 | Zagreb | Clouds | 2 || 22 | 04.08.2018 00:00:00 | Zagreb | Clouds | 3 || 23 | 05.08.2018 00:00:00 | Zagreb | Rain | 0 || 24 | 06.08.2018 00:00:00 | Zagreb | Sun | 0 |+----+---------------------+---------+---------+-------+
自首次发生以来的天数(过去5天内)
select date, city, weather, datediff(day,ca.prior,cte.date) as priorfrom ctecross apply ( select min(prev.date) as prior from cte as prev where prev.city = cte.city and prev.date between dateadd(day,-6,cte.date) and dateadd(day,-1,cte.date) and prev.weather = cte.weather ) ca
jquery – 首次出现选择
if ($(xml).find('errors')[0].text() == 'No Errors') { do something }
!编辑!!!
found it... if ($(xml).find('error').first().text() == 'No errors')
解决方法
if ($(xml).find('.errors:first').text() == 'No Errors') { // do something }
要么:
if ($(xml).find('.errors').eq(0).text() == 'No Errors') { // do something }
这两个if语句都要求文本(不仅仅包含)等于“No Errors”.
要测试文本是否包含“无错误”文本:
if ($(xml).find('.errors').eq(0).text().toLowerCase().indexOf('no errors') > -1) { // do something }
JS Fiddle demo.
参考文献:
> :first
.
> eq()
.
> toLowerCase()
.
> indexOf()
.
MSSQL:选择在另一个表中出现两次以上的行
基本上,我需要获取在StaffOnGrade中出现2次以上的CampaignTitle的列表,并列出等级高于2的CampaignTitle,StaffNo
WorksOn表:
CampaignTitle | StaffNo
-------------------|--------
ADVENTURE WORLD | 11
AIR CANADA | 11
CARNIVAL CRUISES | 3
CARNIVAL CRUISES | 8
CARNIVAL CRUISES | 9
FLIGHT CENTRE | 7
FLIGHT CENTRE | 10
HARVEYWORLD TRAVEL | 4
LAST MINUTE | 4
PRINCESS CRUISES | 3
PRINCESS CRUISES | 5
PRINCESS CRUISES | 6
PRINCESS CRUISES | 7
PRINCESS CRUISES | 11
TRAVELSCENE | 10
VALUETOURS AUST | 3
VIRGIN AUSTRALIA | 10
StaffOnGrade表:
Grade | StaffNo
------|--------
1 | 2
2 | 11
3 | 3
3 | 6
3 | 7
4 | 4
4 | 8
4 | 10
5 | 5
5 | 9
以下两个查询实现了各个部分,但我需要将其作为一个查询结果集返回。
SELECT campaigntitle,COUNT (CAMPAIGNTITLE) As [count]
FROM WORKSON
GROUP BY CAMPAIGNTITLE
HAVING COUNT(CAMPAIGNTITLE) >2
SELECT STAFFNO,GRADE
FROM STAFFONGRADE
WHERE GRADE > 2
希望这是有道理的!
MySQL在每个唯一值的首次出现时选择行
假设您有下表(这里关注的列是cid
):
+-----+-------+-------+-------+---------------------+--------------+
| cid | pid | rid | clink | time | snippet |
+-----+-------+-------+-------+---------------------+--------------+
| 155 | 11222 | 1499 | 1137 | 2012-08-22 03:05:06 | hi |
| 138 | 11222 | 241 | 1136 | 2012-08-21 05:25:00 | again |
| 138 | 11222 | 241 | 1135 | 2012-08-21 05:16:40 | hi |
| 155 | 11222 | 1499 | 1134 | 2012-08-21 05:11:00 | hi cute |
| 140 | 11222 | 11223 | 1133 | 2012-08-21 05:05:18 | hi |
| 154 | 11222 | 565 | 1132 | 2012-08-21 05:04:47 | 7 |
| 153 | 11222 | 272 | 1131 | 2012-08-21 05:04:41 | 6 |
| 146 | 11222 | 362 | 1130 | 2012-08-21 05:04:33 | 5 |
| 152 | 11222 | 364 | 1129 | 2012-08-21 05:04:27 | 4 |
| 151 | 11222 | 390 | 1128 | 2012-08-21 05:04:22 | 3 |
| 150 | 11222 | 333 | 1127 | 2012-08-21 05:04:16 | 2 |
| 148 | 11222 | 268 | 1126 | 2012-08-21 05:04:10 | 1 |
| 140 | 11222 | 11223 | 1125 | 2012-08-21 04:59:57 | hi sir |
| 147 | 11222 | 283 | 1123 | 2012-08-21 03:29:55 | yo |
| 140 | 11222 | 11223 | 1121 | 2012-08-21 02:12:13 | hello! |
| 139 | 11222 | 249 | 1120 | 2012-08-21 02:11:53 | hi :) |
| 140 | 11222 | 11223 | 1119 | 2012-08-21 02:11:26 | hi :) |
| 140 | 11222 | 11223 | 1118 | 2012-08-21 02:11:08 | hi too |
| 139 | 11222 | 249 | 1117 | 2012-08-21 02:11:00 | :P |
| 139 | 11222 | 249 | 1116 | 2012-08-21 02:10:57 | hi |
| 139 | 11222 | 249 | 1115 | 2012-08-21 02:10:51 | helo |
| 139 | 11222 | 249 | 1114 | 2012-08-21 02:06:19 | hi |
| 139 | 11222 | 249 | 1113 | 2012-08-21 02:05:45 | hi baby |
| 139 | 11222 | 249 | 1112 | 2012-08-21 02:05:00 | hi |
| 139 | 11222 | 249 | 1111 | 2012-08-21 02:04:41 | hi |
| 140 | 11222 | 11223 | 1110 | 2012-08-21 02:04:26 | hi |
| 140 | 11222 | 11223 | 1108 | 2012-08-21 01:47:40 | hey :) |
| 139 | 11222 | 249 | 1107 | 2012-08-21 01:44:43 | hi |
| 138 | 11222 | 241 | 1106 | 2012-08-21 01:44:11 | hi |
| 138 | 11222 | 241 | 1105 | 2012-08-21 01:09:20 | conv 1 msg 1 |
+-----+-------+-------+-------+---------------------+--------------+
如何只提取每个的第一次出现cid
?结果表将是:
+-----+-------+-------+-------+---------------------+--------------+
| cid | pid | rid | clink | time | snippet |
+-----+-------+-------+-------+---------------------+--------------+
| 155 | 11222 | 1499 | 1137 | 2012-08-22 03:05:06 | hi |
| 138 | 11222 | 241 | 1136 | 2012-08-21 05:25:00 | again |
| 140 | 11222 | 11223 | 1133 | 2012-08-21 05:05:18 | hi |
| 154 | 11222 | 565 | 1132 | 2012-08-21 05:04:47 | 7 |
| 153 | 11222 | 272 | 1131 | 2012-08-21 05:04:41 | 6 |
| 146 | 11222 | 362 | 1130 | 2012-08-21 05:04:33 | 5 |
| 152 | 11222 | 364 | 1129 | 2012-08-21 05:04:27 | 4 |
| 151 | 11222 | 390 | 1128 | 2012-08-21 05:04:22 | 3 |
| 150 | 11222 | 333 | 1127 | 2012-08-21 05:04:16 | 2 |
| 148 | 11222 | 268 | 1126 | 2012-08-21 05:04:10 | 1 |
| 147 | 11222 | 283 | 1123 | 2012-08-21 03:29:55 | yo |
| 140 | 11222 | 11223 | 1121 | 2012-08-21 02:12:13 | hello! |
| 139 | 11222 | 249 | 1120 | 2012-08-21 02:11:53 | hi :) |
+-----+-------+-------+-------+---------------------+--------------+
MySQL忽然出现某些记录无法更新
MySQL突然出现某些记录无法更新 ??? 一直运行良好的系统这两天出现奇怪的问题:某些记录无法更新成功。具体错误如下: ?Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; e
MySQL突然出现某些记录无法更新??? 一直运行良好的系统这两天出现奇怪的问题:某些记录无法更新成功。具体错误如下:
?Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
??????? at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
??????? at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
??????? at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
??????? at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
??????? at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
??????? at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
??????? at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
??????? at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
??????? at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
??????? at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
??????? at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
??????? at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
考虑到这两天系统只是前端有少许改动,于是让前端开发人员去定位,前端定位半天后说相关地方没改动,要求后台开发人员查,后台开发人员很抗拒,因为最近后台并没更新,这问题是这两天才出现的,后台不情愿地追踪了半天蒙了,发现同样的代码流程,某些记录能够更新成功,某些则失败。后台直接跟我说搞不定了,太诡异了。无奈之下只好自己去查。
?
??? 网上搜到的关于Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 异常的文章都和我们的情况不符。该异常的描述的意思是:本次更新本来期望能得到更行成功一条的返回信息,却得到成功0条,于是hibernate就报错了。
?
??? 根据反复的测试和debug,发现几个特点:1. 可以更新的记录总是可以更新;2. 更新失败的总是更新失败;3. 新增的记录总是可以更新成功;4. 可以更新的和不可以更新的记录间找不出规律能将两类区分;5. (最重要的一点)不能更新的记录手动也无法更新。
???
??? 上面的第5点让我想起本周虚拟机挂掉的情况,我们的系统都跑在虚拟机上,本周一所有的虚拟机都挂掉了,后来由维护人员恢复了。难道这次宕机让硬盘受到损害导致MySQL部分数据只能读无法写?为了验证这个想法,我把数据库全部导出然后再全部导入,问题解决了!
?
???? 这真是个有趣的问题,什么样的损害会导致MySQL只能读取一条记录但是无法修改它呢?
我们今天的关于SQL:从首次出现某些值以来的行数和sql查询首次出现的分享已经告一段落,感谢您的关注,如果您想了解更多关于jquery – 首次出现选择、MSSQL:选择在另一个表中出现两次以上的行、MySQL在每个唯一值的首次出现时选择行、MySQL忽然出现某些记录无法更新的相关信息,请在本站查询。
本文标签: