Solved

SQL Query Syntax

Posted on 2016-08-04
12
119 Views
Last Modified: 2016-08-07
Hello Experts,

Running the SQL query below is inflating numbers for net total, transaction count, and labor. Not sure why...

SELECT
storeinfo.description AS [Store Name],
SUM(posheader.nettotal) AS [Net Total],
COUNT(transact) AS [Transaction Count],
[Net Total] / [Transaction Count] AS [Transaction Average],
SUM(totalwage) AS [Labor]
FROM
posheader
INNER JOIN storeinfo on posheader.snum = storeinfo.storenum
INNER JOIN punchpayroll on posheader.snum = punchpayroll.snum
WHERE
posheader.status = 3 AND
posheader.opendate = 20160802 AND
punchpayroll.opendate = 20160802
GROUP BY
[Store Name]
ORDER BY
[Store Name]

Open in new window



Ideas?

I believe it may have to do with the "transact" column but it doesn't exist in storeinfo or punchpayroll tables.

SQL Anywhere v10
0
Comment
Question by:triphen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 41741905
Didn't see anything wrong. Can you post your query result and expected output?
For average, you can use AVG function.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 41742566
I'd also suggest using AVG.  I also don't see anything wrong.  We may need the following in order to help you:
  • The query result and expected output for at least 1 row
  • The underlying data from the 3 tables that contribute to that row
0
 

Author Comment

by:triphen
ID: 41742952
See attached for the 3 tables.

See attached for the current results.

See attached for the correct results, it happens when I remove the Labor join and its columnspunchpayroll tablePOSHeader.PNG
Storeinfo.PNG
Current-Results.PNG
Labor-Removed---Correct.PNG
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 41743030
It looks to me like either punchpayroll contains data you're not expecting or else (more likely) you're missing a second join condition for punchpayroll (when you include punchpayroll, the counts and sums go up much higher than you expect).

Is it possible to provide the data from all 3 tables for storenum/snum = 3 (or a different one if there is one with even less rows of data)?  The schemas don't help that much.

Does it make any difference if you change the Transaction Average calculation to use AVG?  It shouldn't but it's something to try.
0
 

Author Comment

by:triphen
ID: 41743293
I don't think AVG is the issue but not sure how to even syntax that from [Net Total] / [Transaction Count] AS [Transaction Average]

anyway...

I am sure it has to do with missing a second join, the question is what?

"Is it possible to provide the data from all 3 tables for storenum/snum = 3 (or a different one if there is one with even less rows of data)? "

Attached
0
 

Author Comment

by:triphen
ID: 41743294
It wont let me attach: "An error occurred with the server. Please try again later."
0
 

Author Comment

by:triphen
ID: 41743341
Working now, attached :)
posheader.csv
punchpayroll.csv
storeinfo.csv
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 41743584
Can you also provide what the primary key is for each table?  It might also be helpful to have any other indexes.  Unfortunately, I have a really busy weekend so I may not be able to get to this until next week (but I'll try).
0
 

Author Comment

by:triphen
ID: 41743634
Primary Key for POSHeader = transact, snum
Primary Key for StoreInfo = storenum, snum
PunchPayroll = this is actually a view and not a table. Maybe this is problem? The table it references is called punchclock and that has a primary key of uniqueid and snum

Thanks for your time!
0
 

Author Comment

by:triphen
ID: 41745052
It looks like the transact column is what is repeated over and over when I remove the sum and count aggregations. This must be a clue as to what's going on.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41745474
Try aggregating the tables independently so that the numbers are calculated before rows get multiplied by joining the tables.
SELECT
      storeinfo.description   AS [Store Name]
    , ph.nettotal             AS [Net Total]
    , ph.tcount               AS [Transaction Count]
    , ph.nettotal / ph.tcount AS [Transaction Average]
    , pl.[Labor]
FROM storeinfo
      INNER JOIN (
            SELECT
                  posheader
                , SUM(nettotal)   AS nettotal
                , COUNT(transact) AS tcount
            FROM posheader
            WHERE posheader.status = 3
                  AND posheader.opendate = 20160802
            GROUP BY
                  snum
      ) ph ON storeinfo.storenum = ph.snum
      INNER JOIN (
            SELECT
                  snum
                , SUM(totalwage) AS Labor
            FROM punchpayroll
            WHERE opendate = 20160802
            GROUP BY
                  snum
      ) pl ON storeinfo.storenum = pl.snum
GROUP BY
      storeinfo.description
ORDER BY
      storeinfo.description

Open in new window

0
 

Author Closing Comment

by:triphen
ID: 41746385
Thanks!
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

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 …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

636 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