0

i have a very large select, and i have to join two results, but i cant find a way to make it work. the code is something like:

    (select isnull(sum(fn.tqq),0) from fn where fn.icecream in (1,2,3,4,5,6,7,8,9) and (fn.fdata BETWEEN #2# AND #3#) and fn.usr1 not like '%'+'CHOC'+'%') as Total1,

(select isnull(sum(fn.tqq),0) from fn where fn.icecream in (1,2,3,4,5,6,7,8,9) and (fn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%') as Total2,

thing is, i have to join, in the end, the results from icream 3 and 7, and the icecream 8 and 9. any thoughts?

2 Answers 2

0

Although tagged as VFP, that sample SQL doesn't sound to be a VFP query, nor an MS SQL server query. If for a second we thought that was a valid SQL then you could get what you initially propose in code like this:

Select t1.Total1, t2.Total2 From ;
    (Select Nvl(Sum(fn.tqq),0)  As Total1 ;
    from fn ;
    where fn.icecream In (1,2,3,4,5,6,7,8,9) And ;
    (fn.fdata Between #2# And #3#) And ;
    fn.usr1 Not Like '%'+'CHOC'+'%') t1, ;
    (Select Nvl(Sum(fn.tqq),0) As Total2 ;
    from fn ;
    where fn.icecream In (1,2,3,4,5,6,7,8,9) And ;
    (fn.fdata Between #4# And #5#) And ;
    fn.usr1 Not Like '%'+'Portes'+'%') t2

Or (a more VFP specific query):

Select ;
    Sum(Iif( InList(icecream,1,2,3,4,5,6,7,8,9) And ;
        Between(fdata, #2# , #3#) And ;
        !('CHOC'$usr1), tqq, 0)) as Total1, ;
    Sum(Iif( InList(icecream,1,2,3,4,5,6,7,8,9) And ;
        Between(fdata, #4# , #5#) And ;
        !('Portes'$usr1), tqq, 0)) as Total2 ;
    from fn

However, reading your last sentence, it is more confusing what you really want. If the above is not what you were after, provide us some sample data and the result you want to reach so we can offer a better one.

4
  • any thougths on this? really could use a hand :) Nov 28, 2016 at 11:20
  • I noticed that you had a new addition when you added this reply. Sorry but the code you showed is not valid SQL in the databases I ever use (VFP, PostgreSQL, MS SQL server, SQLite and a few others). What database is that? That makes it harder to understand what you are trying to do. Maybe instead, you should give some sample data and the expected output (and of course which database you are using). Nov 28, 2016 at 12:20
  • i believe its visual fox pro, somewhere between that and sql... but sql will work. i kind of worked it out, but in a caveman way, if you have any thougth... Dec 16, 2016 at 12:00
  • Oh it has been a long time, I thought you already solved it. Did you try the SQLs I gave? (your code was not a valid VFP SQL). Dec 16, 2016 at 12:18
0

ok, i´m kind of lost right now, so here is the full code:

    select 
X.VENDEDOR AS VENDEDOR,
X.VENDNM AS VENDNM,
X.NO AS NO,
X.nome AS nome,
isnull(sum(x.qtt_1),0) as Qtt_1,
isnull(sum(x.qtt_2),0) as Qtt_2,
#2# as Data1,
#3# as Data2,
#4# as Data3,
#5# as Data4,
(SELECT DESIGN FROM ST WHERE ST.forref ='*br1000*') AS ARTIGO,

(select isnull(sum(pn.qtt),0) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%') as Total1,

(select isnull(sum(pn.qtt),0) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%') as Total2,

((select isnull(sum(pn.qtt),0) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%' )-(select isnull(sum(pn.qtt),0) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%')) as Dif_Total_Vend,

isnull(
round((((select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%')
-
(select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))/
(select sum(pn.qtt) from pn where pn.vendedor in (1,2,3,4,5,6,7,8,9) and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))*100,2),0) as Perc_Dif,

isnull(
round((((select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #4# AND #5#) and pn.usr1 not like '%'+'Portes'+'%')
-
(select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))/
(select sum(pn.qtt) from pn where pn.vendnm like x.vendnm and (pn.fdata BETWEEN #2# AND #3#) and pn.usr1 not like '%'+'Portes'+'%'))*100,2),0) as Perc_Dif_Vend

from
(
select
pn.vendedor as Vendedor,
pn.vendnm as Vendnm,
pn.no as No,
(select nome from cl where cl.no=pn.no) as nome,
pn.qtt as Qtt_1,
0 as Qtt_2
from pn
where (pn.fdata BETWEEN #2# AND #3#) 

union all

select
pn.vendedor as Vendedor,
pn.vendnm as Vendnm,
pn.no as No,
(select nome from cl where cl.no=pn.no) as nome,
0 as Qtt_1,
pn.qtt as Qtt_2
from pn
where (pn.fdata BETWEEN #4# AND #5#) AND (pn.Vendedor '3' == pn.Vendedor '7')
)
x

group by x.no,x.nome, x.vendedor,x.vendnm
order by x.nome,x.vendedor

what i want to happen is, the vendor '3' came to replace number '7', and number '8' came to replace '9'. i´m getting duplicate results, when what i want is the results from number '3' and '7' as one (same thing with number '8' and '9').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.