UNION vs. JOIN,b和c未定义? (sqlite3)

时间:2018-10-12 05:27:58

标签: sql sqlite

select city, prov, a, b, c from ( 
select count(city) as a, city, prov from locations left outer join rides on src=lcode 
group by city 
union 
select count(city) as b, city, prov from locations left outer join rides on dst=lcode 
group by city 
union
select count(city) as c, city, prov from locations l left outer join enroute e on l.lcode=e.lcode 
group by city 
) group by city;

所以我得到一个错误,说“没有这样的列:b”(还有列c)。是否可以在不更改大量查询的情况下解决此问题?

2 个答案:

答案 0 :(得分:1)

您的子查询将返回三列if n == Batch_size-1: with h5py.File('M_data1.h5', 'w') as f: print "step 1! Saving Batch" print Batch_data.shape dset = f.create_dataset("dataset_1",(data.shape[0],data.shape[1],iterations),chunks=(Batch_data.shape)) dset[:, :, 0:Batch_size] = Batch_data elif any(i == n for i in (np.linspace(Batch_size*2-1, iterations-1, (iterations-Batch_size)/Batch_size))): with h5py.File('M_data1.h5', 'a') as f: print "step 2! Saving Batch" print Batch_data.shape #print Batch_data.shape #print n dset = f['dataset_1'] dset[:, :, n-Batch_size+1:n+1] = Batch_data f.close() acity。也就是说,对于prov / union查询,列名来自第一个子查询。没有union allb

大概是您想要某种c,而不是JOIN。但是,您的问题并不能解释您要做什么。而且它没有样本数据或预期结果。因此,很难说出您真正想要的是什么。

您真正想要的是我:

UNION ALL

答案 1 :(得分:-1)

未封装在聚合函数中且必须包含在GROUP BY子句中的表达式。对于联合,这些联合中的所有select语句必须具有相同的列名。 下面将完成所需的任务。

select * from ( 
select count(city) as cityCount, prov from locations left outer join rides on src=lcode 
group by prov
union 
select count(city) as cityCount, prov from locations left outer join rides on dst=lcode 
group by prov
union
select count(city) as cityCount,prov from locations l left outer join enroute e on l.lcode=e.lcode 
group by prov
);

Outside子查询不需要额外的groupby。它将已经按位置的省进行了分组。