How to show the day in which the most connections conducted in each month in Qlik sense - qlikview

My data is about connections in 2019. It should show the day in which maximum connections conducted in each month.
It should be like that:
Jan
01.03.2019
45
Feb
02.05.2019
43
But i was able to achieve:
Jan
01.03.2019
752
Feb
02.05.2019
742
This so far. It shows total number of connections in that month instead of that day.
I will be grateful if somebody helps me solve this. Thanks ahead.

Depending on how exactly you want the end result to look, you can use a combination of the following functions and expressions:
Aggr() function
Max() function
Only() function
Concat() function
Total qualifier
Set analysis
Here's what we can achieve using those functions:
To create the field [Connection Month], we simply use the expression Month([Connection Date]) in the Data Load Editor when creating the new field, though it may be wise to use something like MonthName([Connection Date]) instead in case we ever need to view multiple years at the same time.
Then, to create the aggregation expressions in a table chart, we can first create the [Dates with Monthly Max] field by using the following expression:
=Concat(
Aggr(
Only(
{<[Connection Date] = {"=Aggr(Max(total <[Connection Month]> Aggr(Count([Connection ID]), [Connection Date])) = Sum(Aggr(Count([Connection ID]), [Connection Date])), [Connection Month], [Connection Date])"}>}
[Connection Date]
)
, [Connection Date]
), ', ')
Let's break that down:
{<[Connection Date] = {"=Aggr(Max(total <[Connection Month]> Aggr(Count([Connection ID]), [Connection Date])) = Sum(Aggr(Count([Connection ID]), [Connection Date])), [Connection Month], [Connection Date])"}>}
Above is the set expression that we use to select only the [Connection Dates] where the count of [Connection ID] was the Max() for that [Connection Month].
=Concat(
...
, ', ')
The above Concat() function will make a comma list of dates if multiple dates each have the Max() count of [Connection ID] for that [Connection Month].
Finally, we use the following expression to create the [Monthly Connection Max] field:
=Max(total <[Connection Month]> Aggr(Count([Connection ID]), [Connection Date]))

Related

Using IIF Statement with DateDiff, True Is Not Working

(SELECT IIF([Date Retired/Left] = NULL, DateDiff("yyyy", [Date Employed],Now()),
DateDiff("yyyy",[Date Employed],[Date Retired/Left]))
FROM [Employment History]
WHERE [Employment History].EmployeeID = [Training History].EmployeeID)
AS [Time Employed]
This is a subquery that returns the correct value if [Date Retired/Left] holds a date however when it does not hold a date i.e. has not left or retired I want to collect the length of employment to now. I've attempted to use the Now () command but cannot get it to work, receiving blanks in these fields.
Apologies if it is a simple error, I am a fresh novice trying SQL for the first time this week.
Dates
It's been forever since the last time I've worked with ms-access, but I think the problem is this part: [Date Retired/Left] = NULL.
In every database I know, you can't compare values to NULL - the result will always be either false or unknown. You need to change that to [Date Retired/Left] IS NULL.
So your sub query should be this:
(SELECT IIF([Date Retired/Left] IS NULL,
DateDiff("yyyy", [Date Employed], Now()),
DateDiff("yyyy", [Date Employed], [Date Retired/Left])
)
FROM [Employment History]
WHERE [Employment History].EmployeeID = [Training History].EmployeeID)
AS [Time Employed]

How to group totals

I am trying to group by category - But the SQL I am using is grouping them all by date.
Example:
If a user selects a date range of 01/04/17 - 20/04/17 it will show them total number found under category A
Whats actually happening:
The results are showing Category A 10 times and showing me the total number of each date rather than a complete grouped total
Hope this makes sense
SELECT tbl_ComplaintsCoded.CauseManager, Count(tbl_ComplaintsCoded.CauseManager) AS CountOfCauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
FROM tbl_ComplaintsCoded
GROUP BY tbl_ComplaintsCoded.CauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
HAVING (((tbl_ComplaintsCoded.TouchCSM)=[Forms]![frm_Central_Reporting]![Combo209]) AND ((tbl_ComplaintsCoded.[Mail Date]) Between [Forms]![frm_Central_Reporting]![Text204] And [Forms]![frm_Central_Reporting]![Text206]));
this should get what you want:
SELECT tbl_ComplaintsCoded.CauseManager, Count(tbl_ComplaintsCoded.CauseManager) AS CountOfCauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
FROM tbl_ComplaintsCoded
WHERE tbl_ComplaintsCoded.TouchCSM = [Forms]![frm_Central_Reporting]![Combo209]
AND (tbl_ComplaintsCoded.[Mail Date] BETWEEN [Forms]![frm_Central_Reporting]![Text204] AND [Forms]![frm_Central_Reporting]![Text206])
GROUP BY tbl_ComplaintsCoded.CauseManager
HAVING CountOfCauseManager > 0;
Also, please read: WHERE vs HAVING

Date Range in an Access Crosstab

Trying to get a Crosstab to bring up a prompt when a query is opened to allow a date range (start and end) to be input (dd-mm-yyyy) so that only this data comes back when the query is ran.
Currently sitting on the following code;
TRANSFORM Count(AlphaData.[Invoice]) AS CountOfInvoice
SELECT AlphaData.[Reason], Count(AlphaData.[Invoice]) AS [Total Of Invoice]
FROM AlphaData
WHERE ((AlphaData.[DateRaised]) Between AlphaData.[DateRaised] And AlphaData.[DateRaised])
GROUP BY AlphaData.[Reason]
PIVOT Format([DateRaised],"Short Date");
But cannot for the life of me get around the "MS Access DB engine does not recognise 'Alphadata.[DateRaised:]' as a valid field name or expression" issue.
The "WHERE" portion of the query does work in other queries, but it just goes to pot when it's applied in a crosstab.
Any suggestions?
It seems a bit mixed up. How about:
PARAMETERS
[From Date:] DateTime,
[To Date:] DateTime;
TRANSFORM
Count(*) AS CountOfInvoice
SELECT
AlphaData.[Reason],
Sum(AlphaData.[Invoice]) AS [Total Of Invoice]
FROM
AlphaData
WHERE
AlphaData.[DateRaised] Between [From Date:] And [To Date:]
GROUP BY
AlphaData.[Reason]
PIVOT
Format([DateRaised],"Short Date");
You need to add the parameters to the query:
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
TRANSFORM Count(AlphaData.Invoice) AS CountOfInvoice
SELECT AlphaData.Reason, Count(AlphaData.Invoice) AS [Total Of Invoice]
FROM AlphaData
WHERE (((AlphaData.DateRaised) Between [Start Date] And [End Date]))
GROUP BY AlphaData.Reason
PIVOT Format([DateRaised],"Short Date");
(note: parameter added as the first line, and then used in the Between statement).
If you're using the graphical interface you need to look for the Parameter option:
and enter your parameters in the dialog box:
I didn't realise this would happen with a cross-tab as you can just type the parameter in for a select query:
SELECT Invoice, Reason, DateRaised
FROM AlphaData
WHERE DateRaised=[Start Date]

SQL between 2 dates not returning correct results

I am using the query below and I would like to get the rows where "[Last Update Date]" is in either 2015 or 2016.
SELECT [NPI]
,[Entity Type Code]
,[Replacement NPI]
,[Employer Identification Number (EIN)]
,[Provider Organization Name (Legal Business Name)]
,[Provider Last Name (Legal Name)]
,[Provider First Name]
,[Provider Middle Name]
,[Provider Name Prefix Text]
,[Provider Name Suffix Text]
,[Provider Credential Text]
,[Provider Enumeration Date]
,[Last Update Date]
,[Healthcare Provider Taxonomy Code_1]
FROM [Database].[dbo].[NPIInfo]
WHERE[Healthcare Provider Taxonomy Code_1] in ('122300000X', '1223G0001X','1223P0221X','1223P0700X')
AND ([Last Update Date] BETWEEN '2015/01/01' AND '2016/12/31' )
order by [Last Update Date]
I don't receive any results when I run the code above. But if I run it with this "AND" statement I get results:
AND [Last Update Date] like '%2015%' or [Last Update Date] like '%2016%'
This result gives me 500,000 + rows.
If I run it this way per year I get way less results:
AND [Last Update Date] like '%2015%'
Any idea what I am doing wrong here? The last 2 And statements are giving me different results TIA
TRY
WHERE CONVERT(DATETIME, [Last Update Date] , 103) BETWEEN '2015/01/01' AND '2016/12/31'
Check this page for the different formats
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
EDIT: Apparently I'm wrong here. Leaving the answer for reference as to what is not the problem. However, the CAST code might work anyway.
First, you can't use BETWEEN with text data, you have to do it with a datetime or numeric format. If [Last update Date] is text you'll need to convert it to datetime before you can do anything with it. It would be best to change your actual data's format, but if you can't for some reason this may work:
AND (CAST([Last Update Date] as datetime)
BETWEEN CAST('2015/01/01' as datetime) AND CAST('2016/12/31' as datetime))
Regarding your like '%2015%' statements, the wildcard condition you're using is specifically for text data; that's why it works at all. The different row amounts are because the first like is looking for both 2015 and 2016, while the second one is looking for only 2015.

Looking for SQL count performance improvements.

I'm refactoring some older SQL, which is struggling after 4 years and 1.7m rows of data. Is there a way to improve the following MS SQL Query:
SELECT ServiceGetDayRange_1.[Display Start Date],
SUM (CASE WHEN Calls.line_date BETWEEN [Start Date] AND [End Date] THEN 1 ELSE 0 END) AS PerDayCount
FROM dbo.ServiceGetDayRange(GETUTCDATE(), 30, #standardBias, #daylightBias, #DST_startMonth, #DST_endMonth, #DST_startWeek, #DST_endWeek, #DST_startHour, #DST_endHour, #DST_startDayNumber, #DST_endDayNumber) AS ServiceGetDayRange_1 CROSS JOIN
(select [line_date] from dbo.l_log where dbo.l_log.line_date > dateadd(day,-31,GETUTCDATE())) as Calls
GROUP BY ServiceGetDayRange_1.[Display Start Date], ServiceGetDayRange_1.[Display End Date]
ORDER BY [Display Start Date]
It counts log entries over the previous 30 days (ServiceGetDayRange function returns table detailing ranges, TZ aligned) for plotting on a chart.. useless information, but i'm not the client.
The execution plan states 99% of the exec time is used in counting the entries.. as you would expect. Very little overhead in working out the TZ offsets (remember max 30 rows).
Stupidly i thought 'ah, indexed view' but then realised i cant bind to a function.
Current exec time if 6.25 seconds. Any improvement on that +rep
Thanks in advance.
Is it faster if you turn the CASE into a WHERE?
SELECT ServiceGetDayRange_1.[Display Start Date], COUNT(*) AS PerDayCount
FROM dbo.ServiceGetDayRange(GETUTCDATE(), 30, #standardBias, #daylightBias, #DST_startMonth, #DST_endMonth, #DST_startWeek, #DST_endWeek, #DST_startHour, #DST_endHour, #DST_startDayNumber, #DST_endDayNumber) AS ServiceGetDayRange_1 CROSS JOIN
(select [line_date] from dbo.l_log where dbo.l_log.line_date > dateadd(day,-31,GETUTCDATE())) as Calls
WHERE Calls.line_date BETWEEN [Start Date] AND [End Date]
GROUP BY ServiceGetDayRange_1.[Display Start Date], ServiceGetDayRange_1.[Display End Date]
ORDER BY [Display Start Date]
6.25 seconds for nearly 2m rows is pretty good.. maybe try a count of valid rows (your 1/0 conditional should allow that) as opposed to a sum of values.. I think that's more efficient in oracle environments.