日期:2014-05-17  浏览次数:20690 次

求解正确的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