Oracle Sql : unable to use 'With' clause when using group by 'Cube' - sql

I have the following sample code as reference:
Create table wait_weekly as select XXX
.....;
Create table wait_weekly_prev as select
....
from
Wait_weekly
group by
cube(var1, var2);
This works fine.
However, if I put the table wait_weekly either in the 'With' clause or directly in a subquery, like
with wait_weekly as (select XXX)
Create table wait_weekly_prev as select
....
from
(select XXX...)
group by
cube(var1, var2);
`
it will give me the same error message and won't recognize a variable is an invalid identifier.. Any suggestion? thanks.
Here is the sample code, but the reality is that it works in this sample code. Just imagine it's not working, and the error message is that 'invalid identifier for one of the variables in table Test". I did remove the cube( lastname, sex) and replaced with nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex, and the codes work; but I do need cube( ..., ...).
Hope this is clear enough, thanks.
create table test (
Lastname VARCHAR2(12),
Sex VARCHAR2(12),
Age NUMBER);
insert all
into test (Lastname, Sex, Age) values ('Sun', 'M', 8)
into test (Lastname, Sex, Age) values ('Thomas','M',12)
into test (Lastname, Sex, Age) values ('Sun','F',13)
into test (Lastname, Sex, Age) values ('Thomas','F',15)
into test (Lastname, Sex, Age) values ('Berg','F',18)
SELECT 1 FROM DUAL;
Example queries:
with test_1 as
(select * from test where lastname <> 'Berg')
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from test_1
group by cube(Lastname, Sex);
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from (select * from test where lastname <> 'Berg')
group by cube(Lastname, Sex);

Related

How is this query write correctly?

I have a query like below. I want to create a table and convert null values to 0. But my query failed. How can I write it correctly ?
CREATE TABLE TABLE_2
AS
SELECT *
FROM TABLE_1 1 = 2;
INSERT INTO TABLE_2 (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER,
HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT, 0), MANAGER_ID, DEPARTMENT_ID)
SELECT *
FROM TABLE_1;
Both your statements contain syntax errors.
The CREATE TABLE ... SELECT AS statement requires a valid query: your query is missing the WHERE keyword.
CREATE TABLE TABLE_2
AS
SELECT *
FROM TABLE_1 where 1 = 2;
Insert statements can have two projections. The optional project is the list of columns in the target table. Your list includes NVL(COMMISSION_PCT, 0) which is not a valid column name, but a declaration of the data manipulation you wish to apply to the column from the source table. Consequently you need to include it in the projection of the SELECT statement.
INSERT INTO TABLE_2
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER,
HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT, 0), MANAGER_ID, DEPARTMENT_ID
FROM TABLE_1;
I have published a working demo on db<>fiddle.
INSERT INTO TABLE_2(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY,COMMISSION_PCT,MANAGER_ID, DEPARTMENT_ID)
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, NVL(COMMISSION_PCT,0), MANAGER_ID, DEPARTMENT_ID
FROM TABLE_1

BigQuery - Using INSERT INTO to copy data from one nested table into another nested table

Helping a customer out. I'm trying to copy one nested BigQuery table into another nested table and am running into the following error: "Syntax error: Expected ")" or "," but got ".""
Query:
INSERT INTO `<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table2` (id,
first_name,
last_name,
dob,
address.status,
address.address,
address.city,
address.state,
address.zip,
address.numberOfYears)
SELECT
id,
first_name,
last_name,
dob,
address.status,
address.address,
address.city,
address.state,
address.zip,
address.numberOfYears
FROM
`<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table`
Answer below. Hope this helps someone else out too!
INSERT INTO
`<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table2`
(id,
first_name,
last_name,
dob,
addresses)
SELECT
id,
first_name,
last_name,
dob,
ARRAY_AGG(STRUCT(a1.status,
a1.address,
a1.city,
a1.state,
a1.zip,
a1.numberOfYears)) AS addresses
FROM
`<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table`,
UNNEST(addresses) AS a1
GROUP BY
id,
first_name,
last_name,
dob

How to hide distinct column in sql selection

I am doing a query in sql to find rows with distinct value of name as below:
select distinct name, age, sex from person
it works but I don't want to show the name column in the result set. Is there a way to hide this column?
EDIT1
the reason I put distinct name there is to avoid multiple rows with the same name returned. My table has person with the same name but different age and sex. So I want to make the result distinct in name but don't show the name.
You could try something like this.
select age, sex from (
select distinct name, age, sex from person);
I'm presuming you might have people with the same age and sex but a different name.
Otherwise just remove the name
Here is my solution (sql server 2016):
create table person (age varchar(20), [name] varchar(20), gender varchar(20))
go
insert into person values ('20', 'joe', 'm')
insert into person values ('19', 'tom', 'm')
insert into person values ('25', 'sally', 'f')
insert into person values ('28', 'Tammy', 'f')
go
select age, gender from (select distinct name, age, gender from person) t
You have to use your query as a sub query here.
From your query again select age and sex alone.
select age, sex from (select distinct name, age, sex from person) As x

Inserting column data from another table together with more value

How to insert few columns from TableA to tableB together with some additional values.
Following is one way I tried and failed, but it shows clearly what I want to achive:
Insert into
TableA (UserID, FirstName, Lastname,EmailAddress,IsActive,IsOnline,IsLockedOut,Comment)
values
(Select distinct UserID, FirstName, LastName, EmailAddress from TableB,0,0,0,'Imported')
You cannot use values when you use select keyword, also you should include constant/static values in your select statement itself, try this
Insert into
TableA
(
UserID, FirstName, Lastname,
EmailAddress,IsActive,IsOnline,
IsLockedOut,Comment
)
Select distinct UserID, FirstName, LastName,
EmailAddress ,
0,0,0,'Imported'
FROM TableB
You need to include the hard code values along with the columns before the FROM part of your query. SO, change your query to this:
Select distinct UserID, FirstName, LastName, EmailAddress, 0, 0, 0, 'Imported'
from TableB

Inserting multiple rows using SQL - issue with manually incrementing numbers

Someone else designed this table and I am not allowed to modify it so bear with me.
I am trying to insert multiple rows from one table into another. The table where I am inserting the rows has an ID but it does not auto-increment. I cannot figure out how to manually increment the id as I insert rows. The current code throws an error:
Error running query. Page15.CaseSerial is invalid in the select list
becauseit is not contained in either an aggregate function or the
GROUP BY clause.
I've tried adding a GROUP BY clause with no success.
Here's the code:
insert into page4 (serial, caseserial, linkserial, type, add1, add2, city, state, orgname, prefername, email, firstname, lastname, salutation, contactstatus, workphone, notes, cellphone, nametype, homephone, fax, zip, payments)
select id = max(serial), caseserial, linkserial, type, add1, add2, city, state,
orgname, prefername, email, firstname, lastname, salutation, contactstatus,
workphone, notes, cellphone, nametype, homephone, fax, zip, payments
from page16
It would be nice if I could write something to get the highest id from page4 and insert the next highest.
Thanks!
declare #maxId int
select #maxId = max(yourIdColumn)
from YourTable
set #maxId = #maxId + 1
insert into YourTable (yourIdColumn, ....)
values (#maxId, ....)
Disclaimer: not sure how this would transpose over to other RDBMS's, but this is with SQL Server in mind. Also, this handles inserting only one value. If you need to insert a set of values, then please let me know.