Get all Activities to one Account - CRM 2016 - sql

my question is, is it possible to get all Activities for one Account in CRM by a SQL-Query in a acceptable period of time?
Surprising is, that all the Activities are in the Account-Overview in CRM. And that page loads instantly.
I've build a query just for Email-Activities. The query runs round about 25mins. Which is not suprising to me xD. But i can't find a clear relationship between the 2 tables.
Some data:
~460000 Email-Activities
~28000 Contacts
~37000 Accounts
Here's the sql-query:
select account.Name, Max(email.CreatedOn) from Email
as email
join Contact as contact on email.DirectionCode = 1
and datediff(wk, email.CreatedOn, GetDate()) > 12
and (email.ToRecipients Like '%' + contact.EMailAddress1 +'%'
or email.ToRecipients Like '%' + contact.EMailAddress2 +'%'
or email.ToRecipients Like '%' + contact.EMailAddress3 +'%')
join Account as account on account.AccountId = contact.AccountId
Group by account.Name

The problem is in your qry. When you use like with '%...' it will newer use index.
The best solution in your case is catch qry (with SQL Server Profiler (in SSSMS -> tools -> SQL profiler)) - It'll give you information about dataset and you can get connection between tables via reverse analysis.

Related

How can I Select records, using a join, that meets two criteria for the same column?

I have a database with two (actually more) tables: Accounts & Assets. The relationship is many Assets to one Account.
Example of existing data:
Account Asset
Abbot EZ111
Abbot 7000261
Anderson EZ431
Barton EZ207
Barton 8000400
Charlie 8000465
Charlie EZ576
Dickens EZ420
Elmer EZ982
I would like to find all Accounts that DO have an Asset that starts with 'EZ', but DO NOT also have an Asset that starts with a '7' or an '8'.
So for the data above, I'd be expecting the result of:
Account Asset
Anderson EZ431
Dickens EZ420
Elmer EZ982
Disclaimer: Obviously I don't do SQL for a living, but I'm learning a lot from the W3 site. What I can't figure out is the basic structure of this query. I know how to do the Join, but I don't know how to query the same Column twice.
Sorry if I'm a bit of a newbie for this site, but I mainly use create Groups within InforCRM. A query like this seems to be outside the capabilities of the Group's "Search Criteria" function.
Best Regards,
TimL
The way you phrased it, I would suggests not exists:
select t.*
from t
where asset like 'EZ%' and
not exists (select 1
from t t2
where t2.account = t.account and
(t2.asset like '7%' or t2.asset like '8%')
);
select * from tablename
where asset like 'EZ%' and account not in (select account from tablename where asset like '7%' or asset like '8%')
Others beat me to it but here is a complete working example based on what I think your data could look like
https://www.db-fiddle.com/f/8R6CZ36vjZgCUCdqU3DRi/1
SELECT ACT.ACCOUNT, AST.ASSET
FROM ACCOUNT ACT
INNER JOIN ASSET AST ON ACT.ACCOUNT = AST.ACCOUNT
WHERE AST.ASSET LIKE 'EZ%'
AND ACT.ACCOUNT NOT IN (
SELECT ACCOUNT FROM ASSET WHERE ASSET LIKE '7%' OR ASSET LIKE '8%'
);

Using SQL query - How to identify the attribute's that a OIM request has updated + OIM 11g R2 PS3

We extend contractor term date in OIM to 80 days but some times it gets extended by admins/managers more than 80 days. When it gets extended, OIM creates a request id. Now, we would like to know all the users who term date is more than 80 days from the day(request creation date) they got extended.
Is there a way to get the details of the users and the request creation date that happened on termination date attribute in a SQL query so that we can create a BI report.
As i have a requestid which was created yesterday i am using it for developing the query. I tried below query by joining usr, request and request_beneficiary tables but it doesn't return anything. Are there any other tables which i need to use to accomplish this use case.
-- Even try with specific requestid req3.request_id=123456
-- Tried with the request id's beneficiary key too.
SELECT
req3.request_key rk,
usr2.usr_login buid,
usr2.usr_status,
req3.request_creation_date,
req3.request_model_name,
to_char(usr2.usr_udf_terminationdate, 'MM-DD-YYYY') AS Terminationdate
FROM
request req3,
request_beneficiary reqb1,
usr usr2
WHERE
req3.request_key = reqb1.request_key
AND beneficiary_key = usr2.usr_key
and usr2.usr_status = 'Active'
AND usr2.usr_emp_type IN ( 'Contractor');
If anyone has done this type of use case. can you please provide your inputs.
Appreciate your inputs and suggestions
Thanks in advance.
I'm sure you've already figure this out, but here is some SQL that should get you to the data you need.
SELECT r.request_key rk,
R.Request_Creation_Date,
Red.Entity_Field_Name,
Red.Entity_Field_Value,
usr_status,
usr_end_date,
usr_udf_terminationdate
FROM request r
INNER JOIN Request_Entities re
ON R.Request_Key = re.request_key
INNER JOIN Request_Entity_data red
ON re.request_entity_key = red.request_entity_key
INNER JOIN usr
ON Re.Entity_Key = usr.usr_key
WHERE request_model_name = 'Modify User Profile';

Pull back specific post codes from table in SQL

I'm having an issue in selecting specific post codes.
These are all valid UK post code formats:
WV11JX
WV1 1JX
WV102QK
WV10 2QK
WV113KQ
WV11 3KQ
Now, say I had a mix of the above formats in the data table; I'm trying to select only post codes that conform to the WV1 prefix (in this example).
In the above 6 item list, I'd want to return:
WV11JX
WV1 1JX
I would want to exclude:
WV102QK
WV10 2QK
WV113KQ
WV11 3KQ
If I execute the following query this will bring back both the WV11's and the WV1's:
SELECT ad.PostCode,*
FROM Staff st
INNER JOIN Address ad on ad.AddressID = st.Address
WHERE
ad.PostCode like 'WV1%'
Changing the condition in the WHERE to cater for length like this doesn't really work either:
SELECT ad.PostCode,*
FROM Staff st
INNER JOIN Address ad on ad.AddressID = st.Address
WHERE
(
ad.PostCode like 'WV1%'
OR
(ad.PostCode like 'WV1%' and LEN(ad.PostCode) = 6)
The above will just filter out any of the formats with a space so if we cater for those by doing the below:
SELECT ad.PostCode,*
FROM Staff st
INNER JOIN Address ad on ad.AddressID = st.Address
WHERE
(ad.PostCode like 'WV1%' and LEN(ad.PostCode) = 6)
or
(ad.PostCode like 'WV1 %' and LEN(ad.PostCode) = 7)
That fixes the issue but the problem is that we want to check more than just the 'WV1' prefix in this manner so having a growing list of 'OR' comparisons isn't viable.
How do we isolate the above post codes in a scalable way?
I'm using Microsoft SQL Server 2016.
I think the logic you want is:
WHERE ad.PostCode like 'WV1 [0-9][A-Z][A-Z]' OR
ad.PostCode like 'WV1[0-9][A-Z][A-Z]'
I'm not sure if numbers are allowed for the last three characters.

Joining SQL statements Atrium Syntess Firebird DB

I'm trying to get (one or multiple) number lines (PROGCODE) that are attached to an OBJECT (i.e. a building) that is connected to a Relation which in turn has a GC_ID (relation unique ID). I need all the buildings & progcodes connected to a relation ID in a firebird 2.5 database generated by my companies ERP system.
I can look through all the tables in the firebird database and run select queries on them.I like to think I have the join statement syntax down and I know how to find the unique ID belonging to a relation, unfortunately I'm unsure how I can find the correct table that houses the information I seek.
The table I think this data is in has the following fields:
GC_ID, DEVICE_GC_ID, USER_GC_ID, CODE, DESCRIPTION.
However when I query it using
select GC_ID, DEVICE_GC_ID, USER_GC_ID, CODE, DESCRIPTION
from AT_PROGCODE A
Then I get a description of the fields I'm trying to query.
i.e.
| GC_ ID : 100005 | DEVICE_GC_ID : 100174 | USER_GC_ID : 1000073 | DESCRIPTION: >description of what I'm trying to query< |
Can anyone shed some insight how I should handle this?
Update 7-09-2017
I spoke with the ERP consultant and was told the tables I needed (if anyone reading this is using syntess Atrium; the AT_BRENT table holds a description of all the tables.)
However, I've run into a new problem; the data I get from my sql query keeps streaming (it seems to never end with me stopping the script at 90 mil loops and the ERP program crashing if I ask for a count).
select A.GC_OMSCHRIJVING Bedrijf, A.GC_CODE ,M.GC_OMSCHRIJVING Werktitel,
M.TELEFOON1, M.TELEFOON2, M.MOBIEL, M.EMAIL,
M.URL, M.DOORKIES_NR, M.WERKLOCATIE, M.EMAIL_INTERN
from AT_MEDEW M , AT_RELATIE A
JOIN AT_MEDEW ON A.GC_ID = M.GC_ID
WHERE M.TELEFOON1 <> '' OR M.TELEFOON2 <> '' OR M.MOBIEL <> ''
Any ideas on what's the cause for my latest peril?
First I had to find the AT_BRENT table which holds all the descriptions for tables in Syntess Atrium
Then I was using a CROSS JOIN (as pointed out by https://stackoverflow.com/users/696808/bacon-bits )
I ended up using
select A.GC_OMSCHRIJVING Bedrijf, A.GC_CODE ,M.GC_OMSCHRIJVING Werktitel,
M.TELEFOON1, M.TELEFOON2, M.MOBIEL, M.EMAIL,
M.URL, M.DOORKIES_NR, M.WERKLOCATIE, M.EMAIL_INTERN
from AT_MEDEW M
JOIN AT_RELATIE A ON A.GC_ID = M.GC_ID
WHERE M.TELEFOON1 <> '' OR M.TELEFOON2 <> '' OR M.MOBIEL <> ''
Thank you all who helped.

Best Table Structure Design for Dynamic SQL statement

I am looking for the best table structure to create a dynamic SQL statement like this below (which is not dynamic yet). I have to choose between
joined tables
a single row with all columns with the content comma-delimted which I then will parse
one large table with multiple rows per Cost Centre Activity Code
or anything else
In this example the key which all link to is: 'NSEA8102' which is a Cost Centre Activity code
SELECT
#pDate,
#pDate,
'NSEA8102', --Cost Centre Activity Code
ccg.tCCGroup,
SUM(logs.tTripHours) AS tTriphours,
'Actual EMV Hours Worked - ' + DATENAME(MONTH,#pDATE) + ' ' + CAST(YEAR(#pDate) AS CHAR(4))
FROM dbo.tblEMV_Logsheet AS logs INNER JOIN
dbo.tblLookup_EMVEquipment AS ccg ON logs.tEquipmentKey = ccg.tEquipmentKey
WHERE tDate BETWEEN #BMonth and #EMonth
AND (logs.tAreaCode in ('MINEE', 'SERVICE'))
AND (logs.tEventCode LIKE 'RASSTEEPS')
AND logs.tSourceLocationCode = 'STEEPS'
AND logs.tDestinationLocationCode = 'ERASSTSP'
AND (ccg.tCCGroup IN ('FADT', 'FPC800', 'FWA800'))
AND ccg.tValid = 1
GROUP BY ccg.tCCGroup
Any suggestion would be welcome. Thanks