Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Studio SQL Query for SubTotal and Total

Posted on 2009-05-13
16
Medium Priority
?
472 Views
Last Modified: 2013-11-26
I want to create a query that analyses the amount a customer has been invoiced compared to the total invoices for a month. As a bonus, we have a customer that skews the numbers and I would like to also show a total that excludes that customer.
The results would look like this:
Customer|CustomerInvoices|DateMonth|DateYear|TotalInvoices|TotalInvoicesNotCustA

The tables have these fields:

Customer:
CustNo
CustName

Invoices:
InvNo
CustNo
InvDate
InvTotal

The customer to exclude has the CustNo of '0000001046'.
I have tried nested tables and it looks like it could work, but the joins appear convoluted and the whole thing is quite slow.
I don't think my example is a good query, so don't be afraid to toss out a new one.
SELECT     customer.custname AS Customer, SUM(plmain.invtotal) AS CustInvoices, `MONTH`(plmain.invdate) AS DateMonth, `YEAR`(plmain.invdate) AS DateYear, 
 
                      SUM(TotalSales.AllTotalSales) AS TotalSalesAll, SUM(TotalSalesNotInsitsu.NotTotalSales) AS TotalSalesNotInsitsu
 
FROM         plmain, customer,
 
(SELECT     SUM(invtotal) AS AllTotalSales, `MONTH`(invdate) AS AllDateMonth, `YEAR`(invdate) AS AllDateYear
 
FROM          plmain plmain_2
 
GROUP BY AllDateMonth, AllDateYear) TotalSales,
 
(SELECT     SUM(invtotal) AS NotTotalSales, `MONTH`(invdate) AS NotDateMonth, `YEAR`(invdate) AS NotDateYear
 
                            FROM          plmain plmain_1
 
                            WHERE      (custno <> '0000001146')
 
                            GROUP BY NotDateMonth, NotDateYear) TotalSalesNotInsitsu
 
WHERE     plmain.custno = customer.custno AND plmain.invdate < GoMonth(customer.acct_date, 48) AND `MONTH`(plmain.invdate) = TotalSales.AllDateMonth AND
 
                       `YEAR`(plmain.invdate) = TotalSales.AllDateYear AND `MONTH`(plmain.invdate) = TotalSalesNotInsitsu.NotDateMonth AND `YEAR`(plmain.invdate) 
 
                      = TotalSalesNotInsitsu.NotDateYear
 
GROUP BY customer.custname, DateYear, DateMonth, TotalSalesAll, TotalSalesNotInsitsu
 
ORDER BY customer.custname, DateYear, DateMonth

Open in new window

0
Comment
Question by:jason_woods
[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
  • 10
  • 6
16 Comments
 
LVL 43

Expert Comment

by:pcelba
ID: 24375945
First of all some questions:

1) What SQL engine is used to execute above select? Is it FoxPro? Which version?
2) What contains the customer.acct_date?
0
 
LVL 43

Assisted Solution

by:pcelba
pcelba earned 2000 total points
ID: 24376370
It is not necessary to have three selects, two are enough. Hope your SQL engine allows conditional summing:
SUM(CASE WHEN custno <> '0000001146' THEN invtotal ELSE 0 END) AS NotTotalSales,
if not, you may try:
SUM(IIF(custno <> '0000001146', invtotal, 0.00)) AS NotTotalSales,

SELECT customer.custname AS Customer, SUM(plmain.invtotal) AS CustInvoices, `MONTH`(plmain.invdate) AS DateMonth, `YEAR`(plmain.invdate) AS DateYear, 
       SUM(TotalSales.AllTotalSales) AS TotalSalesAll, SUM(TotalSales.NotTotalSales) AS TotalSalesNotInsitsu
FROM         plmain, customer, 
(SELECT SUM(invtotal) AS AllTotalSales, SUM(CASE WHEN custno <> '0000001146' THEN invtotal ELSE 0 END) AS NotTotalSales,
        `MONTH`(invdate) AS AllDateMonth, `YEAR`(invdate) AS AllDateYear 
FROM    plmain plmain_2
GROUP BY AllDateMonth, AllDateYear) TotalSales,
WHERE       plmain.custno = customer.custno AND plmain.invdate < GoMonth(customer.acct_date, 48) 
       AND `MONTH`(plmain.invdate) = TotalSales.AllDateMonth 
       AND `YEAR`(plmain.invdate) = TotalSales.AllDateYear
GROUP BY customer.custname, DateYear, DateMonth
ORDER BY customer.custname, DateYear, DateMonth

Open in new window

0
 
LVL 43

Expert Comment

by:pcelba
ID: 24376513
BTW, the `MONTH`() and `YEAR`() function call isn't allowed neither in FoxPro nor in SQL Server. It was the main reason I am asking for SQL engine version.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:jason_woods
ID: 24376702
I am using visual Studio 2008 and creating an ASP.Net page. The database is VFP9.
I am using VFPOLEDB.1 in my connection string.
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24376788
So in such case you have to use MONTH() and YEAR() without quotes. And IIF() in the SUM() instead of CASE WHEN.
0
 
LVL 4

Author Comment

by:jason_woods
ID: 24376857
Correct, to a point. In Visual Studio 2008 I have to use the `Month` feature so that VS08 doesn't complain. I also am using the IIF() in the previous code you gave.

I am now getting a GROUP BY error. It doesn't occur in the Nested statement when I use it alone.
SELECT customer.custname AS Customer, SUM(plmain.invtotal) AS CustInvoices, `MONTH`(plmain.invdate) AS DateMonth, `YEAR`(plmain.invdate) AS DateYear, 
 
       SUM(TotalSales.AllTotalSales) AS TotalSalesAll, SUM(TotalSales.NotTotalSales) AS TotalSalesNotInsitsu
 
FROM         plmain, customer, 
 
(SELECT     SUM(invtotal) AS AllTotalSales, SUM(IIF(custno <> '0000001146',invtotal ,0.00)) AS NotTotalSales, `MONTH`(invdate) AS AllDateMonth, 
 
                      `YEAR`(invdate) AS AllDateYear
 
FROM         plmain plmain_2
 
GROUP BY AllDateMonth, AllDateYear) TotalSales,
 
WHERE       plmain.custno = customer.custno AND plmain.invdate < GoMonth(customer.acct_date, 48) 
 
       AND `MONTH`(plmain.invdate) = TotalSales.AllDateMonth 
 
       AND `YEAR`(plmain.invdate) = TotalSales.AllDateYear
 
GROUP BY customer.custname, DateYear, DateMonth
 
ORDER BY customer.custname, DateYear, DateMonth

Open in new window

0
 
LVL 43

Accepted Solution

by:
pcelba earned 2000 total points
ID: 24377019
Interesting. I've never used VS to call VFP... The GROUP BY could be solved following way (removing sum from select list and adding two columns to Group by):
SELECT customer.custname AS Customer, SUM(plmain.invtotal) AS CustInvoices, `MONTH`(plmain.invdate) AS DateMonth, `YEAR`(plmain.invdate) AS DateYear, 
 
       TotalSales.AllTotalSales AS TotalSalesAll, TotalSales.NotTotalSales AS TotalSalesNotInsitsu
 
FROM         plmain, customer, 
 
(SELECT     SUM(invtotal) AS AllTotalSales, SUM(IIF(custno <> '0000001146',invtotal ,0.00)) AS NotTotalSales, `MONTH`(invdate) AS AllDateMonth, 
 
                      `YEAR`(invdate) AS AllDateYear
 
FROM         plmain plmain_2
 
GROUP BY AllDateMonth, AllDateYear) TotalSales,
 
WHERE       plmain.custno = customer.custno AND plmain.invdate < GoMonth(customer.acct_date, 48) 
 
       AND `MONTH`(plmain.invdate) = TotalSales.AllDateMonth 
 
       AND `YEAR`(plmain.invdate) = TotalSales.AllDateYear
 
GROUP BY customer.custname, DateYear, DateMonth, TotalSales.AllTotalSales, TotalSales.NotTotalSales
 
ORDER BY customer.custname, DateYear, DateMonth

Open in new window

0
 
LVL 4

Author Comment

by:jason_woods
ID: 24377276
Still a problem. I may have to do the Nested Select on a row-by-row basis in code.
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24377341
What is the error mesaage ?
0
 
LVL 4

Author Comment

by:jason_woods
ID: 24377453
Command Contains Unrecognized Phrase/keyword
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24377461
To be honest, I am not sure if nested selects are allowed at this place and I have no VFP 9 installed to test it right now. It will take some time to download it and test.

To do it row by row in code is possible. Another option could be temporary table and joining this temp table but, again, I am not sure if it is allowed by ADO.
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24377513
So, you should test if it works without nested select. I'll install VFP9 later today.
0
 
LVL 4

Author Comment

by:jason_woods
ID: 24378774
Found it!!! There was an extra comma in the FROM clause at the end!!!
SELECT customer.custname AS Customer, SUM(plmain.invtotal) AS CustInvoices, `MONTH`(plmain.invdate) AS DateMonth, `YEAR`(plmain.invdate) AS DateYear, 
TotalSales.AllTotalSales AS TotalSalesAll, TotalSales.NotTotalSales AS TotalSalesNotInsitsu
FROM         plmain, customer, 
(SELECT     SUM(invtotal) AS AllTotalSales, SUM(IIF(custno <> '0000001146',invtotal ,0.00)) AS NotTotalSales, `MONTH`(invdate) AS AllDateMonth, `YEAR`(invdate) AS AllDateYear
FROM         plmain plmain_2
GROUP BY AllDateMonth, AllDateYear) TotalSales
WHERE       plmain.custno = customer.custno AND plmain.invdate < GoMonth(customer.acct_date, 48) 
       AND `MONTH`(plmain.invdate) = TotalSales.AllDateMonth 
       AND `YEAR`(plmain.invdate) = TotalSales.AllDateYear
GROUP BY customer.custname, DateYear, DateMonth, TotalSales.AllTotalSales, TotalSales.NotTotalSales
ORDER BY customer.custname, DateYear, DateMonth

Open in new window

0
 
LVL 43

Expert Comment

by:pcelba
ID: 24378789
Perfect, now I see it :-)
0
 
LVL 4

Author Closing Comment

by:jason_woods
ID: 31580980
The final answer would be at the bottom of the post.
0
 
LVL 43

Expert Comment

by:pcelba
ID: 24379344
You even answered my question about nested query (or subselect) possibility in VFP9 and you have to be honored, so I've asked new question in Web development - Web languages/standards - ASP zone and waiting for your answer. Thanks for it.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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