[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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.
0
triphen
Asked:
triphen
  • 5
  • 3
  • 2
  • +1
1 Solution
 
Paul MacDonaldDirector, Information SystemsCommented:
ISNULL() is your friend.
0
 
triphenAuthor Commented:
I changed to this

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


But still not working, the whole row is being omitted.
0
 
ste5anSenior DeveloperCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Paul MacDonaldDirector, Information SystemsCommented:
That could be because any of the other criteria fail.  It's difficult to say.
0
 
triphenAuthor Commented:
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.
0
 
triphenAuthor Commented:
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!
0
 
wilcoxonCommented:
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.
0
 
wilcoxonCommented:
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.
0
 
triphenAuthor Commented:
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

0
 
wilcoxonCommented:
You missed making the second (pseudo-table query) join an outer join.
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

0
 
triphenAuthor Commented:
Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now