计算一行中的每个值外观,并从另一行获取第三个对应值

时间:2018-06-19 10:09:01

标签: sql sql-server filtering

我有数据库与表格如图所示。

N包含重复值,每个值必须计算在数据库中出现的次数。

如果计数为>=10,则应找到P列中相应的第3大值,例如对于100288414,它是10.3432543886119。然后将NWP的相应值复制并粘贴到另一个表中。依此类推,直到数据库结束。

我怎样才能在mssql中执行此操作?

Database

Result

2 个答案:

答案 0 :(得分:1)

尝试此查询:

P

如果N列中的特定值不少于10个条目,则上面会相应地返回第3个最大值import aioamqp import asyncio import pytest MQ_HOST = '0.0.0.0' MQ_PORT = 5672 MQ_LOGIN = 'login' MQ_PASSWORD = 'password' class MockMQ: def __init__(self): self.loop = asyncio.get_event_loop() self.transport = None self.protocol = None async def connect(self): try: self.transport, self.protocol = await aioamqp.connect( host=MQ_HOST, port=MQ_PORT, login=MQ_LOGIN, password=MQ_PASSWORD ) self.channel = await self.protocol.channel() except aioamqp.AmqpClosedConnection: print('closed connection') return async def close(self): await self.protocol.close() self.transport.close() async def publish(self, data, queue_name, exchange='', properties=None): queue = await self.channel.queue_declare(queue_name) await self.channel.publish(data, exchange, queue_name, properties=properties) async def consume(self, callback, queue_name): await self.channel.basic_consume(callback, queue_name=queue_name) @pytest.mark.asyncio async def test_mq(): """Basic ping-pong test for RabbitMQ.""" QUEUE_NAME = 'my_queue' @pytest.mark.asyncio async def callback(channel, body, envelope, properties): """This is the callback called when a MQ message is consumed.""" print('we are here') await channel.basic_client_ack(envelope.delivery_tag) print(body) # this gets printed as well foo = bar * 2 # this is where we fail assert body == b'bar' print('we never arrive here') mq = MockMQ() await mq.connect() await mq.consume(callback, QUEUE_NAME) await mq.publish(b'foo', QUEUE_NAME) await asyncio.sleep(1.0) await mq.close() if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(test_mq()) 列。

答案 1 :(得分:0)

我想出了两个完成任务的查询,首先

select n, count(*) as N FROM mytable GROUP BY n;

返回表中列n的值出现在表中的次数,第二次返回 1-2-3最高值

WITH cte AS ( SELECT n, p, ROW_NUMBER() OVER (PARTITION BY n ORDER BY p ASC ) AS rn FROM mytable ) SELECT n, p, rn FROM cte WHERE rn <= 3 ORDER BY n, rn ;

但是我无法弄清楚如何从w列添加数据并将这些查询合并为一个