Solved

SQL Query Syntax

Posted on 2015-01-16
11
161 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help joining two tables 7 48
Query syntax 10 40
How to simplify my SQL statement? 14 50
Alter an update query which rounds 7 33
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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. …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now