Using psql, I would like to format boolean values to display as TRUE and FALSE instead of t and f by default, as I find the latter difficult to distinguish from one another.
I can change how null displays by setting the following in my ~/.psqlrc:
\pset null 'Ø'
How can I do so for boolean values?
While there doesn't appear to be a simple way to modify the psql output behavior like for the null separator, you may achieve this another way, by modifying the output value, casting it to text.
Given a schema like:
CREATE TABLE bools (
name text,
alive boolean
);
INSERT INTO bools VALUES ('Taylor Swift', TRUE);
INSERT INTO bools VALUES ('Elvis Presley', FALSE);
We can then query for the boolean values, case them along the way, or even create a function that handles the casting for us:
postgres=# SELECT name, alive FROM bools;
name | alive
---------------+-------
Taylor Swift | t
Elvis Presley | f
(2 rows)
postgres=# SELECT name, alive::text FROM bools;
name | alive
---------------+-------
Taylor Swift | true
Elvis Presley | false
(2 rows)
postgres=# SELECT name, UPPER(alive::text) AS alive FROM bools;
name | alive
---------------+-------
Taylor Swift | TRUE
Elvis Presley | FALSE
(2 rows)
postgres=# CREATE FUNCTION bool_to_upper(boolean) RETURNS text
postgres-# AS $$ SELECT UPPER(CAST($1 AS text)) $$
postgres-# LANGUAGE SQL;
CREATE FUNCTION
postgres=# SELECT name, bool_to_upper(alive) AS alive FROM bools;
name | alive
---------------+-------
Taylor Swift | TRUE
Elvis Presley | FALSE
(2 rows)
See this SQLfiddle for a working example on Postgresql 9.6.
There aren't any settings which allow you to format boolean output like that, but Marko Tikkaja published a patch a few years ago to support this.
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)
I'm using Ruby on Rails 5. I'm having trouble ordering results returned from this query:
#crypto_currencies = CryptoIndexCurrency
.all
.pluck("crypto_currency_id")
.order(:name)
The name field is a valid member of the table from which I want to order.
cindex=# \d crypto_currencies;
Table "public.crypto_currencies"
Column | Type | Modifiers
--------------------------+-----------------------------+----------------------------------------------------------------
id | integer | not null default nextval('crypto_currencies_id_seq'::regclass)
name | character varying |
symbol | character varying |
latest_market_cap_in_usd | bigint |
latest_total_supply | bigint |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Unfortunately whe I run the above I get the error:
undefined method `order' for #<Array:0x007fa3b2b27bd8>
How do I correct this?
That's right, pluck returns an array. Here is a note from documentation:
Pluck returns an Array of attribute values type-casted to match the plucked column names
where and order and other methods like that return Relation so that you can chain them. pluck should be in the end.
Say you're trying to cook eggs for yourself -- you're trying to scramble an egg carton instead of the egg inside the carton.
name is a field of CryptoCurrency, but .pluck is returning an array of CryptoCurrency objects -- the array has no name field.
You have to get the specific CryptoCurrency array element by either iterating through the array (for example, using .each... do) or accessing an element by index (#crypto_currencies[0]).
For more tutorial information on how to access the elements of an array, either by iteration or by element, please consult the official documentation.
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.
Using SQlite 3, and have a table that looks like the following
PKEY|TS | A |B |C
1 |00:05:00|200|200|200
2 |00:10:00|100|100|100
3 |00:15:00| |25 |
4 |00:20:00| | |
Currently I'm using
select ts, (a+b+c) from tablename WHERE a !='null' AND b !='null' and c !='null';"
which returns
TS | (a+b+c)
00:05:00|600
00:10:00|300
I want my results to look like the following though:
TS | total
00:05:00|600
00:10:00|300
00:15:00|
00:20:00|
So in other words, I always want to return everything from the TS column, but I don't want to return the total unless A,B, and C have values.
I think I might need a union or a join, but I can't seem to find an example where the results from a single column are always returned.
Testing for a != 'null' will never be true. you need to use a is not null.
However, to return a null total if any of the numbers are null, just do this:
select ts, a+b+c
from tablename;
Why does this work? Because in SQL if any part of an expression is null, the whole expression is null. This makes sense when you consider that null means "unknown". Logically, when part of a calculation is unknown, then the result is unknown.
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.......