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
Related
I am pretty new in work with Hive DB and struct data types. I used only basic SELECT statements until now.
I need to join two tables to combine them in my SELECT statement.
Tables have struct datatype with same name, but with different elements inside. This is how tables look like:
TABLE 1
table_one(
eventid string,
new struct<color:string, size:string, weight:string, number:string, price:string>,
date string
)
11 | {"color":"yellow", "size":"xl", "weight":"10", "number":"1111", "price":"1"} | 08-21-2004
12 | {"color":"yellow", "size":"xxl", "weight":"12", "number":"2111", "price":"2"} | 08-21-2004
TABLE 2
table_two(
eventid string,
new struct<number:string, price:string>,
date string,
person string)
11 | {"number":"31", "price":"1"} | 08-21-2004 | john
12 | {"number":"32", "price":"2"} | 08-21-2004 | joe
With SELECT query I need to get value of element 'color' from table_one, but instead that, I am getting value of element 'number' from table_two, query is following:
select
s.eventid,
v.date,
s.new.color,
s.new.size
from table_one s join table_two v where s.eventid = v.eventid;
With s.new.color - instead getting for example value 'yellow' from table_one, I am getting value '31' from table_two. How I am supposed to get wanted value from table_one?
Expected result:
11 | 08-21-2004 | yellow | xl
But I got:
11 | 08-21-2004 | 31 | 1
So how can I select proper value from struct datatype from desired table?
(Please have on mind that this is just simplified example of my problem, I didn't provide exact code or structures of tables to make this clearer for one who will try to provide me answer. I need to use join because I need proper values for some column from table_two)
When working with regular SQL databases, indexes are useful for fetching a few rows, but not so useful when you are fetching a large amount of data from a table. For example, imagine you have a table with stock valuations of 10 stocks over time:
|------+--------+-------+
| time | stock | value |
|------+--------+-------+
| ... | stock1 | ... |
| ... | stock2 | ... |
| ... | ... | ... |
|------+--------+-------+
As far as I can tell, indexing it by stock (even with an enum/int/foreign key) is usually not very useful in a database like Postgres if you want to get data over a large period of time. You end up with an index spanning a large part of the table, and it ends up being faster for the database to do a sequential scan, for example, to get the average value over the whole dataset for each stock:
SELECT stock, avg(value) FROM stock_values GROUP BY stock
Given that QuestDB is row oriented, I would guess that it would result in better performance to have a separated column for each stock.
So, what schema is recommended in QuestDB for a situation like this? One column for each stock, or would a symbol column for each stock symbol be as good (or good enough) even if there are millions of results for each row?
A column per stock is not easy to achieve in QuestDB. If you create table like this
|----------------------------------|
| time | stock1 | stock1 | stock3 |
|----------------------------------|
Then you'll have to insert all values together in one row or you end up with gaps
|----------------------------------|
| time | stock1 | stock1 | stock3 |
|----------------------------------|
| t1 | 1.1 | | |
| t2 | | 3.45 | |
| t3 | | | 103.45 |
|----------------------------------|
Even for t1 == t2 == t3 when you do the insert as 3 operation it will still result in 3 rows.
So symbols are a better choice here.
Symbol can be indexed and not indexed and you may have benefits of non-indexed symbols when distinct number of them is low. Reading full table vs reading by index is the matter of index selectivity, not data range. If the selectivity is high (e.g. distinct symbol count is say 10k) fetching by index is faster than range scans.
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 ChunkRelationship model with a table that looks like this:
+----+---------------+----------------+---------------------+---------------------+
| id | chunk_id | chunk_partner | created_at | updated_at |
+----+---------------+----------------+---------------------+---------------------+
| 1 | 1 | 2 | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 |
| 2 | 2 | 1 | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 |
+----+---------------+----------------+---------------------+---------------------+
Both entries are foreign keys to a Chunk model. Right now, the relationship is being saved twice, once in both directions ( 2 => 1 and 1 => 2). But the relationship can be saved once, because if one ID is known then the other can be found (What is this type of table called?).
I am wondering what the Rails way of doing that would be. I was thinking of creating a before_validation callback on the ChunkRelationship model and taking the smallest number of the two and always saving that to the chunk_id column, which would allow for checking for duplicates easier before saving. But from there I'm not sure how I would retrieve them.
The intended end result would be for chunk.partners to return all the rows paired with it, no matter which column either one is in.
Perhaps you are looking for the has_many_and_belongs_to association: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
This should create a many-to-many relationship which I believe you are describing.
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.......