How to add payment conditions to AR outstanding items in a report on Exact Online? - exact-online

I am using Invantive Control to create an Excel report with some outstanding invoices information from Exact Online.
I have created a model with the block designer and I have the outstanding invoices information I need. Now I also want to know the payment condition of the account which is in debt, but there is no information on the payment condition on the AROutstandingItems table.
This is the query I have so far:
select division_code
, division_name
, number_attr
, outstandingitems_ar_account_code_attr
, outstandingitems_ar_account_name
, description
, invoicedate
, duedate
, currency_code_attr
, invoiceamtfc
, outstandingamtfc
, invoiceamtdc
, outstandingamtdc
from aroutstandingitems
order
by division_code
, outstandingamtdc desc
How can I add the payment condition to my report?

Payment conditions are referenced from the account on the outstanding items. In order to get the (sales) payment conditions on an account, there are several options.
Join the Accounts table and get the payment condition from there (from the field salespaymentcondition_code_attr and salespaymentcondition_description).
The SQL would look like this then:
select ...
, act.salespaymentcondition_code_attr
from aroutstandingitems aom
join exactonlinexml..accounts act
on aom.outstandingitems_ar_account_code_attr = act.code_attr
Use an Excel function to get the payment condition: I_EOL_ACT_SLS_PAY_CODE.
The formula has two parameters: division_code and account_code_attr. The first is optional.
A valid call to the formula would thus be: =I_EOL_ACT_SLS_PAY_CODE(,"22") for the payment condition code for account with code 22 in the current Exact Online company. You can incorporate that in your SQL like this:
select ...
, '=I_EOL_ACT_SLS_PAY_CODE("' + division_code + '", "' + outstandingitems_ar_account_code_attr + '")'
pcn_code
from aroutstandingitems
That would result in your model on sync receive the formula for retrieving the payment condition code. Remember to check the check box 'Formula' to ensure that the SQL outcome is treated as an Excel formula.
The same as above, but then using column expressions:
select ...
, '=I_EOL_ACT_SLS_PAY_CODE("$C{E,.,.,^,.}";"$C{E,.,.,^+3,.}")'
pcn_code
from aroutstandingitems
Remember to check the check boxes 'Formula' and 'Column expression' to ensure that the SQL outcome is treated as an Excel formula with $C column expressions.
The recommended option is to use column expressions, since these work across the widest range of deployment scenarios such as accountancy with hundreds of companies and the formulas are upgrade safe. SQL statements may need to be adapted with new releases of the Exact Online data model of Invantive.

Related

Compare results of two SQL queries?

So I'm using SAP Business One application and using B1 validation configuration to give error messages. The main goal of this is where it compares addresses from the BP master data address and the address from the sales order/delivery order address.
So here's the code for the first query which is for the open sales orders only:
SELECT dbo.ORDR.DocNum, dbo.ORDR.DocStatus, dbo.RDR12.StreetS, dbo.RDR12.BlockS, dbo.RDR12.CityS, dbo.RDR12.ZipCodeS, dbo.RDR12.StateS, dbo.RDR12.CountryS
FROM dbo.ORDR INNER JOIN
dbo.RDR12 ON dbo.ORDR.DocEntry = dbo.RDR12.DocEntry
WHERE (dbo.ORDR.DocStatus = 'o')
Heres the code for the second query from the business partner data. This contains all addresses and data
SELECT dbo.CRD1.Street, dbo.CRD1.Address, dbo.CRD1.Block, dbo.CRD1.ZipCode, dbo.CRD1.City, dbo.CRD1.Country, dbo.CRD1.State
FROM dbo.CRD1 INNER JOIN
dbo.OCRD ON dbo.CRD1.CardCode = dbo.OCRD.CardCode
So now I'm hoping to be able to create an SQL condition where it compares these two. Like for example (pseudo code):
if(street != street.s)
begin
if(zip != zip.s)
begin
if(country != country.s).....
begin
Select 'error' for browse
else
select 'passed' for browse
Overall I'm just trying to compare the 2 queries with ONLY open sales orders/delivery orders.
So I'm trying to get it to trigger the error message.
The problem being, I don't know how to pull the values from each one since there's tons of addresses to compare from and I can't just hard code it in.
For example the inputted data is 91234 for zipcode, and zipcode.s is 92134 which is obviously different and would give the error message.

MySQL - Separating data within 1 column into 3 separate columns for a report

We have a report to track how many edits our sales reps are doing. The current query to pull the number of edits on all 3 pages is below. We didn't care before which page they were making edits on, but now we want to see which pages they are make those edits on.
We are wanting to have 3 different columns: bhns, hns, chns, show up on the report and need to modify this query to show the different columns. So, split the 1 column (customer_edits) into 3 columns base on page.
SELECT
count( `database2`.`sales_edits`.`id` ) AS `customer_edits`,
`database2`.`sales_edits`.`rep` AS `rep`
FROM
`database2`.`sales_edits`
WHERE
((
cast( `database2`.`sales_edits`.`date` AS date ) = curdate())
AND ((
`database2`.`sales_edits`.`page` = 'chs'
)
OR ( `database2`.`sales_edits`.`page` = 'chns' )
OR ( `database2`.`sales_edits`.`page` = 'bhns' )))
GROUP BY
`database2`.`sales_edits`.`rep`
sales_edit table:
It looks like you want conditional aggregation:
select
rep,
sum(page = 'chs') customer_edits_chs,
sum(page = 'chns') customer_edits_chns,
sum(page = 'bhns') customer_edits_bhns
from database2.sales_edits
where date = current_date and page in ('chs', 'chns', 'bhns')
group by rep
Your original code looks more complicated that it needs to:
no need to prefix all columns with the schema and table name - a single table comes into play anyway (and if you had more than one, then you should use table aliases to shorten the code)
the date casting seems unecessary; MySQL happily understands any string in 'yyyy-mm-dd' format as a date
the repeated or conditions can be shortened with in
it is probably unneeded to surround all identifiers with backticks, while they do not contain special characters

Mysql Query for fetching records using single Query from three tables

We have three table
table 1- app ( id , name )
table 2- appPlayer ( id , name )
table 3- appPlayerSession ( id , appId , appPlayerId , version)
my Current query is:
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,count(version) FROM appPlayerSession aps GROUP BY appId,version,appName
we need to count the session users for each game with same version, and also woth the object of all users data using single mysql query.
Current Result using my query, but we also need players for each app..
As you havent given your expected result and on basis of your requirement you can do something this.it may be enhanced as per your requirement.
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,(select P.name from appPlayer P where P.id=aps.appPlayerid) as appPlayerName, count(version) FROM appPlayerSession aps GROUP BY appId,version,appName,appPlayerName
Also check fiddle as per your requirement created as you havent given any data set and its on my assumption.
http://sqlfiddle.com/#!9/30fe4f/1
New Sql as per your new added requirement-
select X.appname,X.version,X.appid,GROUP_CONCAT(distinct X.appPlayerName order by X.appPlayerName) as Users ,
sum(X.vercount)
from (SELECT (select name from app k where k.id= aps.appId)
AS appName,version,appId,
(select P.name from appPlayer P where P.id=aps.appPlayerid)
as appPlayerName, count(version)as vercount
FROM appPlayerSession aps
GROUP BY appId,version,appName,appPlayerName) X
group by X.appname,X.version,X.appid
New fiddle -http://sqlfiddle.com/#!9/13646c/5
You can use JOIN in sql to connect with multiple tables and fetch result
Below is the format :
SELECT t1.col, 
       t3.col 
FROM   table1 
       JOIN table2 
         ON table1.primarykey = table2.foreignkey 
       JOIN table3 
         ON table2.primarykey = table3.foreignkey 
In your case :
SELECT app.col, 
       appPlayer.col,
appPlayerSession.col 
FROM   app 
       JOIN appPlayer 
         ON app.id = appPlayer.appId
       JOIN appPlayerSession 
         ON appPlayer.id = appPlayerSession.appPlayerId
Hope this is helpful.
One suggestion . It is not a standard to use camelCase for table and column names. snake_case is preferred widely.

Reduce MySQL Code down or combine SELECT Statements

I have made a few relations to do with a banking database system.
this is my current code. The table has
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000001;
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000002;
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000003;
the rest of the code is a duplicate of this part with the next type of 'Account' and looping back through sortcode's 1-3 again.
I was wondering if there was a more elegant way of producing this. I need to count the number of student, current and saver accounts for each bank.
Or is there a way to combine lots of selects together to make a neat table?
That's what GROUP BY is for!
SELECT SortCode,COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
GROUP BY SortCode;
UPDATE:
You can also GROUP BY with multiple grouping fields:
SELECT SortCode,AccountType,COUNT(AccountType) AS Student_Total FROM Account
GROUP BY SortCode,AccountType;
You could also apply a PIVOT approach to this query to always return a single row and know the fixed-final columns of the result set. However, applying a group by allows for more flexibility of returned rows, especially if you have a large amount of individual things you are trying to tally up.
select
A.AccountType,
SUM( IF( A.SortCode = 1, 1, 0 )) as SortCode1Cnt,
SUM( IF( A.SortCode = 2, 1, 0 )) as SortCode2Cnt,
SUM( IF( A.SortCode = 3, 1, 0 )) as SortCode3Cnt
from
Account A
where
A.AccountType = 'Student'
AND A.SortCode IN ( 1, 2, 3 )
group by
A.AccountType
Note... it appears your sort code is a numeric as you have no quotes around indicating a character string. So, all the leading zeros are irrelevant. And if you were only doing based on a single Account Type, you don't even need the leading Account Type column and can remove the group by too.

How to count unique occurences of Data in SQL

I am trying to count unique occurrences of a Client ID in the following code.
TRANSFORM Count(Research.Client_ID) AS CountOfClient_ID
SELECT Research.Treatment, Count(DCount("[Client_ID]","[Letter Status]")) AS [Total Letters Sent]
FROM Research INNER JOIN [Letter Status] ON Research.Client_ID = [Letter Status].Client_ID
GROUP BY Research.Treatment
PIVOT [Letter Status].Letter_Status;
The expression I think needs to be modified is:
Total Letters Sent: Count(DCount("[Client_ID]","[Letter Status]"))
The typical form for the DCount function is (expression, domain, *criteria*). I am pretty sure I need to specify, somehow, that the [Client_ID] should be unique in the criteria argument of the DCount function, but I don't know how. Is this possible?
If this query doesn't get what you want, please show us with sample data how it differs from what you want.
SELECT
q.Treatment,
Count(*) AS [Total Letters Sent]
FROM
[SELECT DISTINCT
Treatment,
Client_ID
FROM
Research
]. AS q
GROUP BY
q.Treatment;