How to find out which of the games happened on Mondays only? - database

I have tried 2 codes, the first one hasn't worked, while the second has. I basically have to display how many games were played on Mondays and show the teams that played them.
MATCH (t:Teams)
WHERE date({year:2019, month: 1 }) > t.Date <= date({year:2018, month:12})
RETURN t.HomeTeam AS HomeTeam,
t.AwayTeam AS AwayTeam,
t.Date AS Date
The result is: (No changes, no records) - nothing
MATCH (t:Teams)
WITH [item in split(t.Date, "/") | toInteger(item)] AS dateComponents
WITH ({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
WHERE date = 1
RETURN COUNT(*)
The result is: Count(*) 0

I think there may be a couple of things going on in your first query. The date matching line
WHERE date({year:2019, month: 1 }) > t.Date <= date({year:2018, month:12})
is looking for a date that is less than 20190101 and less than or equal to 20181201. If you are actually looking for a date between those two values you need to change the operator to greather than equals for 201801.
That said, if Date is actually a string then the date comparison will not work either.
In your second query, it looks like you decided that Date was indeed a string and you split it up but still did not get any results. Although you break the date string up into its components you did not supply the date() function around your date components in this line...
WITH ({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
Try this for your second query.
MATCH (t:Teams)
WITH [item in split(t.Date, "/") | toInteger(item)] AS dateComponents
WITH date({day: dateComponents[0], month: dateComponents[1], year: dateComponents[2]}).dayOfWeek AS date
WHERE date = 1
RETURN COUNT(*)

Related

Date Calculation using DateAdd function not calculating correct date

I need to calculate six months based on below condition :
If EPC Review Date is present then Six Month = EPC ReviewDate + 6M (disregard all other dates)
If MDS and modified both present and MDS Review Date > Modified, then Six Month = MDS review date + 6M
if MDS and Modified both present and Modified > MDS, then six months = Modified + 6M
I wrote the below code
,SIXMONTH = CASE WHEN (EPC.REVIEWDATE IS NOT NULL OR EPC.REVIEWDATE <> '') THEN
CONVERT(VARCHAR(10),DATEADD(MM,6,EPC.REVIEWDATE),101)
WHEN ((EPC.REVIEWDATE IS NULL OR EPC.REVIEWDATE = '') AND (MODI.MODIReviewDate IS
NULL OR MODI.MODIReviewDate = '')) THEN
CONVERT(VARCHAR(10),DATEADD(MM,6,MDS.MDSReviewDate),101)
WHEN ((EPC.REVIEWDATE IS NULL OR EPC.REVIEWDATE = '') AND MDS.MDSReviewDate >
MODI.MODIReviewDate) THEN
CONVERT(VARCHAR(10),DATEADD(MM,6,MDS.MDSReviewDate),101)
WHEN ((EPC.REVIEWDATE IS NULL OR EPC.REVIEWDATE = '') AND (MDS.MDSReviewDate <
MODI.MODIReviewDate)) THEN
CONVERT(VARCHAR(10),DATEADD(MM,6,MODI.MODIReviewDate),101)
END
But my conditions are not working correctly, and it's not calculating date correctly. If data is like below
EPC_revire Date MDS_revieDate Modi_reviewDate SixMonth
NULL 04/27/2022 09/01/2021 03/01/2022 -- it should add 6 months to MDS
Null 11/10/2021 06/23/2022 05/10/2022 -- it should add 6 month to Modi
10/25/2021 07/21/2021 null 04/25/2022 -- it correctly added 6 months to EPC
Can anyone help, please.
Give this a try:
SIXMONTH = CASE
WHEN EPC.REVIEWDATE IS NOT NULL THEN dateadd(month, 6, EPC.REVIEWDATE)
WHEN MODI.MODIReviewDate > MDS.MDSReviewDate THEN dateadd(month, 6, MODI.MODIReviewDate)
WHEN MODI.MODIReviewDate < MDS.MDSReviewDate THEN dateadd(month, 6, MDS.MDSReviewDate)
END
I leave you to figure out what to do when MODI.MODIReviewDate = MDS.MDSReviewDate. If either of those dates (MODIReviewDate, MDSReviewDate) is null, the comparison will return UNKNOWN and fall through to the next branch (if present). This is why you don't need to keep repeating various comparisons.
Note that you save a TRIVIAL amount of typing using the datepart abbreviation; using the complete datepart name makes the code much easier to read and understand. A little effort at formatting your code also helps.
I also ignored the converting to varchar(10). Formatting belongs in the presentation layer.

Measure does not work for Month Threshold

I build this Dax measure
_Access_Daily = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]),
FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))))+0
_Access__PreviousDay = CALCULATE(
DISTINCTCOUNTNOBLANK(ApplicationAccessLog[ApplicationUserID]), FILTER('Date','Date'[DateId]=SELECTEDVALUE('DateSelector'[DateId],MAX('DateSelector'[DateId]))-1 ))+0
The Date Selector table is a disconnected table containing dates from the 20th Jan to now. Dateid is a whole number like 20200131.
The Date table is a standard date table with all the dates between 1970 and 2038. Date id is a whole number like 20200131.
However it does not seems to work for the month threshold between Jan and Feb ? So if selected date is 01/02/2020 then it does not return correctly for the 31/01/2020.
As mentioned in the comments, the root problem here is that the whole numbers you use are not dates. As a result, when you subtract 1 and cross month (or year) boundaries, there is no calendar intelligence that can adjust the numbers properly.
Your solution (using 'Date'[DayDateNext]) might work, and if for some additional considerations this design is a must, go with it. However, I'd suggest to revisit the overall approach and use real dates instead of "DateId". You will then be able to use built-in DAX time intelligence, and your code will be more elegant and faster.
For example, if your "Date" and "DateSelector" tables have regular date fields, your code can be re-written as follows:
_Access_Daily =
VAR Selected_Date = SELECTEDVALUE ( 'DateSelector'[Date], MAX ( 'DateSelector'[Date] ) )
VAR Result =
CALCULATE (
DISTINCTCOUNTNOBLANK ( ApplicationAccessLog[ApplicationUserID] ),
'Date'[Date] = Selected_Date
)
RETURN
Result + 0
and:
_Access_PreviousDay =
CALCULATE ( [_Access_Daily], PREVIOUSDAY ( 'Date'[Date] ) )

Netezza date function for current date - 16 days

I want to pull today's date plus the last four weeks. Does anyone know a function for this in Netezza? What I have below is a guess that doesn't work. Also, I don't want to extract the date.
Select c.BUSINESS_UNIT_NBR, c.BUSINESS_UNIT_NAME, b.STORE_NBR, b.INV_CUST_ACNT_NBR,c.INV_CUST_NAME, a.NDC_NBR, a.GENERIC_NAME, a.INV_NBR, a.CONTRACT_ID, a.CONTRACT_NAME, a.ORD_DT, b.INV_DT, b.SHIP_DT, a.ORD_QTY, a.SHIPPED_QTY, a.INV_PRICE_AMT, a.INV_COST_AMT, a.MARKUP_MARKDOWN_PCT, a.INV_LINE_AMT
from fct_dly_invoice_detail a, fct_dly_invoice_header b, dim_invoice_customer c
where a.INV_HDR_SK = b.INV_HDR_SK
and b.DIM_INV_CUST_SK = c.DIM_INV_CUST_SK
and a.SRC_SYS_CD = 'ABC'
and a.NDC_NBR is not null
**and b.inv_dt(current_date)-16**
and b.store_nbr in (813, 1197, 2771, 3048, 3177, 3387, 3477, 3602, 3766, 3912, 4020, 4138, 4228, 4434, 4435, 4507, 4742, 4791, 5353, 5392, 5775, 5776, 5890, 6177, 6692, 6736, 6806, 7933, 9175, 9472)
Assuming inv_dt is the column you want to filter on, your where predicate should include:
WHERE
...
inv_dt between CURRENT_DATE - 16 and CURRENT_DATE
...
16 days does not equal four weeks, but adjust that number accordingly to your needs.

filter by last 13 months

I have controller that is using a standardSetController to implement pagination. I want to filter by the trade date for the last 13 months. The date literals don't have a filter by Last_N_Months:N
Is there a way I can filter by the last 13 months?
Here is my current query:
setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([select TransactionType__c, TradeDate__c, ShareClass__c,
SettlementDate__c, Name, Fund__r.Name, Fund__r.Id, FirstTransaction__c, DCPosition__c, DBR__r.Name, DBR__r.Id, DBR__c,
Amount__c from Transaction__c where DBRPrimaryContact__r.Contact__c =: con.Id ORDER BY TradeDate__c ASC]));
If I can't filter by 13 months, what is the total number of records that can be returned in a query? Is it 2000? There can be a significant number of records for this object and I want to limit the results by 13 months of data. Once I have that result set, I want to add filtering by options.
Thanks for any help.
Try the code below for filtering Date range - you can programatically calculate exact date ranges
DATE d1 = date.today();
Date d2 = d1.addMonths(-13);
Integer d3 = d2.daysBetween(d1);
System.debug('*************' + [SELECT Id FROM Account WHERE CreatedDate >= :d2 AND CreatedDate <=:d1]);
For filtering by date you have LAST_90_DAYS or LAST_N_DAYS:90 like this
SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:90
More info in this link
And yes the total number retrieved in one query is 2000

find the Day of week of a particular date

i have an object which has 2 dates startdate_c and enddate_c .
i need to find a way to find the days of week these dates fall in
For example
startdate = 1 jun 2012 and enddate = 3 jun2012
I need to know which days of the week the days between these dates fall in.
In this example
Mon = false, tue = false, wed = false, thu=false, fri=true,sat=true,sun=true
I want to use this in a Vf page to render the somefields based on the boolean value.
Any pointers would be of great help.
Date has a method called toStartOfWeek which you could leverage, assuming your two dates do lie within the same week you could simply do something like this:
date weekStart = startdate.toStartOfWeek();
list<boolean> days = new list<boolean>();
for(integer i = 0; i < 7; i++)
{
days.add(weekStart.addDays(i) >= startdate && weekStart.addDays(i) <= enddate);
}
A little bit crude, but it'll give you an array of 7 boolean values. For longer/unknown ranges you could use a date cursor and increment that instead of an integer here, but this should get you started. Note, I've not tested this code ;)

Resources