Solved

SQL Query Syntax

Posted on 2016-08-04
12
79 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
12 Comments
 
LVL 40

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
 
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
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.

 

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 48

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

9 Experts available now in Live!

Get 1:1 Help Now