BigQuery Temp Table Column has no Name - sql

I'm trying to create a temp table in BigQuery, something like:
CREATE TEMP TABLE myTmpTable AS
SELECT t.event_id, MAX(t.event_date)
FROM eventsTable t
WHERE t.field_name = "foo"
AND t.new_string = "bar"
GROUP BY t.event_id;
This results in error "CREATE TABLE columns must be named, but column 2 has no name". I understand that it can't extract a column name from MAX(t.event_date). Is there a way I can specify a column name?

Is there a way I can specify a column name?
Use below
SELECT t.event_id, MAX(t.event_date) AS max_event_date
Meantime the whole SELECT looks wrong to me - if you group by issue_id then event_id should be somehow aggregated. Or you might want to group by event_id instead!

Related

Can I hard code a value into column in the SELECT clause

I want to populate column SURVEY_TYPE_ID with 1
Can I do this like so?
SELECT
SURVEY_ID,
SKILL_ID,
1 AS SURVEY_TYPE_ID,
From Table A
This should work as it is:
SELECT
SURVEY_ID,
SKILL_ID,
1 AS SURVEY_TYPE_ID
From Table A
Also, if you want to populate string in the column value just add quotes.
SELECT
SURVEY_ID,
SKILL_ID,
'id_123' AS SURVEY_TYPE_ID
From Table A
This query returns a result set with a third column whose value is 1:
SELECT SURVEY_ID, SKILL_ID, 1 AS SURVEY_TYPE_ID
FROM Table_A
(This is the same as your query with the spurious comma removed.)
If you want to change the value of the column in the table, then you need UPDATE:
UPDATE Table_A
SET SURVEY_TYPE_ID = 1;
If you want to add a new column to the table, then the syntax varies depending on the database, but generally something like this:
ALTER Table_A ADD COLUMN SURVEY_TYPE_ID INT DEFAULT 1;

What is the "DATA" keyword in T-SQL for?

I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"

How to create temp table in postgresql with values and empty column

I am very new to postgresql. I want to create a temp table containing some values and empty columns. Here is my query but it is not executing, but gives an error at , (comma).
CREATE TEMP TABLE temp1
AS (
SELECT distinct region_name, country_name
from opens
where track_id=42, count int)
What did I do wrong?
How to create a temp table with some columns that has values using select query and other columns as empty?
Just select a NULL value:
CREATE TEMP TABLE temp1
AS
SELECT distinct region_name, country_name, null::integer as "count"
from opens
where track_id=42;
The cast to an integer (null::integer) is necessary, otherwise Postgres wouldn't know what data type to use for the additional column. If you want to supply a different value you can of course use e.g. 42 as "count" instead
Note that count is a reserved keyword, so you have to use double quotes if you want to use it as an identifier. It would however be better to find a different name.
There is also no need to put the SELECT statement for an CREATE TABLE AS SELECT between parentheses.
Your error comes form your statement near the clause WHERE.
This should work :
CREATE TEMP TABLE temp1 AS
(SELECT distinct region_name,
country_name,
0 as count
FROM opens
WHERE track_id=42)
Try This.
CREATE TEMP TABLE temp1 AS
(SELECT distinct region_name,
country_name,
cast( '0' as integer) as count
FROM opens
WHERE track_id=42);

VIEW Duplicate column name 'ISLAND'

CREATE VIEW ALL_TABLES AS SELECT * FROM employee_view, av_pay;
I keep getting error message how do I overcome this
VIEW Duplicate column name 'ISLAND'
av_pay:
employee_view:
You are doing a select *, which will output the same column names as defined in the tables you are querying. As you have both columns defined with the same name in both, there you have the error.
So either rename one of the columns or change the query to something like:
select employee_view.ISLAND ISLAND_V, av_pay.ISLAND ISLAND_P, ... FROM ...
The db engine complaints because your select clause is "*" and both the source tables contain the column "island". As a result, the dbms does not know which column should be returned - from employee_view or av_pay?
BTW, a select from 2 tables without a join will result in a cartesian product...

SQL Query to match if data is not present in another table

I have two tables named tblStockManagement and tblFolding in my database. i have column Name in tblFolding table and column FoldingID as Foreign Key in tblStockManagement table. now i have comboBox in my Winform and i want Names of Items in combobox from tblFolding Table but only those items that are not in tblStockManagement Table.
(because i dont want to select data again if it is already in tblStockManagement table . instead i will update the quantity later).
these are the screenshots of both of tables. please tell me how can i do that
NOT EXISTS version:
select *
from tblFolding f
where not exists (select * from tblStockManagement SM
where sm.FoldingID = f.FoldingID)
NOT EXISTS is "NULL safe", which NOT IN isn't.
This is you need.Basically a sub query which gets all folding id and using not in operator I exclude those matching sets.
SELECT Name
FROM tblFolding
WHERE FoldingID NOT IN (
SELECT FoldingID
FROM tblStockManagement
)
;
You can use SQL NOT condition
Select Name
From tblFolding
Where FoldingId Not In (Select FoldingId From tblStockManagement)
Order By Name