cancel
Showing results for 
Search instead for 
Did you mean: 

SAB B1 Query

Former Member
0 Kudos

I am new to B1 Query and I am trying to create a report to show a summary of order discounts by customer for a date range. There are discounts at the line level and at the order level. When I run the following query, it adds the order level discount and order level total to the summary for each order line. I can't figure out how to summarize the info from INV1 and pull in the order total info just once per order. Any ideas would be appreciated. Here is my code:

SELECT T1.CardName, sum(T0.QuantityT0.PriceBefDi)[Total Before Discounts], sum((T0.QuantityT0.PriceBefDi)-T0.LineTotal)[Line Discount], Sum(T1.DiscSum)[Order Discount],

Sum(((T0.Quantity*T0.PriceBefDi)-T0.LineTotal)+T1.DiscSum)[Total Discounts],

Sum(T1.TotalExpns)[Freight],Sum(T1.DocTotal)[Invoice Total]

FROM INV1 T0 INNER JOIN OINV T1 ON T0.DocEntry = T1.DocEntry

WHERE T1.DocDate between [%1] and [%2]

Group by T1.CardName

Order By T1.CardName

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

SELECT T1.CardName, sum(T0.QuantityT0.PriceBefDi) 'Total Before Discounts', sum((T0.QuantityT0.PriceBefDi)-T0.LineTotal)'Line Discount', Sum(T1.DiscSum)'Order Discount',

Sum(((T0.Quantity*T0.PriceBefDi)-T0.LineTotal)+T1.DiscSum)'Total Discounts',

Sum(T1.TotalExpns)Freight,Sum(T1.DocTotal)'Invoice Total'

FROM INV1 T0 INNER JOIN OINV T1 ON T0.DocEntry = T1.DocEntry

WHERE T1.DocDate between '[%1]' and '[%2]'

Group by T1.CardName

Order By T1.CardName

Former Member
0 Kudos

This sums up the OINV fields incorrectly. For example if there were 3 INV1 lines, the summary OINV fields are tripled.

former_member204969
Active Contributor
0 Kudos

Try this one:

declare @d1 datetime
declare @d2 datetime
set @d1= /* select t.docnum from oinv t where t.DocDate between*/ '[%0]'
set @d2=/* and */'[%1]'
SELECT T1.CardName,
 sum(T0.Quantity*T0.PriceBefDi) 'Total Before Discounts',
 sum((T0.Quantity*T0.PriceBefDi)-T0.LineTotal)'Line Discount',
(Select Sum(T.DiscSum) From OINV T
 Where T.CardName = T1.CardName and  T.DocDate between  @d1 and @d2) 'Order Discount',
(sum((T0.Quantity*T0.PriceBefDi)-T0.LineTotal)+
 (Select Sum(T.DiscSum) From OINV T
  Where T.CardName = T1.CardName and  T.DocDate between  @d1 and @d2)) 'Total Discounts',
(Select Sum(T.TotalExpns) From OINV T
 Where T.CardName = T1.CardName and  T.DocDate between  @d1 and @d2) 'Freight',
(Select Sum(T.DocTotal) From OINV T
 Where T.CardName = T1.CardName and  T.DocDate between  @d1 and @d2) 'Invoice Total'
FROM INV1 T0 INNER JOIN OINV T1 ON T0.DocEntry = T1.DocEntry
WHERE T1.DocDate between @d1 and @d2
Group by T1.CardName
Order By T1.CardName

Former Member
0 Kudos

That was amazing! Thank you so much for your help. You are clearly an SQL guru!

Former Member
0 Kudos

I am also new to SQL, so I would like to understand the parts of your query.

1) In the set clauses, what do /* and */ indicate?

2) Why refer to table 'oinv' as 't' when OINV is referred to later in the script as T1? Is the first reference creating a temporary table called 'oinv'?

I am trying to do a similar query that lists quantity and $ sales of items. I did this successfully. But when I add a date-range condition (see below) I get the error: conversion failed when converting datetime from character string.

Once I get the date range problem figured out, I have to somehow do the following:

1)when unitMsr = 'kg' convert to LBs

2)when Currency = 'CAN' convert to USD

3)summarize all by T2.U_APPFIELD

If you or anyone else can offer advise, I would appreciate it.

--Sales by Product Group in Detail by Date Range

SELECT T2.U_APPFIELD, T0.DocDate, T0.Quantity, T0.unitMsr, T0.PriceBefDi, T0.Currency

FROM INV1 T0 INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode INNER JOIN [dbo].[@CHT_PRODGROUP] T2 ON T1.U_CHT_PRODGROUP = T2.Code

WHERE T0.DocDate >=[%0] AND <=[%1]

ORDER BY T2.U_APPFIELD

Thanks!

Lorna Bateman

former_member583013
Active Contributor
0 Kudos

Lorna Bateman

Please post your question as a new thread !!

Suda

Answers (2)

Answers (2)

Former Member
0 Kudos

That is a good tip to get the totals! Let's say the OINV file looks like this:

CardNum CardName DocNum DiscSum Doctotal

C0001 Test Cust 22001 15.20 150.20

and INV1 looks like this:

LineNum Item Qty PricebefDi LineTotal

1 Item1 2 25.00 45.00

2 Item2 2 50.00 90.00

I want one summary line that says:

Cust Total before discount Line Discount Order Discount Invoice Total

TestCust 150.00 15.00 15.00 30.00 150.20

Edited by: Phyllis Van Horn on Aug 6, 2008 8:50 PM

Former Member
0 Kudos

Hi ,

There is one feature on SAP B1 .After you run the query and you want to see the total , you can do

Ctrl+dblclick on the column and you can see the sum of the column .

"I can't figure out how to summarize the info from INV1 and pull in the order total info just once per order."

Can you please explain with specific example ,that will be helpful to specify the desired output you are looking for

Thank you

Bishal