How can I group by date using datetime field in FetchXML query - dynamics-crm

I have an entity in Dynamics CRM with attributes that look like this (just a small snippet):
Date (datetime type) UserID (GUID type) Operation Entity
2020-09-24 00:47:08.000 16742A71-ED5F-E611-80EA-005056B53B31 Delete Account
2020-09-24 00:47:08.000 16742A71-ED5F-E611-80EA-005056B53B31 Create Opportunity
2020-10-07 05:37:54.000 16742A71-ED5F-E611-80EA-005056B53B31 Update Contact
2020-10-07 02:34:45.000 16742A71-ED5F-E611-80EA-005056B53B31 Update Contact
2020-10-07 09:39:02.000 16742A71-ED5F-E611-80EA-005056B53B31 Update Contact
What I'm looking to do is to get unique combination of Date (don't care about the time portion), UserID, Operation and Entity. So based on the data snipped above, I want to get 3 records as a result of a FetchXML query since the last 3 records were created on the same exact date by the same exact user doing the same exact operation against the same exact entity and the top 2 were unique.
Date (datetime type) UserID (GUID type) Operation Entity
2020-09-24 16742A71-ED5F-E611-80EA-005056B53B31 Delete Account
2020-09-24 16742A71-ED5F-E611-80EA-005056B53B31 Create Opportunity
2020-10-07 16742A71-ED5F-E611-80EA-005056B53B31 Update Contact
This would be an equivalent of GROUP BY cast(createdon as date). Is this possible to achieve in FetchXML at all?
TIA,
-Tony.

Yes, this is possible using a dategrouping.
The below example groups by year, but this article explains the other options, including day and month.
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='opportunity'>
<attribute name='opportunityid' alias='opportunity_count' aggregate='count'/>
<attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum'/>
<attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg'/>
<attribute name='actualclosedate' groupby='true' dategrouping='year' alias='year' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='Won' />
</filter>
</entity>

Related

Filter table by another table based on date field

I have a detail table in my model with a row for every hour in a day and the sales amount generated to that point of the date. Its not displayed on my report, rather it acts as the base for another reference table in the model:
Datetime Sales
2019-11-10 06:00:00 100.00
2019-11-10 12:00:00 200.00
2019-11-10 18:00:00 500.00
2019-11-10 23:59:59 999.00
The first (reference) table in the model only displays in my report the last hour value of each day in my base table. they are related via the datetime field using a 1:M with a single cross filter setting:
let
Source = Base,
#"Filtered Rows" = Table.SelectRows(Source, each ([Datetime] = #time(23, 59, 59)))
in
#"Filtered Rows"
Datetime Sales
2019-11-10 23:59:59 999.00
2019-11-11 23:59:59 950.00
2019-11-12 23:59:59 900.00
I would like to place a third table on my report that shows each of the hourly rows from the base table when a user clicks on a row in the daily table with the default setting being today's data which is added hourly.
I went down the path of using Power Query to try and filter the base table but have not been able to make it work. Should this instead be done using DAX? Either way, what would the query look like?
thanks in advance!
Thanks, but I have just resolved this. By changing my relationship to use a date field instead of the datetime field, and changing it to a (*:1) type, i am now getting the filter to do what I want. Now just have to figure out how to default to today's date. Thanks again!

How do I get a Month from LocalDate in JPQL query?

I got a transaction with a LocalDate as created attribute:
private LocalDate created;
I also got following query to retrieve every date out of my table transaction.
SELECT DISTINCT t.created FROM Transaction t
This however just gives me all dates and I am mapping those dates to all month that were found.
public List<Month> findAllUsesdMonth() {
return getEm().createNamedQuery(Transaction.FIND_ALL_MONTH, LocalDate.class).getResultList().stream().map(LocalDate::getMonth)
.collect(Collectors.toList());
}
This leads to duplicate month obviously because I selected distinct dates and not distinct months. A workaround would be to use a Set instead of a List.
I would like to retrieve only the month in the JPQL query instead of mapping them afterward. Is there any possibility to do so?
Date format: yyyy-MM-dd HH:mm:ss
Query: SELECT t FROM Transaction t GROUP BY (SUBSTRING(t.created, 6, 7))
By calling this NamedQuery you will get distinct month.

Getting DeadLock while updating the table

We are using spring + mybatis +sqlserver.
Scenario is something like we have a table, suppose transaction_table. We received some data while using jdbc inbound channel. After getting the data we do a insert into this table. and we have a status column in this table.
Which needs to be updated after every steps. We have a one persistence layer class which has all the data base function to call the dao layer methods. We are using annotation for transaction handling.. for this we have added #Transaction on the top of the methods.
In dao.xml we have written something like this -
INSERT INTO TRANSACTION_DETAIL(
Column_1, Column_2, Column_3, Column_4
)
<foreach collection="transactionsSubLst" item="element" index="index" separator="union all">
SELECT
#bean.prop,jdbcType=INTEGER,
#bean.prop2,jdbcType=VARCHAR,
#bean.prop3,jdbcType=VARCHAR,
#bean.prop3,jdbcType=VARCHAR
</foreach>
<foreach collection="transactionsSubList" item="element" index="index" separator=";">
UPDATE TRANSACTION_DETAIL
<set>
Column_3=#element.targetSystem,
Column_4=#element.ruleName,
Column_2=#element.ruleVersion,
Column_1=#element.weightage
</set>
<where>
Column_1=#element.transactionNo
AND Column_2=#element.requestNumber
</where>
</foreach>
When we ran this on single node... then we are fine, but as we move to multiple node, we start getting DeadLock on updates..

oracle xmltable performace - converting clob to xmltype and extracting for multiple records

I have a specific issue. i am trying to retrieve values from a xml clob field in a column. this xml is very large and i have of over 40 thousand records that have this xml_clob. I am currently able to retrieve information using the displayed query but found that performance is a big issue. Currently, its take hours to process the query. i need to be able to reduce this query processing time just a few minutes. There is not xml_schema provided. Access to the server is very limited so i can can create is temporary Table. But this has not been much help. These follow query only displays "Gender". It takes minimum of 15 minutes.
example of xml
<root>
<entry>....</entry>
<entry effective="2010012108354553" timestamp="2010012108354553" id="12345">
<field name="Org" t="0" s="4" d="0">
<nv>0000</nv>
</field>
<field name="FirstName" t="1" s="13" d="0">
<nv>John</nv>
</field>
<field name="LastName" t="1" s="13" d="0">
<nv>Doe</nv>
</field>
<entry>
</root>
query:
SELECT X.*,X2.*
From XML_TABLE XM,
XMLTABLE (
'for $e in /entry
where $e/#type = "create"
and $e/#effective > 20110229
and $e/#effective < 20140229
return $e'
passing extract(xml, '/root/entry')
COLUMNS
effective VARCHAR(60) PATH '/entry/#effective'
) AS X ,
XMLTABLE (
'for $i in /field
where $i/#name = "Gender"
return $i'
passing X.xml
COLUMNS
name VARCHAR(60) PATH '/field/#name',
old_value VARCHAR(60) PATH '/field/ov',
new_value VARCHAR(60) PATH '/field/nv'
) AS X2

search record using year criteria in hibernate

I need all records which have year entered from search criteria.
for ex: String year = "2012";
In hibernate how it work ?
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is fetch in format DD-MM-YY from oracle database.
Please suggest..
Try something like:
criteria.add(Restrictions.sqlRestriction("to_char({alias}.CREATE_DATE, 'yyyy') = ?", year, Hibernate.STRING));
I'm assuming that the name of the column mapped to createDate is CREATE_DATE

Resources