首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求解正确的sql语句解决方案

2013-12-21 
求解正确的sql语句SELECT id,convert(varchar(13),date,120),[type],count(*) FROM (select * from barcod

求解正确的sql语句


SELECT id,convert(varchar(13),date,120),[type],count(*) FROM 
(select * from barcode_2d WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_n WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_sep_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_qr_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_qr WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_sep WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00')a 
GROUP BY convert(varchar(13),date,120) ORDER BY convert(varchar(13),date,120) ASC


结果报错:Column 'a.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

求正确结果及原因,谢谢!
[解决办法]
SELECT?id,convert(varchar(13),date,120),[type],count(*)?FROM?
(select?*?from?barcode_2d?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?*?from?barcode_2d_n?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?*?from?barcode_2d_temp?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?*?from?barcode_2d_sep_temp?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?id,username,packageno,lotno,description,[type],barcode,date?from?barcode_2d_qr_temp?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?id,username,packageno,lotno,description,[type],barcode,date?from?barcode_2d_qr?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00'?
union?all?
select?id,username,packageno,lotno,description,[type],barcode,date?from?barcode_2d_sep?WHERE?date?>=?'2013-07-08?08:00:00'?AND?date?<?'2013-07-09?08:00:00')a?
GROUP?BY?id,convert(varchar(13),date,120)?ORDER?BY?convert(varchar(13),date,120)?ASC
[解决办法]
在group by中加了id列,改成这样试试:


SELECT id,convert(varchar(13),date,120),[type],count(*) FROM 
(select * from barcode_2d WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_n WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select * from barcode_2d_sep_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_qr_temp WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_qr WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00' 
union all 
select id,username,packageno,lotno,description,[type],barcode,date from barcode_2d_sep WHERE date >= '2013-07-08 08:00:00' AND date < '2013-07-09 08:00:00')a 
GROUP BY id,convert(varchar(13),date,120) ORDER BY convert(varchar(13),date,120) ASC



[解决办法]
从你的报错信息来看,应该是select中的id列,没有在group by中出现,也没有使用聚合函数,所以才会报错的哈

热点排行