Relationship between GeneralJournalAccountEntry and LedgerJournalTrans_Project - axapta

We are attempting to relate GL transactions GeneralJournalAccountEntry with their appropriate Project Category Id LedgerJournalTrans_Project, but are having a very difficult time relating the two tables.
We can get there by this route:
GeneralJournalAccountEntry.GeneralJournalEntry = GeneralJournalEntry.RecId (Many to 1)
GeneralJournalEntry.SubLedgerVoucher = LedgerJournalTrans.Voucher (1 to Many)
LedgerJournalTrans.RecId = LedgerJournalTrans_Project.RefRecId (1 to 1)
But the individual leg of the transaction gets lost in the Many to 1 and 1 to Many relationships. We understand the tables are from different "modules", but are hoping there is some way to connect the two without depending on something messy like transaction amount.
Is this possible, or is there another way to accomplish our goal?

There is not a one-to-one relationship.
The closest thing would be to use Voucher and TransDate on LedgerJournalTrans to fetch ledger transactions.
See this post.
There is a white paper describing the AX 2012 ledger posting framework.

I'm not certain yet that I have this completely figured out, but I think this can be used to create a similar link. I'm trying to link the generaljournalaccountentry line items to the inventjounrnaltrans line items. This should provide the link between the two tables based on the voucher and line item number and give me the itemid. I would think there would necessarily be a similar link to the project because of the ability to navigate that way in the front end application. It might also be helpful to trace the SQL query when performing the action in the front end.
SELECT itp.[PARTITION]
, itp.[DATAAREAID]
, itp.[TRANSDATE]
, itp.[VOUCHER]
, itp.[ITEMID]
, itojt.[INVENTJOURNALLINENUM]
, MAX(ito.[REFERENCEID]) AS [REFERENCEID]
FROM [dbo].[INVENTTRANSPOSTING] itp WITH(NOLOCK)
INNER JOIN [dbo].[INVENTTRANSORIGIN] ito WITH(NOLOCK)
ON ito.[RECID] = itp.[INVENTTRANSORIGIN]
INNER JOIN [dbo].[INVENTTRANSORIGINJOURNALTRANS] itojt WITH(NOLOCK)
ON itojt.[INVENTTRANSORIGIN] = ito.[RECID]
WHERE (itp.[INVENTTRANSPOSTINGTYPE] = 1 OR itp.[INVENTTRANSPOSTINGTYPE] = 0)
AND itp.[ISPOSTED] = 1
GROUP BY itp.[PARTITION]
, itp.[DATAAREAID]
, itp.[TRANSDATE]
, itp.[VOUCHER]
, itp.[ITEMID]
, itojt.[INVENTJOURNALLINENUM]

Related

How to stop a row from show on Null count in query Access 2010

Here is the problem. I have a table and the table is 100% completed. There are no null values in the table. The table is broken down to:
Division > Region > RAPM > Status > Disposition
I want to count how many times "Training" is in [Disposition] for a [Region] using a query.
The error I get is when a [Region] has 0 "Training" in [Disposition] the count is a coming back as Null so the entire row is not shown.
How do i get the count to come back as "0" so I can keep the [Division], [Region], & [RAPM] in the results for reporting even if there is 0 count for training.
I have tried NZ() but this will not work because there is technically no Null cell to be converted.
Here is the statement:
SELECT tblAlignment.Division, tblAlignment.Region,tblAlignment.RAPM, Count(tblCase.Dispostion) AS CountofTraining
FROM tblCase INNER JOIN tblAlignment ON (tblCase.Region = tblAlignment.Region) AND (tblCase.Store = tblAlignment.[Store Number])
Where (((tblCase.Status)="Closed") AND ((tblCase.Disposition)="Training")
Group BY tblAlignment.Division, tblAlignment.Region, tblAlignment.RAPM
HAVING (((tblAlignment.Division)=[Forms]![frmDashboardNative]![NavigationSubform].[Form]![NavigationSubform].[Form]![Combo16]))
This is not the complete answer, but my reputation is not high enough to comment so this is the only way I can respond. To get a fuller answer you would need to provide more detailed table structure including primary keys and relationships between the tables. Making guesses from what you have provided I can make a couple of suggestions, but your post raises a few questions:
You say you want to count related entries based on same Region, but your join links on Region and Site. Is there a relationship between Site and Region? Can a single site only ever appear in one Region? If so then I think this information should possibly be in a separate table.
I think the HAVING condition should actually be in the WHERE clause.
I think you may have duplicated the ![NavigationSubform].[Form]
Anyway, a slightly more generic example of one way of achieving what you're after would be:
SELECT a.Region, Nz(b.RecordCount, 0) AS FinalCount
FROM TableA a
LEFT JOIN (SELECT Region, Count(*) AS RecordCount
FROM TableB
WHERE Status = "Closed" AND Disposition = "Training"
GROUP BY Region) AS b ON a.Region = b.Region
WHERE a.Division = [Combo16]
Hope this is of some help.

SQLITE for each row of a table, count occurences in two others tables

I need some help to simplify the below request and also make it faster to execute. Here is what I'm trying to do :
Table Player (idPlayer, namePlayer)
Contains the list of players
Table EventPVE (idPlayer)
Each line represents an event when a player got killed by the game environment
Table EventPVP (idPlayerKilled, idPlayerKiller)
each line represents an event when a player kills or get killed by another player
I'm trying, for each player, to count how many time they got killed by the environment, how many time they got killed by a player and how many time they killed another player. The request below works but is very slow (3 subrequests of course)
Could you please help me to simplify it ? I'm very bad at sql .... I've seen some "solutions" with LEFT JOIN but it seems that it doesn't work in my case. The first LEFT JOIN works well but the others return bad counts.
SELECT p.idPlayer, p.namePlayer,
(
SELECT COUNT(*)
FROM EventPVE pve
WHERE pve.idPlayer = p.idPlayer
) AS pveDeath,
(
SELECT COUNT(*)
FROM EventPVP pvp
WHERE pvp.idPlayerKiller = p.idPlayer
) AS pvpKills,
(
SELECT COUNT(*)
FROM EventPVP pvp
WHERE pvp.idPlayerKilled = p.idPlayer
) AS pvpDeaths
FROM Player p
Here is a test db : https://www.dropbox.com/s/tc7z1i26mumvhgm/test.db?dl=0
The query itself already is as simple as possible.
The problem is that the database has to scan all the tables to find rows with matching player ID values, and it has to do this once for every player.
These lookups could be sped up with some indexes:
CREATE INDEX ep ON EventPVE(idPlayer);
CREATE INDEX pkiller ON EventPVP(idPlayerKiller);
CREATE INDEX pkilled ON EventPVP(idPlayerKilled);
I think there's no need of second Table EventPVE.
Try this:
SELECT *FROM eventpvp left join player on eventpvp.idPlayerKilled=player.idPlayer group by idPlayerKilled
It is working for me. I didn't used the database you provided, i created one.

Retrieving data from two tables

I have two tables vendors and customers. My problem is that I want to make the search of the name and in result I get the common data from both the tables(if any exist).
Example: If I search the name John and it exits in both the tables so in result I must get the gridviews of both the tables.
Your question is not quite clear for me. Are you looking for a SQL statement?
Try with:
SELECT * FROM Vendors LEFT JOIN Customers ON Vendors.Name = Customers.Name
or
SELECT * FROM Vendors WHERE Vendors.Name IN (SELECT Customers.Name FROM Customers)

T-Sql ordering results with count() by priority

I am trying to build a system, which has threads and posts. I am trying to fetch a thread that is the most popular (the user can click "like" button to make it more popular) and has most posts. The problem is to order the results by most posts..and then by liked posts.
So for example, if I have a thread with 300 posts, and 200 likes.. while another thread has got 300 likes and 201 likes..I want the second post to be selected..
Table structure in a nutshell:
topic:
--------
topic_id
liked
comment:
-------
comment_id
topic_id
Here is my stored procedure so far:
dbo.Trends
AS
SELECT TOP 1 title, COUNT(com.topic_id), COUNT(topc.user_id_liked)
FROM comment AS com
INNER JOIN topic AS topc ON com.topic_id=topc.topic_id
GROUP BY com.topic_id, topc.user_id_liked,title
ORDER BY COUNT(com.topic_id), COUNT(topc.user_id_liked) DESC
I am not sure if I am right, or will I have to result to control flow logic. I placed the topic_id from the topic table before topic liked column in the order statement..hoping the selecting/ordering of the topic_id will take precendence.
UPDATED: query updated.
I don't really know that you want. But maybe this will help:
;WITH CTE
AS
(
SELECT
COUNT(com.topic_id) OVER(PARTITION BY topc.liked) AS topicCount,
COUNT(com.liked) OVER(PARTITION BY topc.topic_id) AS likedCount,
title
FROM
commnet AS com
INNER JOIN topic AS topc
ON com.topic_id=topc.topic_id
)
SELECT TOP 1
CTE.title,
CTE.topicCount,
CTE.likedCount
FROM
CTE
ORDER BY
topicCount,
likedCount
EDIT
The differences between the GROUP BY and PARTITION BY is that PARTITION BY is an inline GROUP BY so this will not affect the number of rows. I like to use that in a CTE that is a inline view. Makes it clearer and you separate the different steps you want to do. If you remove the TOP 1 you will see what I mean.

Complex datasource issue in Dynamics AX

I have a grid that displays lines from a table. Now, I have these two requirements:
Show only the lines that contains values in the "hour" fields. The "hour" field is an array type.
Show the lines from a project and its subproject
My problem is this: to meet requirement #1, I need to use a select statement in my datasource since I cannot access array value using QueryBuildDataSource (this is a known limitation in Dynamics AX).
But to meet requirements #2 I need to have two "exists join", and this is not possible in Dynamics AX. For example the following code would not work:
select from table where
exists join tableChild where projectId = MyProjectId OR
exists join tableChild where parentProjectId = MyProjectId
How would someone address this issue?
From the select statement example you provided, it appears it may not be necessary to have two exist joins. A single exists join with the "OR" for the two possible conditions in its where clauses may be sufficient, of course, you should still have some relationship between table and tableChild for the join to make logical sense.
select from table
exists join tableChild
where (tableChild.projectId = MyProjectId || tableChild.parentProjectId = MyProjectId)
You can use array values in query ranges providing field IDs using fieldID2Ext function.
You can build view and apply confitions on top of it.

Resources