sql

Default values for columns in Big Query Tables

萝らか妹 提交于 2021-02-18 21:09:27
问题 Is there a way to set default values for columns in tables in big query? I would like to set 'false' as a default value for a column of boolean data type. 回答1: A nullable column can (trivially) have a NULL default value, but there is no other notion of default in BigQuery (you either insert a particular value or omit the value and it will have the NULL value). That said, if you want to wrap your raw table in a View, you can map a NULL column value to any default that you like. 回答2: There is

Default values for columns in Big Query Tables

天涯浪子 提交于 2021-02-18 21:09:21
问题 Is there a way to set default values for columns in tables in big query? I would like to set 'false' as a default value for a column of boolean data type. 回答1: A nullable column can (trivially) have a NULL default value, but there is no other notion of default in BigQuery (you either insert a particular value or omit the value and it will have the NULL value). That said, if you want to wrap your raw table in a View, you can map a NULL column value to any default that you like. 回答2: There is

mysql insert after delete fails because of “duplicate entry”

懵懂的女人 提交于 2021-02-18 20:56:19
问题 I have a code with two mysql queries. DELETE FROM my_table WHERE user_id=some_number INSERT INTO my_table (user_id, ... ) VALUES(some_number, ...) The field user_id is unique. In rare cases the insert fails claiming a duplicate entry occurred. My first instinct leads me to to believe the DELETE didn't finish and now the insert is trying to insert and I'm getting a duplicate entry. Is this possible? How can I avoid this? Might there be a different explanation you can think of? Update: The

Calculate loads and avoiding cursors

[亡魂溺海] 提交于 2021-02-18 20:45:36
问题 Given the following table structure, which is a representation of a bus route where passengers get on and off the bus with a door sensor. And, there is a person who sits on that bus with a clipboard holding a spot count. CREATE TABLE BusLoad( ROUTE CHAR(4) NOT NULL, StopNumber INT NOT NULL, ONS INT, OFFS INT, SPOT_CHECK INT) go INSERT BusLoad VALUES('AAAA', 1, 5, 0, null) INSERT BusLoad VALUES('AAAA', 2, 0, 0, null) INSERT BusLoad VALUES('AAAA', 3, 2, 1, null) INSERT BusLoad VALUES('AAAA', 4,

Calculate loads and avoiding cursors

怎甘沉沦 提交于 2021-02-18 20:45:26
问题 Given the following table structure, which is a representation of a bus route where passengers get on and off the bus with a door sensor. And, there is a person who sits on that bus with a clipboard holding a spot count. CREATE TABLE BusLoad( ROUTE CHAR(4) NOT NULL, StopNumber INT NOT NULL, ONS INT, OFFS INT, SPOT_CHECK INT) go INSERT BusLoad VALUES('AAAA', 1, 5, 0, null) INSERT BusLoad VALUES('AAAA', 2, 0, 0, null) INSERT BusLoad VALUES('AAAA', 3, 2, 1, null) INSERT BusLoad VALUES('AAAA', 4,

How to add syntax highlight to SQL line magic, cell magic and custom command in jupyter notebook?

北战南征 提交于 2021-02-18 20:15:11
问题 I was searching for ways to highlight SQL codes in jupyter notebook. I was able to highlight SQL cell magic only, but not line magic and custom settings. Case 1 (works) Highlight cell magic (cell startswith %%sql) Ref: adding syntax highlighting to Jupyter notebook cell magic require(['notebook/js/codecell'], function(codecell) { codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ; Jupyter.notebook.events.one('kernel_ready.Kernel', function(){ Jupyter

SQL Server Combine multiple rows to one row with multiple columns

夙愿已清 提交于 2021-02-18 19:35:33
问题 I have a database in which I have the following rows: ID | Date start | Date end ---------------------------------- a | 01-01-1950 | 30-01-1951 a | 01-01-1948 | 31-12-1949 a | 31-01-1951 | 01-06-2000 b | 01-01-1980 | 01-08-2010 c | 01-01-1990 | 31-12-2017 c | 31-01-1985 | 31-12-1989 What I got Multiple rows per person One start and end date per row In a not chronological order Select query which I want to return the following: ID | Date start 1 | Date end 1 | Date start 2 | Date end 2 | Date

LEFT OUTER JOIN Error creating a subquery on bigquery

ぃ、小莉子 提交于 2021-02-18 19:29:32
问题 I'm trying to eval MAL, WAL and DAU from a event table on my bq... I create a query find DAU and with him find WAU and MAU, but it does not work, i received this error: LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join. It's my query WITH dau AS ( SELECT date, COUNT(DISTINCT(events.device_id)) as DAU_explorer FROM `workspace.event_table` as events GROUP BY 1 ) SELECT date, dau, (SELECT COUNT(DISTINCT(device_id)) FROM `workspace.event

Formatting hierachical queries with lpad function

≡放荡痞女 提交于 2021-02-18 19:18:09
问题 SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS org_chart FROM employees START WITH last_name='King' CONNECT BY PRIOR employee_id=manager_id ; LPAD(char1,n [,char2]) returns char1, left-padded to length n with the sequence of characters in char2. This tells SQL to take the LAST_NAME and left-pad it with the '_' character till the length of the resultant string is equal to the value determined by LENGTH(last_name)+(LEVEL*2)-2. For LEVEL = 1. Hence, (2 * 1) - 2 = 2 - 2 = 0. For

GROUP BY for specific rows

ε祈祈猫儿з 提交于 2021-02-18 18:58:41
问题 I have a sql server result which groups by person. And SUMS's their daily figure and monthly figure. Person Team Daily Figure Month To Date Tony Team 1 53 635 Dan Team 2 47 172 Tom Team 3 17 232 Simon Team 2 16 655 Ben Team 3 17 232 Fred Team 2 16 655 How do i group these results just by team 2? For example i still want the others individual results to show but i want just team 2 to be grouped together. Example result Person Team Daily Figure Month To Date Tony Team 1 53 635 Tom Team 3 17 232