I am trying to create the tableau_table_1 table from the aggregated data of the covid_deaths table.
Here is my query...
CREATE TABLE tableau_table_1 AS
SELECT
SUM(new_cases) AS total_cases,
SUM(cast(new_deaths as int)) AS total_deaths,
SUM(cast(new_deaths as int))/SUM(New_Cases)*100 AS death_percentage
FROM
covid_data..covid_deaths
WHERE continent IS NOT NULL
-- Group By date order by 1,2
The error that I am showing is...
Incorrect syntax near the keyword 'from'
You could try SELECT INTO Statement
SELECT * INTO tableau_table_1
FROM (
SELECT
SUM(new_cases) AS total_cases,
SUM(cast(new_deaths AS int)) AS total_deaths,
SUM(cast(new_deaths AS int))/SUM(New_Cases)*100 AS death_percentage
FROM
covid_data..covid_deaths
WHERE continent IS NOT NULL) AS aggregated_data;
Related
I write a query in hive. it not working
query:
hive>> select country ,max(total_count) from (select country, count(airlineid) from airport group by country) t2;
it shows expression group by 'country' is missing.
Multiple problems. if you format it you should be able to see it.
select
country, -- you need to add this in group by after t2.
max(total_count) -- you need to create/alias a column called total_count
from
(
select
country,
count(airlineid)
from
airport
group by
country
) t2;
Fixed SQL -
select
country,
max(total_count) max_total_count
from
(
select
country,
count(airlineid)total_count
from
airport
group by
country
) t2
group by country
;
I have a listing of all consumer purchases where some consumers make many purchases over the time frame in scope. I'd like to populate a column with the location of each consumer's first purchase but I'm getting this error:
Error in SQL statement: ParseException:
mismatched input '(' expecting <EOF>(line 2, pos 25)
== SQL ==
SELECT consumer_id
,location OVER(partition BY table.consumer_id) AS first_purchase_site
---------------------^^^
FROM table
For clarity, here is my query:
SELECT consumer_id
,location OVER(partition BY table.consumer_id) AS first_purchase_site
FROM table
WHERE consumer_purchase_order_sequence = 1
I'd like to populate a column with the location of each consumer's first purchase
Are you looking for first_value()?
SELECT consumer_id,
FIRST_VALUE(location) OVER (partition BY table.consumer_id) AS first_purchase_site
FROM table;
Your window function is, errr, missing the function.
You need window function FIRST_VALUE():
SELECT DISTINCT consumer_id,
FIRST_VALUE(location) OVER(PARTITION BY consumer_id ORDER BY consumer_purchase_order_sequence) AS first_purchase_site
FROM table
Change consumer_purchase_order_sequence with the column that orders the purchases.
Its hard to do it with window calculations .You can do it with joins,
SELECT
table.consumer_id,
table.location,
a.first_purchase_site
FROM table LEFT JOIN
(SELECT consumer_id,location AS first_purchase_site FROM table WHERE
consumer_purchase_order_sequence = 1) a ON a.consumer_id=table.consumer_id
Below is my SQL query which currently outputs this result:
SQL query:
SELECT
COUNT(*) AS noofuser,
[DateTime], [Date], [hour],
[Company]
FROM
LMT2_lmutilserverLicenseuser
But, I need to add extra row called 'Total' based on a column 'DateTime' as shown in the below table.
Combining the query with a group by should work, haven't been able to run but this should give the result. You can insert the result of your query in temp table (thus count(*) gets its own column) and then just do another group by on the temp table
SELECT cnt, date_time, date , hr ,company
FROM LMT2_lmutilserverLicenseuserTemp
UNION
SELECT SUM(cnt), date_time, MAX(date) , MIN(hr) , 'tot' AS company
FROM LMT2_lmutilserverLicenseuserTemp
GROUP BY date_time
ORDER BY date_time
I'm attempting to create a persistent table using the WITH clause however, I'm getting an error.
For context the answer that I currently find is
CREATE TABLE my_table
AS
WITH my_tables_data AS (
SELECT another_table.data1 AS some_value
FROM another_table
)
SELECT *
FROM some_data;
However, I am getting an error
Msg 319, Level 15, State 1, Line 5 Incorrect syntax near the keyword
'with'. If this statement is a common table expression, an
xmlnamespaces clause or a change tracking context clause, the previous
statement must be terminated with a semicolon.
My code is
CREATE TABLE SalesOrdersPerYear
WITH t1 AS (
-- Define the CTE expression name and column list.
WITH Sales_CTE (SalesPersonID, BaseSalary)
AS
-- Define the CTE query.
(
SELECT SALES_PERSON.SALES_PERSON_ID, SALES_PERSON.BASE_SALARY
FROM SALES_PERSON
WHERE SALES_PERSON_ID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, BaseSalary AS TotalSales
FROM Sales_CTE
ORDER BY SalesPersonID, BaseSalary;
)
Would anyone be able to provide some guidance on this?
Many thanks in advance!
This is not valid syntax for sql server. you can either create a table using CREATE TABLE and specifying the column names and types, or you can do a SELECT INTO statement including data.
Approach 1 : Create the table and then populate:
CREATE TABLE SalesOrdersPerYear
( SalesPersonID int, BaseSalary float)
;
WITH Sales_CTE (SalesPersonID, BaseSalary)
AS
(
SELECT SALES_PERSON.SALES_PERSON_ID, SALES_PERSON.BASE_SALARY
FROM SALES_PERSON
WHERE SALES_PERSON_ID IS NOT NULL
)
insert into SalesOrdersPerYear
SELECT SalesPersonID, BaseSalary AS TotalSales
FROM Sales_CTE
ORDER BY SalesPersonID, BaseSalary;
Approach 2 - all in one step
WITH Sales_CTE (SalesPersonID, BaseSalary)
AS
(
SELECT SALES_PERSON.SALES_PERSON_ID, SALES_PERSON.BASE_SALARY
FROM SALES_PERSON
WHERE SALES_PERSON_ID IS NOT NULL
)
select SalesPersonID, BaseSalary AS TotalSales
into SalesOrdersPerYear
FROM Sales_CTE
ORDER BY SalesPersonID, BaseSalary;
Use approach 1 when you need to specify more about the table (primary keys, indexes, foregin keys etc.
Use approach 2 for things that are more temporary. (you would normally use a temporary table such as #SalesOrdersPerYear here).
Either way, the data is now stored in your table, and you can use it again.
Using temporary tables:
-- Check for existence and drop first to avoid errors if it already exists.
if OBJECT_ID('tempdb..#SalesOrdersPerYear') is not null
drop table #SalesOrdersPerYear
WITH Sales_CTE (SalesPersonID, BaseSalary)
AS
(
SELECT SALES_PERSON.SALES_PERSON_ID, SALES_PERSON.BASE_SALARY
FROM SALES_PERSON
WHERE SALES_PERSON_ID IS NOT NULL
)
select SalesPersonID, BaseSalary AS TotalSales
into #SalesOrdersPerYear
FROM Sales_CTE
ORDER BY SalesPersonID, BaseSalary;
You could also define it as a table variable, which is a bit of a cross between the approaches:
declare #SalesOrdersPerYear table
( SalesPersonID int, BaseSalary float)
;
WITH Sales_CTE (SalesPersonID, BaseSalary)
AS
(
SELECT SALES_PERSON.SALES_PERSON_ID, SALES_PERSON.BASE_SALARY
FROM SALES_PERSON
WHERE SALES_PERSON_ID IS NOT NULL
)
insert into #SalesOrdersPerYear
SELECT SalesPersonID, BaseSalary AS TotalSales
FROM Sales_CTE
ORDER BY SalesPersonID, BaseSalary;
This option will only persist with this batch, and does not need dropping - just like any other variable.
Creating a view using WITH and SELECT :
CREATE VIEW SCHEMA.DEMO
AS
WITH STAGING AS (
SELECT col1 as id,col2 as name from tbl1
UNION ALL
SELECT col1 as id,col3 as name from tbl2
)
SELECT distinct id,name
FROM STAGING;
CREATE VIEW AS my_view
AS
WITH my_tables_data AS (
SELECT another_table.data1 AS some_value
FROM another_table
)
SELECT *
FROM some_data;
CREATE TABLE my_table AS
SELECT * FROM my_view;
I'm doing some relatively simple queries in Hive and cannot seem to combine GROUP BY and ORDER BY in a single statement. I have no problem doing a select into a temporary table of the GROUP BY query and then doing a select on that table with an ORDER BY, but I can't combine them together.
For example, I have a table a and can execute this query:
SELECT place,count(*),sum(weight) from a group by place;
And I can execute this query:
create temporary table result (place string,count int,sumweight int);
insert overwrite table result
select place,count(*),sum(weight) from a group by place;
select * from result order by place;
But if I try this query:
SELECT place,count(*),sum(weight) from a group by place order by place;
I get this error:
Error: Error while compiling statement: FAILED: ParseException line 1:45 mismatched input '' expecting \' near '_c0' in character string literal (state=42000,code=40000)
Try using group by as a sub-query and order by as an outer query as show below:
SELECT
place,
cnt,
sum_
FROM (
SELECT
place,
count(*) as cnt,
sum(weight) as sum_
FROM a
GROUP BY place
) a
ORDER BY place;
use sort by like this:
SELECT place,count(*),sum(weight) from a group by place sort by place;