How to insert a blank column in a SQL query? - exact-online

This is the SQL code I currently use via the Invantive Control for Excel, linked with our Exact Online DB.
As you can see in the code, in front of the line of the second select case I would like to enter 4 blank columns. The way it's coded right now doesn't work of course, but it was just a try-out. :)
<pre>select date
, InvoiceNumber
, AccountCode
, AccountName
, YourRef
, GLAccountCode
, GLAccountDescription
, CostUnit
, CostUnitDescription
, ProjectCode
, ProjectDescription
, Description
, AmountDC
, AmountFC
, CostCenter
, FinancialPeriod
, JournalDescription
, substr(GLAccountCode, 1, 1) type
, case
when substr(GLAccountCode, 1, 1) = '6'
then 'KOST'
else 'OPBRENGST'
end
pl
<<HERE>>
, case
when substr(GLAccountCode, 1, 1) = '6'
then AmountDC
else 0
end
debet
, case
when substr(GLAccountCode, 1, 1) = '6'
then 0
else AmountDC
end
credit
, AmountDC dc2
from TransactionLines
where FinancialYear = 2017 and JournalCode >='600'
order by date<code>
Second part of my question: can I add excel formulas into these blank columns, via the model editor?
This could be useful so everytime when synchronizing with Exact Online, these formulas are not erased, but refreshed together with the data.

When running on Invantive Control, you need to change both the SQL as well as make sure that your layout allows for additional columns.
First of all insert:
, null COLUMNNAME1
, null COLUMNNAME2
, null COLUMNNAME3
, null COLUMNNAME4
where you need it in the column list.
Then choose 'Refresh' with Fields tab in the model editor.
On an application of 'Synchronize' button, you will see that the columns where necessary move to the right. But...
when you have an Excel range defined in Presentation tab in the model editor for the block, you will need to resize the Excel range to accomodate the 4 new columns. For instance by changing your layout range to include the 4 new columns.

Related

Fetch how many failure and success did each person (name) have

I have a table buggy, the dummy dataset link can be see here
https://github.com/FirzaCank/Project/blob/main/SQL/IFG%20test/Dataset%20Dummy%20no%205.sql
Which contains:
id (INT)
name (VARCHAR)
bug (INT, contains the numbers 0 and 1)
With dataset explanations on 'bug' column are:
0, it means fault / failure
1, it means success
If there is no 'fault', then the 'fault' value will be filled with '0' (null is okay too), so is 'success'
I've tried a MySQL query like this:
SELECT name,
CASE
WHEN bug = 0 THEN COUNT(bug)
END AS failure,
CASE
WHEN bug = 1 THEN COUNT(bug)
END AS success
FROM buggy
GROUP BY name;
The desire output is like This, but as far as I've tried in the above syntax it just came out like this
Thank you for the help!
You should use SUM instead of Count.
SELECT
name,
SUM(IF(bug = 0, 1, 0)) as fault,
SUM(IF(bug = 1, 1, 0)) as success
FROM buggy
GROUP BY name
This counts the number of rows satisfying the conditions inside the IF function.
this sql will give wanted result
SELECT t.name , SUM(t.failure) as failure , SUM(t.success) as success
from ( SELECT name , CASE
WHEN bug < 1 THEN COUNT(bug) ELSE 0
END AS failure,
CASE
WHEN bug = 1 THEN COUNT(bug) ELSE 0
END AS success
FROM buggy
GROUP BY name,bug ) t
GROUP BY t.name;

Access get value from previous record

I have an Access query with the following
GL_A.Account,
GL_P.FiscalYear,
GL_P.FiscalPeriod,
GL_P.BeginningBalance,
GL_P.DebitAmount,
GL_P.CreditAmount,
[BeginningBalance]+([DebitAmount]-[CreditAmount]) AS EndingBalance
The problem is that BeginningBalance only has values for January (FiscalPeriod 1).
I need to have a new field ActualBeginngBal which comes from the previous month EndingBalance (Except January)
Note: there are many account #'s but each account only has 1 record per FiscalPeriod/FiscalYear
Your help would be greatly appreciated,
Thank you
See if this helps. Put a 2nd copy of the table in the query, joined to the 1st copy on Account and FiscalYear but not on FiscalPeriod. Then the ActualBeginningBalance can be calculated from the 2nd copy of the table with a constraint to select only FiscalPeriod < the FiscalPeriod from the 1st table. Note - you may get a null results for January, which you may need to convert to a 0.
OK, it's a bit more complicated - I did end up using a subquery similar to the other response, but calculated the EB instead of trying to pull it from the table
SELECT Ledger.Account, Ledger.FiscalYear, Ledger.FiscalPeriod,
[BeginningBalance]+
IIf([FiscalPeriod]<>"01",
(select sum(T.BeginningBalance+T.DebitAmount-T.CreditAmount)
from Ledger T
where T.account=Ledger.account and T.FiscalYear=Ledger.FiscalYear and T.FiscalPeriod<Ledger.FiscalPeriod)
,0)
AS ActualBeginningBalance,
Ledger.DebitAmount AS DebitAmount,
Ledger.CreditAmount AS CreditAmount,
[ActualBeginningBalance]+[DebitAmount]-[CreditAmount] AS EndingBalance
FROM Ledger;
This SQL does the job and returns the table below (with my test data):
SELECT T1.Account
, T1.FiscalYear
, T1.FiscalPeriod
, T1.ActualBeginngBal
, (
SELECT TOP 1 T2.EndingBalance
FROM Table1 T2
WHERE CLNG(T2.FiscalYear & Format(T2.FiscalPeriod,"00")) <
CLNG(T1.FiscalYear & Format(T1.FiscalPeriod,"00")) AND
T2.Account = T1.Account
ORDER BY CLNG(T2.FiscalYear & Format(T2.FiscalPeriod,"00")) DESC
) AS BeginningBalance
, T1.EndingBalance
FROM Table1 T1
IT WOULD BE BETTER IF YOU USE BeginningBalance in primary table. Because it is only one record belong to one account. it is one to one relationship.
How about moving to the previous record, grabbing the value, and returning to the original record.
For example:
Private Sub txtXYZ1_AfterUpdate()
Dim tmpXYZ As Single
DoCmd.GoToRecord , , acPrevious
tmpXYZ = txtXYZ1
DoCmd.GoToRecord , , acNext
txtPriorXYZ = tmpXYZ
txtXYZChange = txtXYZ1 - txtPriorXYZ
End Sub

SSRS Grouping Data Together

I have a report that lists securities that are held and there security types. I want to some business logic that says security types 1, 2 and 3 are Equities, while security types 4, 5 and 6 are Bonds and then I want to group the report by these. Any idea how to do that? Right now the report lists each individual security type.
amend your dataset: within your select sql statement , lastly add the following:
CASE WHEN [security types] IN ('1', '2', '3') THEN 'Equities'
WHEN [security types] IN ('4', '5', '6') THEN 'Bonds'
ELSE 'others'
END AS securitiestype
Then with in your SSRS report you can now use securitiestype as a group filter.
A good way to do this is to add a calculated field to your dataset that evaluates this logic. Then you can group on this new column in the report.
Go to your dataset properties
Add a calculated field
Name it and enter the expression for it. It could be something like this:
=Switch(Fields!SecurityType.Value = 1 OR Fields!SecurityType.Value = 2 OR Fields!SecurityType.Value = 3, 'Equity'
,Fields!SecurityType.Value = 4 OR Fields!SecurityType.Value = 5 OR Fields!SecurityType.Value = 6, 'Bond', true, 'Other')
Add a grouping to your table/matrix using this new column

MS Access 2007/2010: Need to set Date field based on most recent of 5 other Date fields

I seem to have a problem I haven't encountered before within the same record. I need to set a Date field value based on the most recent date of 5 other Date fields. I would like to do this at the query level so that I can assign the most recent date and store it in a field, but I'm not sure if it's possible. If I have to do it within a Form, then I'm not sure how to write out the Case statement. Any help is appreciated!
What I'm trying to do now is write an expression that pretty much goes as follows:
ESA Exp Date: IIf([1-Record Search Date]>[2-Site Reconnaissance Date] And [1-Record Search Date]>[3-Owner Interview Date] And [1-Record Search Date]>[4-Lien/AUL Search Date] And [1-Record Search Date]>[5-User Questionnaire Date],[1-Record Search Date],IIf([2-Site Reconnaissance Date]>[1-Record Search Date]
And so on... However, expressions have a limit of characters, and so someone said in an unrelated question to write a function. I'm not sure how that would work in my case though.
Thanks again
Rob
If changing your table structure isn't an option then you can use a function like the one created by Allen Browne http://allenbrowne.com/func-09.html, there are fuller instructions on the webpage.
Call it in a query as: (may I suggest not putting spaces in whenever not required, will help further down the line in VBA)
ESAExpDate: MaxOfList([1-Record Search Date],_
[2-Site Reconnaissance Date],[3-Owner Interview Date],[4-Lien/AUL Search Date], _
[5-User Questionnaire Date])
I have pasted below in case the link dies.
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
End If
End If
Next
MaxOfList = varMax
End Function
The reason this seems so difficult is because your table structure is not optimal for relational querying. Let's say your current table (date_values) has the columns d1 to d5, all dates. You need to normalise the table like so (note that this doesn't change your actual table, it just 'repackages' it in memory into an easier-to-query form):
select entity_id, 'type 1' as date_type, d1 as date_value
from date_values
where d1 is not null
union all
select entity_id, 'type 2' as date_type, d2 as date_value
from date_values
where d2 is not null
union all
select entity_id, 'type 3' as date_type, d3 as date_value
from date_values
where d3 is not null
union all
select entity_id, 'type 4' as date_type, d4 as date_value
from date_values
where d4 is not null
union all
select entity_id, 'type 5' as date_type, d5 as date_value
from date_values
where d5 is not null
Note that entity_id is any field which uniquely identifies a row in your table. Once you have data in a normalised form, querying is easy:*
update date_values as d
inner join (
select ssq.entity_id, max(ssq.date_value) as max_date
from (
... the query I show above ...
) as ssq
group by ssq.entity_id
) as sq
on d.entity_id = sq.entity_id
set d.[ESA Exp Date] = sq.max_date
where entity_id = [... the entity you wish to update ...]
The last clause above, the where clause, restricts the modification to a single row you can specify. You can enter it manually when prompted by Access or you can have it refer to a field in a form. There are many options.
* OK, easi_er_.

MDX Help: - Comparing Values in Two Max Time Periods within a larger Set of Time Periods to Populate an Indicator

I'm brand new to MDX and need some help. In SSRS I have a dataset that pulls from an SSAS cube. The dataset always contains six months of data. What I need to be able to do is to compare a value for the max(timeID) with a value for the second max(timeID) and if the value for the max(timeID) > value for the second max(timeID) than the arrow goes up in the indicator, etc...
So for the dataset below I would subtract 20130201's Value which is 8 from
20130301's Value which is 10. The result would be a positive number and the indicator would be an upward pointing green arrow. If it was 0 it would be straight and if negative the arrow would be red and point down. I understand how to deal with the indicator - that's not an issue. It's the MDX I need help with.
20130201 8
20130301 10
20121201 4
I can write it in SQL and it would look like this.
Select Item, case when sum(Time1ContentCount) > sum(Time2ContentCount) then 3 when sum(Time1ContentCount) = sum(Time2ContentCount) then 2 when sum(Time1ContentCount) sum(Time2ContentCount) then 1 end as Indicator, sum(Time1ContentCount) as Time1Count, sum(Time2ContentCount) as Time2Count from (Select timeID, dc.Item, Case when timeID = (Select max(timeID) from FactUsage) then count(fu.Contentid) else 0 END as Time1ContentCount, Case when timeID = (Select max(timeID) from FactUsage where timeID <>(Select max(timeID) from FactUsage)) then count(fu.Contentid) else 0 END as Time2ContentCount from factUsage fu INNER JOIN dimContent dC on dc.ContentID = fu.ContentID WHERE TimeID in (Select distinct top 6 timeid from factUsage order by timeID desc) Group by timeID, Item) a group by Item
Thanks so much for your help!
Edit:
I changed the statement to read as follows for the indicator.
WITH Member MEASURES.Indicator AS (
IIF(( [Measures].[Activity], [Time].[Time ID].LastChild ) >
( [Measures].[Activity], [Time].[Time ID].LastChild.PrevMember),3,
(IIF(([Measures].[Activity], [Time].[Time ID].LastChild ) =
([Measures].[Activity], [Time].[Time ID].LastChild.PrevMember), 2,1))))
SELECT {Measures.Indicator} on 0
FROM [DW]
It works when I run it as a query against the cube in SSMS but I tried to put it in the indicator and that doesn't work. Just adding the IIF statement doesn't work either. When I tried to add it into the query or the cube itself so I could just pull from there it errors out with an out of memory error.
I don't know how much you can edit in the MDX expression - or in your report builder, but to get the difference between two values in a series, you can create a measure (in your report) that is the difference between the CurrentMember and PrevMember. Since the time series (timeid) is sorted by the key, it will always be in the right order (or your schema and architecture needs a rework)
So basically, you can do :
WITH
MEMBER MEASURES.GrowthTime AS (
( [Measures].[Value], [TimeID].CurrentMember ) -
( [Measures].[Value], [TimeID].PrevMember )
)
MEMBER MEASURES.GrowthRatio AS (
( [Measures].[Value], [TimeID].CurrentMember ) /
( [Measures].[Value], [TimeID].PrevMember )
)
SELECT { Measures.Value, Measures.GrowthTime, Measures.GrowthRatio } on 0,
[TimeID].CHILDREN on 1
FROM Cube
This is pseudo as i don't know your cube structure. For TimeID you would want it like [DimensionName].[AttributeName].CurrentMember and PrevMember