Link to home
Start Free TrialLog in
Avatar of triphen
triphen

asked on

SQL Query Syntax

Hey guys,

Here is my SQL

   "select t.opendate as 'OpenDate', t.Transact as 'Transaction', t.NetTotal as 'HeaderNet', isnull(sum(d.Quan*d.CostEach), 0) as 'DetailNet', t.FinalTotal as 'HeaderFinal',  t.TenderTotal as 'HowPayAppTotal' " _
        & "from dba.posDetail d, " _
        & "(select h.opendate, h.Transact, h.NetTotal, h.FinalTotal, sum(p.Tender) TenderTotal " _
        & "from dba.posHeader h, dba.howpayapproved p " _
        & "where h.Transact = p.Transact and h.opendate between " & lbl_SrchStartDate.Text & " and " & lbl_SrchEndDate.Text & " group by h.opendate, h.Transact, h.NetTotal, h.FinalTotal) t " _
        & "where t.Transact *= d.Transact " _
        & "and d.prodtype not in (101) " _
        & "and opendate between " & lbl_SrchStartDate.Text & " and " & lbl_SrchEndDate.Text & " " _
        & "group by t.opendate, t.Transact, t.NetTotal, t.FinalTotal, t.TenderTotal " _
        & "order by t.transact"

Open in new window


The problem is if there is no record in  t.TenderTotal as 'HowPayAppTotal' then the whole row is skipped and not returned. I want a 0 to be returned if something is missing rather than omitting the whole row.
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

ISNULL() is your friend.
Avatar of triphen
triphen

ASKER

I changed to this

isnull(t.TenderTotal,0) as 'HowPayAppTotal'


But still not working, the whole row is being omitted.
Where's your SQL? I only see a VB/VBA string.

1. Use ANSI JOINs.
2. Don't concatenate SQL strings like this, without input validation. It's prone to SQL injection.
3. Don't concatenate SQL strings like this, date literals are problematic. Use an explict format.
4. When possible use parameterized queries or commands. This solves 2. and 3.
5. t.TenderTotal is a column, it could not be a row in it.
That could be because any of the other criteria fail.  It's difficult to say.
Avatar of triphen

ASKER

It's not other criteria....If I remove the record from howpaidapproved, then the whole row is omitted. If I put it back, then I get it all back.
Avatar of triphen

ASKER

5. t.TenderTotal is a column, it could not be a row in it.


Exactly, If I dont have a value for TenderTotal, then NOTHING is displayed for that Transact row. Almost as if the transact is not there, but it is.


When possible use parameterized queries or commands. This solves 2. and 3.

Can you explain this or provide an example? I am new to this. Thanks!
Assuming you haven't changed the SQL query from your previous question (I haven't read the VB string close enough to verify), this should work:
select t.Transact, t.NetTotal, t.FinalTotal, isnull(sum(d.Quan*d.CostEach),0) DetailTotal, t.TenderTotal
from dba.posDetail d,
    (select h.Transact, h.NetTotal, h.FinalTotal, isnull(sum(p.Tender),0) TenderTotal
     from dba.posHeader h, dba.howpayapproved p
     where h.Transact *= p.Transact group by h.Transact, h.NetTotal, h.FinalTotal) t
where t.Transact *= d.Transact
and d.prodtype not in (101)
group by t.Transact, t.NetTotal, t.FinalTotal, t.TenderTotal

Open in new window


Again, it's a matter of adding isnull and making it an outer join.
In general, it is much easier to provide SQL help if you provide the SQL query (rather than a VB concatenated string).

ste5an has some good points but, if they are necessary, depends on how/where this will be used.
Avatar of triphen

ASKER

I removed the VB, see below.....I added the null like suggested, but still not returning the whole row.

select t.opendate as 'OpenDate', t.Transact as 'Transaction', t.NetTotal as 'HeaderNet', isnull(sum(d.Quan*d.CostEach), 0) as 'DetailNet', t.FinalTotal as 'HeaderFinal',  isnull(t.TenderTotal,0) as 'HowPayAppTotal'
from dba.posDetail d,
(select h.opendate, h.Transact, h.NetTotal, h.FinalTotal, isnull(sum(p.Tender),0) as 'TenderTotal'
from dba.posHeader h, dba.howpayapproved p
where h.Transact = p.Transact group by h.opendate, h.Transact, h.NetTotal, h.FinalTotal) t
where t.Transact *= d.Transact
and d.prodtype not in (101)
group by t.opendate, t.Transact, t.NetTotal, t.FinalTotal, t.TenderTotal
order by t.transact

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of triphen

ASKER

Thanks!