Solved

SQL Query Syntax

Posted on 2015-01-16
11
142 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 33

Expert Comment

by:paulmacd
Comment Utility
ISNULL() is your friend.
0
 

Author Comment

by:triphen
Comment Utility
I changed to this

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


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

Expert Comment

by:Stefan Hoffmann
Comment Utility
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 33

Expert Comment

by:paulmacd
Comment Utility
That could be because any of the other criteria fail.  It's difficult to say.
0
 

Author Comment

by:triphen
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:triphen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

16 Experts available now in Live!

Get 1:1 Help Now