I created a subquery inside my select statement which aggregates multiple columns into one which then is called [maxBoosterDatum]. Now I need to perform a DateAdd calculation with the result of the calculated value. Can anyone please help/advise me on how I can achieve this?
(SELECT MAX(maxBoosterDatum)
FROM (VALUES (Quests.[Datum Booster Impfung]),
(Quests.[Datum Booster Impfung nach 1]),
(Quests.[Datum Booster Impfung nach 2])) AS DatumB(maxBoosterDatum)) AS maxBoosterDatum,
Thank you in advance!
Related
I am trying to capture the change in the "Number of records" before processing the cube and after processing the cube. I would then want to setup a table in sql server as following.
I was thinking of running an mdx script to count the number of records in a dimension in ssis before and after processing the cube to get the results. Could you please let me know how to count the number of records in a dimension using mdx. Thank you in advance.
Here is the query to count Year members from Date hierarchy:
with member [Count] as
sum(existing [Date].[Calendar Year].MEMBERS
,count(existing [Date].[Calendar Year].CurrentMember))-1
select [Count] on 0
from [Adventure Works]
And proof image:
You can use is to get total number of members or even pre-filtered (by user, query or any tool).
I found this useful for finding the number of records in Cube for each Dimension
SELECT DISTINCT
[CATALOG_NAME] AS [Database],
[CUBE_NAME] AS [Cube],
[DIMENSION_CAPTION] AS [Dimension],
[DIMENSION_CARDINALITY] AS [Records]
FROM $system.MDSchema_Dimensions
I know this problem has been asked a lot but when I address the error message and use a HAVING clause, I am still receiving the dreaded:
An aggregate may not appear in the WHERE clause unless it is in a
subquery contained in a HAVING clause or a select list,
and the column being aggregated is an outer reference.
What am I doing wrong, here?
SELECT
mr.ClubKeyNumber,
COUNT(mr.MonthlyReportID),
SUM(CONVERT(int,mr.Submitted))
FROM MonthlyReport mr
WHERE mr.ReportYear = 2014
AND COUNT(mr.MonthlyReportID) = 12
GROUP BY mr.ClubKeyNumber
HAVING (SUM(CONVERT(int,mr.Submitted))) > 11
The problem isn't with your HAVING clause it's in your WHERE clause.
You have an aggregate count in your where clause, try this:
SELECT
mr.ClubKeyNumber,
COUNT(mr.MonthlyReportID),
SUM(CONVERT(int,mr.Submitted))
FROM MonthlyReport mr
WHERE mr.ReportYear = 2014
GROUP BY mr.ClubKeyNumber
HAVING (SUM(CONVERT(int,mr.Submitted))) > 11 and COUNT(mr.MonthlyReportID) = 12
The where clause checks each row being aggregated before the group by clause. It cannot count your MonthlyReportID until after the group by so move it to the having clause.
Here is a simple example you can play with to demonstrate where vs have.
There are many accountants and each of them has jobs (paid by the hour) and I need to get the accountant name of every accountant who has an average job cost higher than the overall average of job costs. How do I do this?
SELECT Accountant_Name, AVG(job_cost) as 'Average'
FROM job_view
WHERE Average > (SELECT AVG (job_cost) AS AV
FROM job_view)
GROUP BY Accountant_Name;
Everything needed is in a view named the job_view. The above code is not working any help on modifications would be appreciated. Thanks in advance.
This should do it for you:
SELECT Accountant_Name
, AVG(Job_Cost) as 'Average'
FROM Job_View
GROUP BY Accountant_Name
HAVING AVG(Job_Cost) > (SELECT AVG(Job_Cost) FROM Job_View)
As per your comment, the error you're getting at WHERE Average > is because the alias Average is not visible in a WHERE clause and usually requires you to put the entire contents of the column just as you defined it in the SELECT part.
But because in the SELECT part the Average column is a aggregate function, these can only go lower in the HAVING section, because HAVING handles filtering of aggregate conditions.
Why all this? Because there are rules for order of execution of statements in a query, as explained here.
You'll still need to Group by Accountant_Name
SELECT Accountant_Name, AVG(job_cost) as 'Average'
FROM job_view
GROUP BY Accountant_Name
Having AVG(job_cost) > (SELECT AVG (job_cost) FROM job_view);
I am new to MS SQL and I am having difficulty in getting correct results. I currently have a table with headers updateddate(date), addedby(number), operationid(number) and servicereqno(number) that have been selected from three different tables.
The operationid is an event and the servicereqno is a job number. I want to be able to get a time difference taken between events and then once this is established calculate the average.
For example:
opid 512 is at 20:15 - servicereqno 1
opid 535 is at 21:23 - servicereqno 1
My first task will be to determine what the difference in time is, ensuring it is within the same job number.
Many thanks
In order to get the timespan between events, you need to number all operations sequentially (one increments without leftout), and then join that on itself with an offset of 1. You'll get n-1 rows as result with the timespan inbetween.
Something like this:
WITH cteOps AS (
SELECT ROW_NUMBER() OVER (PARTITION BY servicereqno ORDER BY updateddate) seqid, updateddate, servicereqno
FROM yourdatasource
)
SELECT DATEDIFF(millisecond, o1.updateddate, o2.updateddate) updateddatediff, servicereqno
FROM cteOps o1
JOIN cteOps o2 ON o1.seqid=o2.seqid+1 AND o1.servicereqno=o2.servicereqno;
Of course you can perform aggregates on that to get the average or whatever you need.
SQL Server provides a built-in DATEDIFF function, which you can use to find the difference between two dates. Furthermore, SQL Server has a built-in AVG function that you can use to calculate the average of a set of values..
I have one scenario where i want to get a count if date difference between the two dates is
<=14 days.That is in first table i have to filter records where any one value of DATE1 values are <=14 days of DATE2.
For Ex:
q1="SELECT DATE1 FROM DATE1_TABLE";
q2="SELECT DATE2 FROM DATE2_TABLE";
My simple query :
SELECT
COUNT(*)
FROM
DATE1_TABLE WHERE DATEDIFF(DD,DATE1,(SELECT DATE2 FROM DATE2_TABLE))<=14
But i have multiple records in both the tables,but i want to choose any record having
this difference then it will get a count >0.So,it is throwing error subquery returned more
than one record.I want some solutions for this.I am using SQL SERVER 2008
NOTE:I can't use join here.because i wanted results from two different queries.
Thanks in advance.
You can use TOP 1 clause in your query..
SELECT TOP 1 *
FROM DATE1_TABLE
WHERE DATEDIFF(DD,DATE1,(SELECT DATE2 FROM DATE2_TABLE))<=14
You cannot use SELECT which will return multiple values where function expects scalar value... however you can join those tables:
SELECT
COUNT(DISTINCT dt1.*)
FROM DATE1_TABLE dt1 INNER JOIN DATE2_TABLE dt2 ON DATEDIFF(DD,DATE1,DATE2)<=14
This query will join tables on values only when they are within 14 days and count on unique values from DATE1_TABLE. I have no idea if is it performance wise.