结合两个SQL语句

时间:2013-07-30 08:28:42

标签: sql sqlite select where

我想知道是否可以在SQLite中将两个语句组合成一个语句。

第一个陈述如下:

SELECT MAX(goodput_mbps), packetsize_bytes 
FROM data 
WHERE src = "A" AND dst = "B" 
GROUP BY packetsize_bytes

产生类似的东西:

(1.065251641628522, 32)
(2.023058064838313, 64)
(3.9937457226166333, 128)
(7.13931248161879, 256)
(9.732371794078158, 384)
(12.030082350641777, 512)
(14.941748420411137, 640)
(16.768300606617814, 768)
(19.55032187231529, 1028)
(21.54109687187735, 1280)
(23.025071751003825, 1460)
(15.871752691948677, 1534)
(17.309115863006014, 1788)
(18.40483524961694, 2048)

对于每个条目,我然后使用WHERE子句的参数运行另一个SQL查询,每个记录的第一个和第二个条目,例如我将运行的前一个结果集的第一个条目:

SELECT MAX(median)-MIN(median) / MAX(bitrate_mbps)-MIN(bitrate_mbps), packetsize_bytes 
FROM data 
WHERE src = "A" and dst = "B" and packetsize_bytes = 32 and bitrate_mbps < 1.12 
ORDER BY bitrate_mbps ASC 

产生类似的东西:

(0.035000000000000225, 32)

基本上我想要一个产生如下内容的查询:

(x, 32)
(x, 64)
(x, 128)
(x, 256)
(x, 384)
(x, 512)
(x, 640)
(x, 768)
(x, 1028)
(x, 1280)
(x, 1460)
(x, 1534)
(x, 1788)
(x, 2048)

这在SQLite中是否可行?我知道有WHERE IN子句但它似乎接受一个参数。

4 个答案:

答案 0 :(得分:0)

SELECT MAX(median)-MIN(median) / MAX(bitrate_mbps)-MIN(bitrate_mbps), 
       packetsize_bytes 
FROM 
    (SELECT MAX(goodput_mbps) , packetsize_bytes 
    FROM data 
    WHERE src = "A" AND dst = "B" 
    GROUP BY packetsize_bytes) 

WHERE packetsize_bytes = 32 and bitrate_mbps < 1.12 
ORDER BY bitrate_mbps ASC 

答案 1 :(得分:0)

您没有指定&#34;合并&#34;,但如果您只想获得两个查询的结果,则可以将它们与UNION ALL结合使用:

SELECT MAX(goodput_mbps), packetsize_bytes
FROM data
WHERE src = 'A' AND dst = 'B'
GROUP BY packetsize_bytes

UNION ALL

SELECT MAX(median)-MIN(median) / MAX(bitrate_mbps)-MIN(bitrate_mbps),
       packetsize_bytes
FROM data
WHERE src = 'A' AND dst = 'B'
  AND packetsize_bytes = 32 AND bitrate_mbps < 1.12

ORDER BY bitrate_mbps ASC

答案 2 :(得分:0)

SELECT MAX(d1.median)-MIN(d1.median) / MAX(d1.bitrate_mbps)-MIN(d1.bitrate_mbps), d2.packetsize_bytes 
FROM data d1, (SELECT MAX(goodput_mbps), packetsize_bytes FROM data WHERE src = "A" AND dst = "B" GROUP BY packetsize_bytes) d2  
WHERE d1.src = "A" and d1.dst = "B" and d1.packetsize_bytes = d2.packetsize_bytes and d1.bitrate_mbps < "d2.MAX(goodput_mbps)"  
GROUP BY d2.packetsize_bytes
ORDER BY d2.packetsize_bytes ASC

答案 3 :(得分:-2)

不,你不能,因为SQLite本身不支持分析函数,也不支持pl / sql。