Sorry, I am very new to DbGrids.
Should I use the query's field editor and somehow add a new field that captures the TIMEDIFF and then just add that as a column in my DbGrid?
Or can/should I skip the field editor and somehow declare the TIMEDIFFF as a column?
For this table, I want a DbGrid with 4 columns : start time, end time, duration, description (run_id is the primary key & will not be displayed).
I am stumped as to how to get data into a 'duration' column ...
mysql> describe test_runs;
+------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+----------------+
| run_id | int(11) | NO | PRI | NULL | auto_increment |
| start_time_stamp | timestamp | YES | | NULL | |
| end_time_stamp | timestamp | YES | | NULL | |
| description | varchar(64) | YES | | NULL | |
+------------------+-------------+------+-----+---------+----------------+
4 rows in set (0.37 sec)
[Update]
The query for the datasource is
SELECT start_time_stamp,
end_time_stamp,
TIMEDIFF(end_time_stamp, start_time_stamp) as duration,
description
FROM test_runs ORDER BY start_time_stamp DESC
and when I execute it manually in MySql, I get
mysql> select TIMEDIFF(end_time_stamp, start_time_stamp) as duration FROM
+----------+
| duration |
+----------+
| NULL |
| 00:04:43 |
| 00:00:13 |
| 00:00:06 |
| 00:00:04 |
+----------+
5 rows in set (0.00 sec)
but the corresponding column in the DB grid remains blank. Can anyone help? Thanks.
[Update] I am using AnyDac, if that helps. The query produces all fields, including the time difference, in MySql and also in the Delphi IDE when I use the AnYDac query editor and execute it.
The only problem is that I don't see it in the DB grid at run time. I double click the DB grid at design time and the columns are correct. The FielName property is set to duration, which is retuned by the query shown above. It doesn't exist in the database, but is calculated by the query; could that somehow be the problem??
[Aaaaaaaargh!!!] Someone tried to "improve" my code and set the query's text programatically at run-time (as SELECT * FROM test_runs), thus overwriting my design time query!! Since the databse table does not have a duration field, none was shown in the DB grid.
Words were had, voices were rasied and now I must apolgize for wasting your time. Sorry.
I would create a calculated field in your Query and add that field to your DbGrid.
so, as you say, with the Field Editor open for the query, right-click and select New Field (or press Ctrl-N). Give your new field a name (eg Duration), keep Component name default or rename if you desire. Set the Type as appropriate (DateTime most likely in this case) and set the Field Type to Calculated.
Then, in the OnCalcFields event of your Query, set the value of that field to the value you want. eg:
procedure TForm1.Query1CalcFields(DataSet: TDataSet);
begin
Dataset.FieldByName('description').AsDateTime :=
DataSet.FieldByName('end_time_stamp').AsDateTime -
DataSet.FieldByName('start_time_stamp').AsDateTime;
end;
Or, you could also include the Duration as an extra field in your select query. Unfortunately I don't have ready access to MySQL here, but it could be something like:
select run_id, start_time_stamp, end_time_stamp, description,
(end_time_stamp - start_time_stamp) as duration from test_runs;
Should I use the query's field editor and somehow add a new field that
captures the TIMEDIFF and then just add that as a column in my DbGrid?
Not at first, delete all of the entries from that field editor
Or can/should I skip the field editor and somehow declare the
TIMEDIFFF as a column?
Not at first, delete all of the entries from that column editor
After that you should see all columns from the open dataset inside the grid, because you eliminate all the limitations from column editor and field editor.
As a guess what happens to your grid/data you did not set the FieldName property for column duration and therefor the grid didn't know what to present.
to use a DBgrid you need a datasource,dataset each should be assigned to respective components,and make sure you have made the connection. if you didnt get any thing in the grid you need to check your dataset,if only the date difference column data is missing but header display the field name in the query then probably you may be getting null data.....if even you didn't get the header name too you need to check your query.......
Related
I have a page 100 with a Classic Report table region p100_report. It renders with a query similar to this:
select id, pages_list from (
select id, listagg(col_page, ':') within group (order by col_page) as pages_list
from t1
group by id
order by id
) t1_lag;
So if t1 is a table like this:
|id|col_page |
|--|---------|
| 1| 102|
| 1| 103|
| 1| 500|
| 1| 600|
| 2| 101|
| 2| 102|
| 2| 103|
then t1_lag is a table like this:
|id| pages_list|
|--|---------------|
| 1|102:103:500:600|
| 2| 101:102:103|
Each cell in p100_report table, pages_list column contains a link to the modal page 200. It has Select2 Multi-select item called p200_pages_s2.
List of values for p200_pages_s2:
|pageID|pageName|
|------|--------|
| 100|Main |
| 101|First |
| 102|Second |
| 103|Third |
| 200|Modal |
| 500|Admin |
| 600|Log |
It uses pageID for submits and pageName for Select2 lis display correctly.
I try to send specific page_list values to page 200 with Link > Target > Set Items, assigning \#PAGES_LIST#\ value (for example, \101:102:103\) to the P200_PAGES_S2.
In session state it has required colon separated string value, but GUI part is empty. I tried different Dynamic Actions with Submit, Refresh, even some JS code from here, but nothing seems to work.
The problem: I need to set pre-selected values in ul.select2-selection__rendered, add an interactive working li for each value from the colon separated list. Any advice?
The problem was in an incorrect Dynamic Action that was trying to set value with an SQL SELECT. I've deleted it, and now it works as designed.
I've tried to do it with SQL SELECT because before I was trying to set value via link, but with a comma separated set of values (e.g. 100,101,102), and that didn't work. Select2 understands only colon separated sets (100:101:102).
I have a model Program containing fields program_title, department_id and date. I have inserted two rows having same program title and date but different department_id.
Insert into programs(program_title,date,department_id) Values ("prog1","4/2/2017","1");
Insert into programs(program_title,date,department_id) Values ("prog1","4/2/2017","2");
Now I want to return rows which will be distinct by program_title whatever be the department_id. I have tried,
#event_contents=Program.select(:id,:date,:program_title).distinct(:program_title)
But still it returns both the rows. Any help is appreciated.
SQL can only collapse rows where all values are the same when using DISTINCT. Because you are selecting id, which is different for every record, the rows are not distinct. E.g.:
---------------------------------
| id | program_title | date |
---------------------------------
| 1 | prog1 | 4/2/2017 |
| 2 | prog1 | 4/2/2017 |
---------------------------------
You'll need to exclude the id from your #select for it to work:
Program.select(:date, :program_title).distinct
Try this
Program.all.distinct(:program_title).pluck( :id,:program_title,:date)
It will return data as array of elements though
Hope it helps
I need to create a Rails app that will show/utilize our current CRM system data. The thing is - I could just take Rails and use current DB as backend, but the table names and column names are the exact opposite Rails use.
Table names:
+-------------+----------------+--------------+
| Resource | Expected table | Actual table |
+-------------+----------------+--------------+
| Invoice | invoices | Invoice |
| InvoiceItem | invoice_items | InvItem |
+-------------+----------------+--------------+
Column names:
+-------------+-----------------+---------------+
| Property | Expected column | Actual column |
+-------------+-----------------+---------------+
| ID | id | IniId |
| Invoice ID | invoice_id | IniInvId |
+-------------+-----------------+---------------+
I figured I could use Views to:
Normalize all table names
Normalize all column names
Make it possible to not use column aliases
Make it possible to use scaffolding
But there's a big but:
Doing it on a database level, Rails will probably not be able to build SQL properly
App will probably be read-only, unless I don't use Views and create a different DB instead and sync them eventually
Those disadvantages are probably even worse when you compare it to just plain aliasing.
And so I ask - is Rails able to somehow transparently know the id column is in fact id, but is InvId in the database and vice versa? I'm talking about complete abstraction - simple aliases just don't cut it when using joins etc. as you still need to use the actual DB name.
When I try to join on one of the customDimensions fields, I get a syntax error: "join attributes may be only column entity or equality expressions". I am able to join on non-custom columns such as name or timestamp.
Sample code:
let ExperimentLaunchedEvents = customEvents | where name=="ExperimentLaunched" and timestamp > now(-30d);
let ExperimentTerminatedEvents = customEvents | where name=="ExperimentTerminated" and timestamp > now(-30d);
ExperimentLaunchedEvents
| project name, timestamp, experimentId=customDimensions.ExperimentId
| join kind=leftanti (ExperimentTerminatedEvents
| project name, timestamp, experimentId=customDimensions.ExperimentId) on tostring(experimentId)
If joining on customDimensions columns is not supported, is there any way to achieve selecting launched experiments that haven't been terminated? Thanks!
As mentioned by John in the comments When using custom dimensions for any operations you need to convert it to a type that can be used by the query engine. In this case I use tostring(), but you can also use other functions like toint().
I also extend a column type so it can be reused in clauses like join or where without having to use the long hand over and over again.
ExperimentLaunchedEvents
| extend experimentId=tostring(customDimensions.ExperimentId)
| project name, timestamp, experimentId
| join kind=leftanti (ExperimentTerminatedEvents
| extend experimentId=tostring(customDimensions.ExperimentId)
| project name, timestamp, experimentId)
on experimentId
Same as James Davis answer, but a minor enhancement of stopping the experimentId column repeating twice due to its inclusion in both the project clauses, as pointed out by squallsv
let myExperimentLauncedEvents=
ExperimentLaunchedEvents
| extend experimentId=tostring(customDimensions.ExperimentId)
| project name, timestamp, experimentId
| join kind=leftanti (ExperimentTerminatedEvents
| extend experimentId=tostring(customDimensions.ExperimentId)
| project name, timestamp, experimentId)
on experimentId;
myExperimentLauncedEvents
| project name, timestamp, experimentId
As a result, by assigning the result to a variable (temp table), and then using the project clause on that variable, we can choose to display only those columns that are required.
i need your help to finish my delphi homework.
I use ms access database and show all data in 1 dbgrid using sql. I want to show same column but with criteria (50 record per column)
i want select query to produce output like:
No | Name | No | Name |
1 | A | 51 | AA |
2 | B | 52 | BB |
3~50 | | 53~100| |
Is it possible ?
I can foresee issues if you choose to return a dataset with duplicate column names. To fix this, you must change your query to enforce strictly unique column names, using as. For example...
select A.No as No, A.Name as Name, B.No as No2, B.Name as Name2 from TableA A
join TableB B on B.Something = A.Something
Just as a note, if you're using a TDBGrid, you can customize the column titles. Right-click on the grid control in design-time and select Columns Editor... and a Collection window will appear. When adding a column, link it to a FieldName and then assign a value to Title.Caption. This will also require that you set up all columns. When you don't define any columns here, it automatically returns all columns in the query.
On the other hand, a SQL query may contain duplicate field names in the output, depending on how you structure the query. I know this is possible in SQL Server, but I'm not sure about MS Access. In any case, I recommend always returning a dataset with unique column names and then customizing the DB Grid's column titles. After all, it is also possible to connect to an excel spreadsheet, which can very likely have identical column names. The problem arrives when you try to read from one of those columns for another use.