2
I have the following code:
String select1 = @"SELECT CODIGOB, DESCRICAO, QUANTIDADE FROM " + path + @"\IVENDA.DBF WHERE DATA BETWEEN {^" + datainicial + "} AND {^" + datafinal + @"} ";
String select2 = @"SELECT CODIGOB, DESCRICAO, QUANTIDADE FROM " + path + @"\NFCEI.DBF WHERE DATAE BETWEEN {^" + datainicial + "} AND {^ " + datafinal + @"} ";
String instrucao1 = @"SELECT CODIGOB, DESCRICAO, QUANTIDADE FROM (" + select1 + ")VND " + @"UNION All " + select2 + " ";
String sql= @"SELECT CODIGOB, DESCRICAO, QUANTIDADE FROM (" + instrucao1 + ")SQL GROUP BY CODIGOB, DESCRICAO ORDER BY QUANTIDADE DESC";
When doing instruction 1 it does not let me use GROUP BY. Would anyone know why ? How should I use GROUP BY
when I have UNION ALL
?
You’ve noticed you’re wearing
select1
as subquery, howeverselect2
is being used directly? If you useselect2
as subquery also, I believe your problems are solved– Jefferson Quesado
You must first do the
union
with all the data and group afterwards. If necessary, make aselect into
in a temporary table with all the required fields, then do thegroup by
from that table– Ricardo Pontual
Always write the query in an sql browser before trying to put it in a program that speaks to DBMS. Helps catch errors a lot more
– Jefferson Quesado
@Ricardopoint, in this case, Cte is most appropriate for this use https://www.mysql.com/why-mysql/presentations/mysql-80-common-table-expressions/
– Jefferson Quesado
Your problem is not in
GROUP BY
and yes, in theORDER BY
within the sub-consultations. Move it to the final query and test again.– Ismael
It is also an option @Jeffersonquesado, only remains to know if the doubt is about
MySql
:-) Anyway has implementations to other databases, is a good suggestion.– Ricardo Pontual
Building the query in
SQLServer
in the same way as he created it, it is possible, but not when using theorder by
internally. Just as it is not possible inVIEWS
. The ORDER BY clause is invalid in common displays, embedded functions, derived tables, sub-consultations and table expressions, unless TOP, OFFSET or FOR XML is also specified.– Ismael
@Ricardopunctual, you’re right. I thought I read the Mysql tag. It follows generically : https://en.m.wikipedia.org/wiki/Common_table_expression
– Jefferson Quesado
@But I have no problem with
ORDER BY
, even removed from the query but what is wrong with thegroup by
– Fabio C
Now that I saw in your first query that you are using the wrong field on
group by
. The column callsDESCPROD
and in thegroup by
you used description. You’ve already corrected that?– Ismael
@Smael I made change as you said. Give a look the column 'DESCPROD' is correct. What happens now for sure but it does not group the items that have different amount. What if I take the amount of error in sql query.
– Fabio C
@Fabioc Your last appointment should not be:
String sql= @"SELECT DESCRICAO, sum(QUANTIDADE) FROM (" + instrucao1 + ")SQL GROUP BY DESCRICAO ORDER BY QUANTIDADE DESC";
I didn’t understand why you took out theSUM
.– Ismael