Can somebody tell me what the meaning of the vertical bar ' | ' is in the following sqlplus query?
select distinct z303_id_1 Barcode, ' | ', substr(z303_name,1,35) Name from z36, z303
many thanks
Looks like someone just wanted a column with | characters in it. That query will return three columns:
Barcode containing the value of z303_id_1
An unnamed column with | in it
Name containing the result of substr(z303_name,1,35)
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 have a table that contains run numbers. This is then linked to a second table that contains the serial numbers of the panels that go through each run. I was wondering is it possible to design a query that will give for each run number the serial numbers.
I would like it in a table like:
Run Number1, First Serial Number for 1, Second Serial Number for 1, etc..
Run Number2, First Serial Number for 2, Second Serial Number for 2, etc..
I can get in in the form:
Run Number1, First Serial Number for 1
Run Number1, Second Serial Number for 1
Run Number2, First Serial Number for 2
Run Number2, Second Serial Number for 2
Is there a way to set this up?
You can use my DJoin function as this will accept SQL as the source, thus you won't need additional saved queries:
' Returns the joined (concatenated) values from a field of records having the same key.
' The joined values are stored in a collection which speeds up browsing a query or form
' as all joined values will be retrieved once only from the table or query.
' Null values and zero-length strings are ignored.
'
' If no values are found, Null is returned.
'
' The default separator of the joined values is a space.
' Optionally, any other separator can be specified.
'
' Syntax is held close to that of the native domain functions, DLookup, DCount, etc.
'
' Typical usage in a select query using a table (or query) as source:
'
' Select
' KeyField,
' DJoin("[ValueField]", "[Table]", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' The source can also be an SQL Select string:
'
' Select
' KeyField,
' DJoin("[ValueField]", "Select ValueField From SomeTable Order By SomeField", "[KeyField] = " & [KeyField] & "") As Values
' From
' Table
' Group By
' KeyField
'
' To clear the collection (cache), call DJoin with no arguments:
'
' DJoin
'
' Requires:
' CollectValues
'
' 2019-06-24, Cactus Data ApS, Gustav Brock
'
Public Function DJoin( _
Optional ByVal Expression As String, _
Optional ByVal Domain As String, _
Optional ByVal Criteria As String, _
Optional ByVal Delimiter As String = " ") _
As Variant
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotReadKey As Long = 5
' SQL.
Const SqlMask As String = "Select {0} From {1} {2}"
Const SqlLead As String = "Select "
Const SubMask As String = "({0}) As T"
Const FilterMask As String = "Where {0}"
Static Values As New Collection
Dim Records As DAO.Recordset
Dim Sql As String
Dim SqlSub As String
Dim Filter As String
Dim Result As Variant
On Error GoTo Err_DJoin
If Expression = "" Then
' Erase the collection of keys.
Set Values = Nothing
Result = Null
Else
' Get the values.
' This will fail if the current criteria hasn't been added
' leaving Result empty.
Result = Values.Item(Criteria)
'
If IsEmpty(Result) Then
' The current criteria hasn't been added to the collection.
' Build SQL to lookup values.
If InStr(1, LTrim(Domain), SqlLead, vbTextCompare) = 1 Then
' Domain is an SQL expression.
SqlSub = Replace(SubMask, "{0}", Domain)
Else
' Domain is a table or query name.
SqlSub = Domain
End If
If Trim(Criteria) <> "" Then
' Build Where clause.
Filter = Replace(FilterMask, "{0}", Criteria)
End If
' Build final SQL.
Sql = Replace(Replace(Replace(SqlMask, "{0}", Expression), "{1}", SqlSub), "{2}", Filter)
' Look up the values to join.
Set Records = CurrentDb.OpenRecordset(Sql, dbOpenSnapshot)
CollectValues Records, Delimiter, Result
' Add the key and its joined values to the collection.
Values.Add Result, Criteria
End If
End If
' Return the joined values (or Null if none was found).
DJoin = Result
Exit_DJoin:
Exit Function
Err_DJoin:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotReadKey
' Key is not present, thus cannot be read.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_DJoin
End Select
End Function
' To be called from DJoin.
'
' Joins the content of the first field of a recordset to one string
' with a space as delimiter or an optional delimiter, returned by
' reference in parameter Result.
'
' 2019-06-11, Cactus Data ApS, Gustav Brock
'
Private Sub CollectValues( _
ByRef Records As DAO.Recordset, _
ByVal Delimiter As String, _
ByRef Result As Variant)
Dim SubRecords As DAO.Recordset
Dim Value As Variant
If Records.RecordCount > 0 Then
While Not Records.EOF
Value = Records.Fields(0).Value
If Records.Fields(0).IsComplex Then
' Multi-value field (or attachment field).
Set SubRecords = Records.Fields(0).Value
CollectValues SubRecords, Delimiter, Result
ElseIf Nz(Value) = "" Then
' Ignore Null values and zero-length strings.
ElseIf IsEmpty(Result) Then
' First value found.
Result = Value
Else
' Join subsequent values.
Result = Result & Delimiter & Value
End If
Records.MoveNext
Wend
Else
' No records found with the current criteria.
Result = Null
End If
Records.Close
End Sub
Full documentation can be found in my article:
Join (concat) values from one field from a table or query
If you don't have an account, browse to the link: Read the full article.
Code is also on GitHub: VBA.DJoin
I am trying to put together a QUERY that I need to reference a static cell within the SELECT clause to input the cell's value as column value.
I've browsed around Stack Overflow and could not find that specific issue being referred to.
I need a workaround for:
= QUERY(Data!A1:R,
"SELECT A, Sheet2!B1, R
WHERE A is not null
LABEL Sheet2!A1 'Batch ID',1)
And it should be resulting in:
+--------------------------+
| QUERY output |
+--------------------------+
| Name | Batch ID | Status |
+------+----------+--------+
| Alan | 7632r | Sent |
+------+----------+--------+
| Joe | 7632r | Sent |
+------+----------+--------+
And the static reference cell from Sheet2!B1:
+------------------+
| Sheet2 reference |
+------------------+
| Batch ID | 7632r |
+----------+-------+
Apologies for not sharing a dummy sheet, but I have corporate security restrictions, that are not allowing me to share any sheet's link to anyone outside the company.
I am also interested in what downsides are possible if this can be made to work?
pausing query argument with '" appending cell with & then again appending the continue of query argument & and reopening argument with "'
=QUERY(Data!A1:R,
"select A, '"&Sheet2!B1&"', R
where A is not null
label '"&Sheet2!B1&"' 'Batch ID', 1)
I am trying to get sql query output in DBfit using i.e. !|Execute|select * from abc| but don't know how it will display in DBfit.
I think that you are looking for the Inspect Query table (you can find reference docs about it here).
!|Inspect Query|select * from abc|
When executed, this will print the resultset of the query.
First, the execute fixture is typically used for actions that do not return data, e.g.:
!|Execute|insert into tablename values (…)|
or
!|Execute|update tablename st... where...|
However, even some non-data actions have more specific commands. The above update can be done with, for example, with:
!|Update|tablename |
|field_to_change=|field_to_select|
|new value |matching value |
For returning data, use the query fixture
!|query|select Id, BatchNum from tablename|
|Id |BatchNum? |
|1 |>>Bat1 |
|2 |<<Bat1 |
As shown, just put your field names in the row below the fixture, then your data rows below that.
I have this recommendations table:
Table "public.recommendations"
Column | Type | Modifiers
-----------------------+-----------------------------+--------------------------------------------------------------
id | integer | not null default nextval('recommendations_id_seq'::regclass)
comment_on_provider | text |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
search_vector | tsvector |
Indexes:
"recommendations_pkey" PRIMARY KEY, btree (id)
"recommendations_search_vector_idx" gin (search_vector)
Triggers:
recommendations_vector_update BEFORE INSERT OR UPDATE ON recommendations FOR EACH ROW EXECUTE PROCEDURE search_trigger()
and this trigger
CREATE OR REPLACE FUNCTION search_trigger() RETURNS trigger AS $$
DECLARE
search TEXT;
links_title TEXT;
links_description TEXT;
begin
SELECT string_agg(title, ' ') INTO links_title
FROM links
INNER JOIN recommendations
ON new.link_id = links.id;
SELECT string_agg(description, ' ') INTO links_description
FROM links
INNER JOIN recommendations
ON new.link_id = links.id;
search := '';
search := search || ' ' || coalesce(new.comment_on_provider);
search := search || ' ' || links_title;
search := search || ' ' || links_description;
new.search_vector := to_tsvector(search);
return new;
end
$$ LANGUAGE plpgsql;
CREATE TRIGGER recommendations_vector_update
BEFORE INSERT OR UPDATE ON recommendations
FOR EACH ROW EXECUTE PROCEDURE
search_trigger();
When I insert a record to the recommendations table the trigger doesn't fire and search_vector is set to null.
But when I update any record it fires and search_vector gets updated with the expected values.
How to make the trigger working on INSERT?
I suppose in the case of INSERT some of the variables in the expressions assigned to the search variable are NULL. Most probably it's new.comment_on_provider but better check.