Solved

SQL Query Syntax

Posted on 2015-01-16
11
168 Views
Last Modified: 2015-01-17
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
Comment
Question by:triphen
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 40554122
ISNULL() is your friend.
0
 

Author Comment

by:triphen
ID: 40554134
I changed to this

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


But still not working, the whole row is being omitted.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40554138
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 40554143
That could be because any of the other criteria fail.  It's difficult to say.
0
 

Author Comment

by:triphen
ID: 40554167
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
 

Author Comment

by:triphen
ID: 40554169
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40554222
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40554230
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
 

Author Comment

by:triphen
ID: 40554241
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40554332
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
 

Author Closing Comment

by:triphen
ID: 40555000
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to count the days a record spends in a step 21 56
SQL query and VBA 5 55
Not listening to where 1 19
Merge join vs exist 3 27
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question