I am trying to get the average of the minimum dates in a qlikview set analysis.
There is a set of data with an Initial Contact date, which is then followed by many Meeting Dates.
What I wanted to know was the average days from the InitialContact to the Min(MeetingDate) for all customers.
Ive had a look around the site and havnt seen anything comparable, I have also tried the online automatic set expression creator tool, however it doesnt have an example that works in this way.
All help is appreciated.
Cheers
Gary
I don't think a Set Analysis is what you are after. An aggr() should do what you want. I made this pretend data based on what you said
LOAD *,date(num(date#(IC,'YYYY/MM/DD'))) as InitialContact INLINE [
CustomerNo, IC
1, 2016/08/28
2, 2016/08/29
3, 2016/08/30
];
LOAD *,date(num(date#(MD,'YYYY/MM/DD'))) as MeetingDate INLINE [
CustomerNo, MD
1, 2016/08/30
1, 2016/08/31
2, 2016/08/31
1, 2016/09/01
2, 2016/09/01
1, 2016/09/02
2, 2016/09/02
3, 2016/09/02
1, 2016/09/03
2, 2016/09/03
3, 2016/09/03
2, 2016/09/04
3, 2016/09/04
3, 2016/09/05
3, 2016/09/06
];
And then I can use this expression
=avg(aggr(min(MeetingDate)-min(InitialContact),CustomerNo))
to get the average time from Initial Contact to First Meeting
Related
My requirement is I have to sort a row Month Wise, that is Jan, Feb, Mar etc right now it is not sorted.
I have tried writing query under query window order by case when...
I have tried writing query in expression of month field i.e.
IIF(Fields!Month_Y.Value = "Feb-19", 2,
IIF(Fields!Month_Y.Value = "Mar-19", 3,
IIF(Fields!Month_Y.Value = "Apr-19", 4,
IIF(Fields!Month_Y.Value = "May-19", 5,
IIF(Fields!Month_Y.Value = "Jun-19", 6,
IIF(Fields!Month_Y.Value = "Jul-19", 7,
IIF(Fields!Month_Y.Value = "Aug-19", 8,
IIF(Fields!Month_Y.Value = "Sep-19", 9,
IIF(Fields!Month_Y.Value = "Oct-19", 10,
IIF(Fields!Month_Y.Value = "Nov-19", 11, 12)))))))))))
I have wrote same IIF condition query in Tablix - Sorting expression field too.
But still it is not sorting the report month wise. if anyone can have a look and give the solution please.
Thank you in advance.
I understand from your question that you tried to use "case" on the "data set"
I have tried writing query under query window order by case when...
So the next thing that you need to do is:
Right-click on the column.
Go to "Shorting".
Press "Add".
Choose your "number" column(that you mapped - "Feb-19" etc.).
Choose your order method "Z-A" or "A-Z".
I simulated your case and it worked for me.
See the image below
So as the title implies, I have the same exact structure for two queries but I just need to change the grouping. I know I can do a union, however, to narrow the script, is there a way to associate both grouping in one query? For example:
SELECT shtuff FROM report_tickets WHERE something = something GROUP BY companies
//Query two
SELECT shtuff FROM report_tickets WHERE something = something GROUP BY ticket_id
Can I do?:
SELECT * FROM report_tickets WHERE something = something GROUP BY companies AND ticket_id
I know this doesn't work but in concept. Have the query take in account both groupings? ^^
I would need to add the following to line 30 from the linked code below, this may determine if it can work or not.
SUM(IF(ROUND((SELECT difference)) < 1, 1, 0)) AS zero_to_one,
SUM(IF(ROUND((SELECT difference)) BETWEEN 1 AND 8, 1, 0)) AS one_to_eight,
SUM(IF(ROUND((SELECT difference)) BETWEEN 8 AND 24, 1, 0)) AS eight_to_twentyfour,
SUM(IF(ROUND((SELECT difference)) > 24, 1, 0)) AS over_twentyfour,
I am trying to extract data in a tabular format with different values for part-time and full-time, to get an output like this:
but my query generates this error:
syntax error missing operator
What operator is missing from this query?
SELECT
BANINST1_SZVIRST.SZVIRST_TERM_DESC,
Count(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F") AS ["Full-time Undergraduate Enrollment"],
Count(IIf(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="P",1,Null)) AS ["Part-time Undergraduate Enrollment"]
FROM BANINST1_SZVIRST
GROUP BY
BANINST1_SZVIRST.SZVIRST_TERM_DESC,
BANINST1_SZVIRST.SZVIRST_TERM_CODE,
BANINST1_SZVIRST.SZVIRST_LEVL_CODE,
BANINST1_SZVIRST.SZVIRST_FULL_PART_IND
HAVING
(((BANINST1_SZVIRST.SZVIRST_TERM_CODE) In ("201020","201120","201220","201320","201420","201520","201620"))
AND ((BANINST1_SZVIRST.SZVIRST_LEVL_CODE)="01"));
I think you have 4 different conditions happening here that you are testing for:
Counts of people that are Full-Time
Counts of people that are Part-Time
Counts of people that are Graduate
Counts of people that are UnderGraduate
You are on the right track to use the iif() statement. I think that was from a previous question you asked a day or two ago. The iif() statements would be:
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F", 1, 0)) as [FullTime],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="P", 1, 0)) as [PartTime],
SUM(IIF(SZVIRST_LEVL_CODE="01", 1, 0)) as [Graduate],
SUM(IIF(SZVIRST_LEVL_CODE="02", 1, 0)) as [UnderGraduate],
I changed the COUNT() to a SUM() just because it feels more natural since the IIF() is returning a 1 or a 0. You could change that back to a COUNT(Iif(<condition>, 1, null)) and it would fine too.
Checking for two conditions
If you want to combine a couple of conditions to get a count, the formula changes slightly.
For insstance, if you wanted a count of all Full-Time People that are also Under Graduates you could use:
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F" AND SZVIRST_LEVL_CODE="02", 1, 0)) AS [FullTimeUnderGraduates]
Here we put BOTH conditions INSIDE the first parameters of the IIF() formula. If both are TRUE, it returns a 1, then all those 1s gets summed up.
Lastly, if you are wanting to count/sum both Graduate and Under Graduates in your query, then you can't exclude them in your HAVING clause. So you'll need to remove the bit that reads:
((BANINST1_SZVIRST.SZVIRST_LEVL_CODE)="01")
Otherwise a test like
iif(BANINST1_SZVIRST.SZVIRST_LEVL_CODE="01", 1, 0)
will ALWAYS return a 1 and
iif(BANINST1_SZVIRST.SZVIRST_LEVL_CODE="02", 1, 0)
will ALWAYS return 0
Example
The following should run in your database and give you a good working example of the logic discussed above:
SELECT
BANINST1_SZVIRST.SZVIRST_TERM_DESC,
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F", 1, 0)) as [FullTime],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="P", 1, 0)) as [PartTime],
SUM(IIF(SZVIRST_LEVL_CODE="01", 1, 0)) as [Graduate],
SUM(IIF(SZVIRST_LEVL_CODE="02", 1, 0)) as [UnderGraduate],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F" AND SZVIRST_LEVL_CODE="02", 1, 0)) AS [FullTimeUnderGraduates],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="P" AND SZVIRST_LEVL_CODE="02", 1, 0)) AS [PartTimeUnderGraduates],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="F" AND SZVIRST_LEVL_CODE="01", 1, 0)) AS [FullTimeGraduates],
SUM(IIF(BANINST1_SZVIRST.SZVIRST_FULL_PART_IND="P" AND SZVIRST_LEVL_CODE="01", 1, 0)) AS [PartTimeGraduates]
FROM BANINST1_SZVIRST
GROUP BY
BANINST1_SZVIRST.SZVIRST_TERM_DESC,
BANINST1_SZVIRST.SZVIRST_TERM_CODE,
BANINST1_SZVIRST.SZVIRST_LEVL_CODE,
BANINST1_SZVIRST.SZVIRST_FULL_PART_IND
WHERE BANINST1_SZVIRST.SZVIRST_TERM_CODE IN ("201020","201120","201220","201320","201420","201520","201620")
I am running PostgreSQL 9.1.9 x64 with PostGIS 2.0.3 under Windows Server 2008 R2.
I have a table:
CREATE TABLE field_data.trench_samples (
pgid SERIAL NOT NULL,
trench_id TEXT,
sample_id TEXT,
from_m INTEGER
);
With some data in it:
INSERT INTO field_data.trench_samples (
trench_id, sample_id, from_m
)
VALUES
('TR01', '1000001', 0),
('TR01', '1000002', 5),
('TR01', '1000003', 10),
('TR01', '1000004', 15),
('TR02', '1000005', 0),
('TR02', '1000006', 3),
('TR02', '1000007', 9),
('TR02', '1000008', 14);
Now, what I am interested in is finding the difference (distance in metres in this example) between a record's "from_m" and the "next" "from_m" for that trench_id.
So, based on the data above, I'd like to end up with a query that produces the following table:
pgid, trench_id, sample_id, from_m, to_m, interval
1, 'TR01', '1000001', 0, 5, 5
2, 'TR01', '1000002', 5, 10, 5
3, 'TR01', '1000003', 10, 15, 5
4, 'TR01', '1000004', 15, 20, 5
5, 'TR02', '1000005', 0, 3, 3
6, 'TR02', '1000006', 3, 9, 6
7, 'TR02', '1000007', 9, 14, 5
8, 'TR02', '1000008', 14, 19, 5
Now, you are likely saying "wait, how do we infer an interval length for the last sample in each line, since there is no "next" from_m to compare to?"
For the "ends" of lines (sample_id 1000004 and 1000008) I would like to use the identical interval length of the previous two samples.
Of course, I have no idea how to tackle this in my current environment. Your help is very much appreciated.
Here is how you get the difference, using the one previous example at the end (as shown in the data but not explained clearly in the text).
The logic here is repeated application of lead() and lag(). First apply lead() to calculate the interval. Then apply lag() to calculate the interval at the boundary, by using the previous interval.
The rest is basically just arithmetic:
select trench_id, sample_id, from_m,
coalesce(to_m,
from_m + lag(interval) over (partition by trench_id order by sample_id)
) as to_m,
coalesce(interval, lag(interval) over (partition by trench_id order by sample_id))
from (select t.*,
lead(from_m) over (partition by trench_id order by sample_id) as to_m,
(lead(from_m) over (partition by trench_id order by sample_id) -
from_m
) as interval
from field_data.trench_samples t
) t
Here is the SQLFiddle showing it working.
I'm having a heck of a time creating the ICriteria for two tables.
The SQL I am trying to mimic is below.
Is there anyway to do this?
I've been trying CreateAlias, Subqueries, and a bunch of other stuff But I always end up with errors.
I have tried posting this on the nhusers Google group, but not getting much help.
Thanks.
Kim
SELECT *
FROM Echo_File_status efs, Data_DELETION_PARAMETER ddp
WHERE
efs.EFS_PRODUCT_CODE = DDP.DDP_PRODUCT_CODE(+)
AND
DDP.DDP_PROCESS_TYPE = 'D'
AND
( ( trunc(nvl(efs.efs_file_create_date, sysdate)) > sysdate - dp.DDP_DAYS_ON_LINE ) or
( efs.efs_status_code != 'ACKED' ) )
ORDER BY efs.efs_product_code, decode(efs.efs_status_code, 'READY', 1, 'TRANS', 2 , 'FAERR', 3, 'FCERR', 4, 'PRERR', 5, 'TRERR', 6, 'PREP', 7, 'PRCOM', 8, 'FCREA', 9 , 'TRCOM', 10, 'ACKED', 11, 1),
efs.efs_file_create_date DESC
Why use icriteria while Hql would be easy to use? Join the objects on that code property.
My personal opinion on this kind of problem, is that you should just keep writing expressions like that in pure SQL, since it will only be worse to read and maintain using the NHibernate criteria API.
As long as you have test coverage on the query, you can safely hide that implementation detail away.