Related
On my spreadsheet, I have an "Organisation List" tab, "Schedule" tab and an increasing number of tabs per organisation, such as "BlueEdge Gym".
I have a QUERY formula that lists all dates from the different organisation's tabs schedules into one list on the "Schedule" tab. This makes a master schedule for me to view all organisations in chronological order, and I can see the next job.
Unfortunately, I have to manually add each organisations tab to this query in order to include them in the results.
I would like this to be dynamic from the organisation list, so that any organisation I add to my organisation tab list, will automatically/ dynamically be included into the list of sheets that the query on my schedule tab lists.
I would also like to have the Organisation name listed next to the date to which it corresponds, on the master schedule.
Here's the sheet:
https://docs.google.com/spreadsheets/d/1W3LSAjrshz3Mil6dsYVzKmR4fuL0Nojptl778ppTaEY/edit?usp=sharing
in A2 try:
=QUERY(SHEETNAMES(), "where not Col1 matches 'Schedule|Staff List|Organisation List'", 0)
and in E2:
=SORT(QUERY({
IFERROR(INDIRECT(IF(A2="", 0, A2)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A3="", 0, A3)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A4="", 0, A4)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A5="", 0, A5)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A6="", 0, A5)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A7="", 0, A7)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A8="", 0, A8)&"!D:F"), {"","",""});
IFERROR(INDIRECT(IF(A9="", 0, A9)&"!D:F"), {"","",""})},
"where Col3=FALSE", 0), 1, 1)
or try this more automated solution...
add script:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Schedule");
var src = sheet.getRange("A2"); // The cell which holds the formula
var str = src.getValue();
var cell = sheet.getRange("E2"); // The cell where I want the results to be
cell.setFormula(str);
}
and use this formula in A2:
=INDEX("=SORT(QUERY({"&JOIN(";", "'"&QUERY(SHEETNAMES(),
"where not Col1 matches 'Schedule|Staff List|Organisation List'", 0)&"'!D:F")&
"}, ""where Col3 = FALSE"", 0), 1, 1)")
In this code snippet
void main() {
List<int> myList = List.generate(2, (_) => 0, growable: true);
myList.addAll(List<int>.generate(1, (_) => 0));
myList.insert(3, 11);
print(myList);
}
I generate a list with two elements. Then I add a list with one element to it. So in total the original list should have 3 elements. So why does an insert at index 3 (fourth element) work? I expect an out of range error as is the case with myList.insert(4, 11).
Debug Code:
List<int> myList = List.generate(2, (_) => null, growable: true);
print("the list before adding temp list: ${myList}");
myList.addAll(List<int>.generate(1, (_) => null, growable: true));
print("the list after adding temp list: ${myList}");
myList.insert(3, 11);
print("the list after inserting an element at index 3 of combined list: ${myList}");
Debug Output:
the list before adding temp list: [null, null]
the list after adding temp list: [null, null, null]
the list after inserting an element at index 3 of combined list: [null, null, null, 11]
That's just how insert works.
The index you provide is not an element position, but the position of a gap between elements - or before the first element or after the last.
So, a list of length 3 has four gaps, the |s of: |1|2|3|. These have positions 0 through 3, and you can insert an element in any of them.
Move your mouse to List.generate you will see the following:
The created list is fixed-length if [growable] is set to false.
As you are passing true value in growable so It will increase the length of array if you will pass value False then the behavior will be changed
growable: true, it will not throw error untill you pass invalid index for example if you write myList.insert(5, 11); without writting myList.insert(4, 11); then myList.insert(5, 11); will throw error
List<int> myList = List.generate(2, (_) => 0, growable: true);
myList.addAll(List<int>.generate(1, (index) => 0));
myList.insert(3, 11);
print(myList);
growable: false It will throw error on both addAll and insert
List<int> myList = List.generate(2, (_) => 0, growable: false);
myList.addAll(List<int>.generate(1, (index) => 0));
myList.insert(3, 11);
print(myList);
Run the above different code you see the different behaviour.
Is it possible to initialize a list on one line in Dart? Something like the following...
List<int> options = new List<int>{ 1,2,5,9 };
(this is possible in c# and is called a collection initializer)
Yes:
List<int> options = [1, 2, 5, 9];
I'd recommend reading:
https://api.dartlang.org/stable/1.24.3/dart-core/List-class.html
Yes, you can do it using the List.unmodifiable constructor:
var options = new List.unmodifiable([3,6,7,8]);
Or by using the List.from constructor:
var options = new List.from([3,6,7,8]);
Or just like this:
var options = [5,7,9,0];
There are also available List.filled and List.generate factory constructors:
List<int?> s = List.filled(5, 10, growable: true); // [10, 10, 10, 10, 10]
This creates list of length 5, of type int or null, and initializes each element with 10. This list is growable, which means its length can be changed with a setter:
s.length = 10;
s[8] = 2; // [10, 10, 10, 10, 10, null, null, null, 2, null]
After changing the list length, new elements will be initialized with null. If the list element type is not-nullable this will cause Exception.
List.generate generates a list of values.
var n = List.generate(5, (index) => 0); // [0, 0, 0, 0, 0]
The created list is fixed-length, and each element is set to 0.
List<int?> n = List.generate(5, (index) => index * index, growable: true); // // [0, 1, 4, 9, 16]
If we want to create growable list (i.e. we set growable to true) we need to explicitly choose non-nullable type eg. int? as we did here, otherwise increasing list length will raise exception. This stands for both List.generate and List.filled factories.
Good reads about those are:
https://api.dart.dev/stable/1.24.3/dart-core/List/List.generate.html
and
https://api.dart.dev/stable/1.24.3/dart-core/List/List.filled.html
var vals = <int>[1, 2, 3];
var vals2 = List<int>()..addAll([1, 2, 3]);
var vals3 = List<int>.of([1, 2, 3]);
Note that when we don't provide a type, we in fact create a list of a
dynamic type. Also, the new keyword is optional.
Square brackets define a List
var listOfInt = [1,2,3]
Curly brackets define a Set
var setOfInt = {1,2,3};
Curly brackets with colons define a Map
var mapOfIntString = {1: "a", 2: "b"};
It is possible to specify the type explicitly.
var list = <int>[1,2,3]
var setOfInt = <int>{1,2,3};`
var map = <int,String>{1: "a", 2: "b"};
Initialize empty list
List<int> options = [];
Initialize filled list
List<int> options = [1,2,5,9];
For monitoring and investigation purpose I would like to have the result from sp_WhoIsActive (especially the query plan which is xml column) to be stored into table, due to restriction I would have to store the result on another server.
When trying this using link server the error pop:
Xml data type is not supported in distributed queries. Remote object 'IROWSET' has xml column(s)
How can this be achieved?
First you need to get the schema for your parameters as given below:
DECLARE #createTableSchema VARCHAR(MAX)
EXEC sp_WhoIsActive
#filter = '',
#filter_type = 'session',
#not_filter = '',
#not_filter_type = 'session',
#show_own_spid = 0,
#show_system_spids = 0,
#show_sleeping_spids = 0,
#get_full_inner_text = 0,
#get_plans = 1,
#get_outer_command = 0,
#get_transaction_info = 1,
#get_task_info = 1,
#get_locks = 0,
#get_avg_time = 0,
#get_additional_info = 1,
#find_block_leaders = 0,
#delta_interval = 0,
#output_column_list = '[dd%][session_id][sql_text][sql_command][login_name][wait_info][tasks][tran_log%][cpu%][temp%][block%][reads%][writes%][context%][physical%][query_plan][locks][%]',
#sort_order = '[start_time] ASC',
#format_output = 1,
#destination_table = '',
#return_schema = 1,
#schema = #createTableSchema OUTPUT,
#help = 0;
SELECT #createTableSchema
GO
Once you get the schema, use that to create table as given below:
CREATE TABLE dbo.AuditTableName (
[dd hh:mm:ss.mss] VARCHAR(8000) NULL
,[session_id] SMALLINT NOT NULL
,[sql_text] XML NULL
,[login_name] NVARCHAR(128) NOT NULL
,[wait_info] NVARCHAR(4000) NULL
,[tran_log_writes] NVARCHAR(4000) NULL
,[CPU] VARCHAR(30) NULL
,[tempdb_allocations] VARCHAR(30) NULL
,[tempdb_current] VARCHAR(30) NULL
,[blocking_session_id] SMALLINT NULL
,[reads] VARCHAR(30) NULL
,[writes] VARCHAR(30) NULL
,[physical_reads] VARCHAR(30) NULL
,[query_plan] XML NULL
,[used_memory] VARCHAR(30) NULL
,[status] VARCHAR(30) NOT NULL
,[tran_start_time] DATETIME NULL
,[open_tran_count] VARCHAR(30) NULL
,[percent_complete] VARCHAR(30) NULL
,[host_name] NVARCHAR(128) NULL
,[database_name] NVARCHAR(128) NULL
,[program_name] NVARCHAR(128) NULL
,[additional_info] XML NULL
,[start_time] DATETIME NOT NULL
,[login_time] DATETIME NULL
,[request_id] INT NULL
,[collection_time] DATETIME NOT NULL
)
Now, mention the Audit table name in the sp_whoisactive call. The data from the sp_whoisactive will be stored in the table.
EXEC sp_WhoIsActive
#filter = '',
#filter_type = 'session',
#not_filter = '',
#not_filter_type = 'session',
#show_own_spid = 0,
#show_system_spids = 0,
#show_sleeping_spids = 0,
#get_full_inner_text = 0,
#get_plans = 1,
#get_outer_command = 0,
#get_transaction_info = 1,
#get_task_info = 1,
#get_locks = 0,
#get_avg_time = 0,
#get_additional_info = 1,
#find_block_leaders = 0,
#delta_interval = 0,
#output_column_list = '[dd%][session_id][sql_text][sql_command][login_name][wait_info][tasks][tran_log%][cpu%][temp%][block%][reads%][writes%][context%][physical%][query_plan][locks][%]',
#sort_order = '[start_time] ASC',
#format_output = 1,
#destination_table = 'dbo.AuditTableName',
#return_schema = 0,
#schema = NULL,
#help = 0;
GO
You can also refer to detailed article on this by Adam Mechanic himself. Capturing Output of Whoisactive
It seems like you have a bunch of SQL servers and want to send all results to one central global production audit server. This is the method I've used. This allows you to add additional columns, such as SERVER NAME. Linked Servers simply does not support the XML data type, it must be a minor modified solution.
You have to convert the XML column into a VARCHAR(MAX) and store the Query Plan as a VARCHAR(MAX) on the global Table in your other Server. Do the following steps
Create a <table> with the sp_WhoIsActive schema with the XML column
Run INSERT INTO <table>... EXEC sp_WhoIsActive to populate that table in one statement
Now query the <table> while converting XML to VARCHAR(MAX)
Insert the results of that converted XML to VARCHAR into the table in your Linked Server
Doing this in a SQL Agent Job also works too.
Brent Ozar has an color excellent example located here. Look at step one to create a destination table.
However a much more detailed example is located here by Adam Machanic who I believe wrote this great diagnostic proc and released it to the public. Thanks Adam!
Basically you have to use the parameters #return_schema=1 and #schema
as an output variable. Afterwards you do a replace of ''
with a table of your choice. Then execute it as dynamic SQL.
I m working on timesheet module.What my problem is i have table with following fields
work_start_time Datetime
work_end_time Datetime
and other field
now my requirement is i have to calculate total hour submitted till date.
field are save like this format dd/mm/yy 10:00 AM
please anybody help me how to find total hour from above two field and sum total hour using LINQ.
any help would be appreciated.
int totalhours = (from d in db.timesheetWorkingHours
.Where(d=>d.WStartTime.HasValue && d.WEndTime.HasValue)
select (d.WStartTime - d.WEndTime).Hours).FirstOrDefault();
its gives error
'System.Nullable<System.TimeSpan>' does not contain a definition for 'Hours' and no extension method 'Hours' accepting a first argument of type 'System.Nullable<System.TimeSpan>' could be found (are you missing a using directive or an assembly reference?)
Here is the pseudo code. This may help. in your scenario timesheets object you would be getting from the db.
IList<Timesheet> timesheets = new List<Timesheet>();
timesheets.Add(new Timesheet
{
work_start_time = new DateTime(2012, 11, 5, 10, 0, 0),
work_end_time = new DateTime(2012, 11, 5, 19, 0, 0)
});
timesheets.Add(new Timesheet
{
work_start_time = new DateTime(2012, 11, 5, 10, 0, 0),
work_end_time = new DateTime(2012, 11, 5, 19, 0, 0)
});
int totalhours = 0;
timesheets.ToList().ForEach(
p => { TimeSpan ts = p.work_end_time - p.work_start_time; totalhours += ts.Hours; }
);
Console.WriteLine(totalhours.ToString());
Console.ReadKey();