Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Any SQL masters about?

Thread Tools
 
Search this Thread
 
Old 23 January 2008, 04:55 PM
  #1  
Beefy
Scooby Regular
Thread Starter
 
Beefy's Avatar
 
Join Date: Feb 2003
Location: Wagons rolllllll yippee kai ayyy ..errrr ...etc ..etc
Posts: 2,700
Likes: 0
Received 0 Likes on 0 Posts
Default Any SQL masters about?

I am learning how to write SQL queries in SAP Busines One. One of the columns in my query is full of figures and I want the SQL query to total that column up at the bottom and give me the SUM.

Someone advised me to use a UNION statement, but having looked into that I cant see how that is going to give me a column total within the report as it seems to be used for joining two separate queries.

Is it possible to run query 1 to show the column with the values in and query 2 to total the values in the relevant column of query 1? Its a bit beyond my feeble abilities at the moment to work it out.

Can anyone help?
Old 23 January 2008, 08:39 PM
  #2  
AlexJReid
Scooby Regular
 
AlexJReid's Avatar
 
Join Date: Apr 2004
Location: Tynemouth
Posts: 213
Likes: 0
Received 0 Likes on 0 Posts
Default

No idea about SAP.

UNION is used to give you the values from the results of two queries with any duplicates are removed. Your total will be unique so this will probably work.

SELECT col FROM table;
UNION
SELECT SUM(col) FROM table;

Would be easier just to return two resultsets.

SELECT col FROM table;
SELECT SUM(col) FROM table;

You could also just do the sum aggregation outside of the database, i.e. while(rs.next()) { total += rs.getInt("col"); }
Old 23 January 2008, 09:00 PM
  #3  
scoobz72
Scooby Regular
 
scoobz72's Avatar
 
Join Date: Nov 2007
Location: Webbed Feet Land
Posts: 754
Likes: 0
Received 0 Likes on 0 Posts
Default

I use SAP R3. All our SQL can be managed from exporting to your local desktop. Are you using SAP on a server or on a PC?
If you are using SAP on a server, then maybe SAP helpdesk would possibly help further.

I take it you are using the data in Excel to use the UNION function.
Old 23 January 2008, 10:39 PM
  #4  
J4CKO
Scooby Regular
iTrader: (1)
 
J4CKO's Avatar
 
Join Date: Jan 2003
Posts: 19,384
Likes: 0
Received 1 Like on 1 Post
Default

You are really talking about reporting or programming here, in SQL Server you can use the CASE statement, like this example

SQL Server: Calculating Running Totals, Subtotals and Grand Total Without a Cursor


In Oracle I would drop down to PL/SQL and use a cursor that goes round the records adding to some total variables, display the detail lines then output the total line.

You could perhaps create a view, another query or temporary table with the results of the sum and select the detail rows as the report body and then select from the totals view/query/temp table.

Theres also Pivot tables/crosstabs, Subqueries

This will do a running total.
SQL Running Totals

Dont know SAP but you tend to find that each dialect has some add ons for these situations so RTM.
Old 24 January 2008, 10:13 AM
  #5  
GaryK
Scooby Regular
 
GaryK's Avatar
 
Join Date: Sep 1999
Location: Bedfordshire
Posts: 4,037
Likes: 0
Received 0 Likes on 0 Posts
Default

Doesnt SAP B1 use SQL 2005 as its datastore? You can use the union or the two query syntax as specified by Alex, what are you grouping on? Customers, orders?
Old 24 January 2008, 04:11 PM
  #6  
Beefy
Scooby Regular
Thread Starter
 
Beefy's Avatar
 
Join Date: Feb 2003
Location: Wagons rolllllll yippee kai ayyy ..errrr ...etc ..etc
Posts: 2,700
Likes: 0
Received 0 Likes on 0 Posts
Default

Thanks for all the replies and apologies for the delay in responding, we've had some hardware upgrades to perform this morning.

Gary - SAP B1 does indeed use SQL2005 for new adopters of the software but we have yet to make the move so are still using SQL2000 SP4 (we went live years ago with v6.5 of SBO).

I guess it wouldve helped if I posted my SQL query so you could all see where I was up to so it is below (if this looks odd its because it has been created using the SAP B1 Query Generator rather than Access or any other tool):

SELECT T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.CreateDate, SUM(T1.LineTotal+T1.LineVat) AS GBP_Invoice_Total
FROM ORIN T0 INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry
GROUP BY T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.CreateDate
ORDER BY T0.docnum

I had a problem in that anything I used in the SELECT statement had to be included in the GROUP BY statement (not sure if this is a limitation of SAP's query writer or a SQL rule).

Where i have the SUM part of the statement, it is the results that appear in this column of the report that I want to have a further total for at the bottom of the column.

Last edited by Beefy; 24 January 2008 at 04:14 PM.
Old 24 January 2008, 04:27 PM
  #7  
AlexJReid
Scooby Regular
 
AlexJReid's Avatar
 
Join Date: Apr 2004
Location: Tynemouth
Posts: 213
Likes: 0
Received 0 Likes on 0 Posts
Default

I'm not convinced that forcing the total into the initial record set is the best way to go.

Better calculated elsewhere, or as a separate result set.

SUM is an aggregate function, grouped on the (optional) GROUP BY clause.
Old 24 January 2008, 04:44 PM
  #8  
Beefy
Scooby Regular
Thread Starter
 
Beefy's Avatar
 
Join Date: Feb 2003
Location: Wagons rolllllll yippee kai ayyy ..errrr ...etc ..etc
Posts: 2,700
Likes: 0
Received 0 Likes on 0 Posts
Default

The problem I have is that the people running the report are not very good in Excel, and have to run this report regularly for printing. For the optimum time saving they want to run the report within SAP and press print then forget about it (so it includes the totals on the print).
Old 24 January 2008, 06:03 PM
  #9  
AlexJReid
Scooby Regular
 
AlexJReid's Avatar
 
Join Date: Apr 2004
Location: Tynemouth
Posts: 213
Likes: 0
Received 0 Likes on 0 Posts
Default

Something along the lines of (untested):

SELECT 'Line',col INTO #t FROM table;
DECLARE @total MONEY;
SELECT @total = SUM(col) FROM #t;
INSERT INTO #t VALUES('Total',@total);
SELECT * FROM #t;
DROP TABLE #t;

would return:

Line 50.00
Line 50.00
Line 10.00
Total 110.00

Stil not an advisable solution. Maybe some SAP people will be able to advise!
Old 24 January 2008, 07:25 PM
  #10  
Dracoro
Scooby Regular
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

Use the following:
CUBE
ROLLUP
or you could use COMPUTE for the whole total if not aggregating.
Old 24 January 2008, 09:14 PM
  #11  
GaryK
Scooby Regular
 
GaryK's Avatar
 
Join Date: Sep 1999
Location: Bedfordshire
Posts: 4,037
Likes: 0
Received 0 Likes on 0 Posts
Default

You could try;

SELECT T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.CreateDate, SUM(T1.LineTotal+T1.LineVat) AS GBP_Invoice_Total,
T2.grandtotal
FROM ORIN T0 INNER JOIN
RIN1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN (select sum(t1.LineTotal+t1.LineVat) as grandtotal from ORIN) AS T2 on t0.DocEntry = t2.DocEntry
GROUP BY T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.CreateDate
ORDER BY T0.docnum

Originally Posted by Beefy
I had a problem in that anything I used in the SELECT statement had to be included in the GROUP BY statement (not sure if this is a limitation of SAP's query writer or a SQL rule).

Where i have the SUM part of the statement, it is the results that appear in this column of the report that I want to have a further total for at the bottom of the column.
Yes if a column is included in a select then you must include it in the group by, unless you do a sub-select like the example above which can let you mix aggregated and non-aggregated queries into 1 statement.

Gary
Old 24 January 2008, 10:16 PM
  #12  
Beefy
Scooby Regular
Thread Starter
 
Beefy's Avatar
 
Join Date: Feb 2003
Location: Wagons rolllllll yippee kai ayyy ..errrr ...etc ..etc
Posts: 2,700
Likes: 0
Received 0 Likes on 0 Posts
Default

Thanks Gary I will give that a try in the morning.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
28 December 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM
mart360
Non Scooby Related
9
29 September 2015 01:45 PM
Pro-Line Motorsport
Car Parts For Sale
0
27 September 2015 11:23 AM



Quick Reply: Any SQL masters about?



All times are GMT +1. The time now is 02:05 AM.