SQL同时合并行 [英] SQL merging rows with same time

查看:86
本文介绍了SQL同时合并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向我的数据库寻求帮助。我需要一个SQL查询。



http://shrani.si/f/2E/ge/2JXHBdq2/untitled.png



如你所见,时间相等3行,但数据不在一行中。我想要的是从这些中排成一排。



http://shrani.si/f/3W/lZ/4NJJyHxf/untitled.png



你可以帮我生成查询,如果可能的话?或者我是否必须通过数据库循环并比较值并将它们粘贴在一起?



Thnx为您提供帮助!

I am asking for help with my databse. I need a SQL query doing following.

http://shrani.si/f/2E/ge/2JXHBdq2/untitled.png

As you can see, that the time is equal in 3 rows, but the data is not in one row. What i want, is to make 1 row out of these.

http://shrani.si/f/3W/lZ/4NJJyHxf/untitled.png

Could u help me generate the query, if its possible? Or will i have to loop trough the databse and compare values and paste them together?

Thnx for your help!

推荐答案

像这样修改你的选择查询

modify your select query like this
SELECT BREAKS,TIME,   
SUM (st3_T1_KV),SUM (st3_T2_KV)  
FROM orders GROUP BY TIME;  



注意:添加所有列...供参考,我只显示了四列



更多信息,请参阅

SQL SUM()与组by [ ^ ]


这是您的问题的解决方案。



i已经在样本表上工作。

输入表:



Here is the solution for your problem.

i have worked on a sample table.
input table:

id	val1	val2
1	5	NULL
2	NULL	2
1	NULL	2
2	3	NULL







select * from (select distinct s1.id, s1.val1, s2.val2<br />
from sample as s1<br />
inner join sample as s2<br />
on s1.id = s2.id) t where t.val1 IS NOT NULL and t.val2 IS NOT NULL





查询后的表输出。





table output after query.

id      val1    val2
1	5	2
2	3	2


您好,为您解决的查询解决方案是。



Hi, The query modified for you solution is.

select * from <br />
(select distinct s1.date, s1.time, s1.st3_T1_KV, s1.st3_T2_KV, s2.st4_T1_KV, ... etc<br />
from your_table_name as s1 inner join your_table_name as s2 on s1.id = s2.id) t <br />
where t.st3_T1_KV IS NOT NULL and t.st3_T2_KV IS NOT NULL and t.st4_T1_KV, ans ..... etc





希望这会给你的结果。



hopefully this will give your result.


这篇关于SQL同时合并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆