I have been getting the error message when trying to write the CTE SQL code.
WITH PopulationVancine (Date, Location, population, continent, new_vacinations, RollingPeopleVaccinated)
AS (
SELECT Death.date,Death.location, Death.population, Death.continent, vacine.new_vaccinations, SUM (vacine.new_vaccinations) OVER (PARTITION BY Death.location order by Death.location, Death.date) AS RollingPeopleVaccinated
FROM `my-data-project-96387.PortfolioProjectSamp.CovidDeath` as Death
INNER JOIN `my-data-project-96387.PortfolioProjectSamp.CovidVacination` as vacine
ON Death.location = vacine.location
AND Death.date = vacine.date
WHERE Death.continent is not null
)
Select *
From PopulationVancine
Please I will need your assist as regards this error message.
Working on the same project and got this error too as opposed to the script. I removed the (Date, Location, population, continent, new_vacinations, RollingPeopleVaccinated) and it worked with same results as script.Try this:
WITH PopulationVancine
AS (
SELECT Death.date,Death.location, Death.population, Death.continent, vacine.new_vaccinations, SUM (vacine.new_vaccinations) OVER (PARTITION BY Death.location order by Death.location, Death.date) AS RollingPeopleVaccinated
FROM `my-data-project-96387.PortfolioProjectSamp.CovidDeath` as Death
INNER JOIN `my-data-project-96387.PortfolioProjectSamp.CovidVacination` as vacine
ON Death.location = vacine.location
AND Death.date = vacine.date
WHERE Death.continent is not null
)
Select *
From PopulationVancine
Emmanuel Korli's answer worked brilliantly. Maybe it's something specific for Big Query or Alex's work, but the query does work without the first
"(Date, Location, population, continent, new_vacinations, RollingPeopleVaccinated)"
In my case it looks like this:
WITH PopVsVac
AS
(
SELECT
dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations,
SUM(vac.new_vaccinations) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date) AS rollout_vaccines
FROM
COVID.covid_deaths AS dea
JOIN
COVID.covid_vaccinations AS vac
on dea.location = vac.location
and dea.date = vac.date
WHERE
dea.continent is not null
ORDER BY
2, 3
)
SELECT
*, (rollout_vaccines/population)*100
FROM
PopVsVac
Related
Here is the query
WITH PopVSVac (continent, location, date, population, new_vaccinations, RollingPeopleVaccinated) AS
(
SELECT
DEATHS.continent, DEATHS.location, DEATHS.date,
DEATHS.population, VAC.new_vaccinations,
SUM(CAST(VAC.new_vaccinations AS INT)) OVER (PARTITION BY DEATHS.location
ORDER BY DEATHS.location, DEATHS.date) AS RollingPeopleVaccinated
FROM
PortfolioProject1..CovidDeaths AS DEATHS
JOIN
PortfolioProject1..CovidVaccinations AS VAC ON DEATHS.location = VAC.location
AND DEATHS.date = VAC.date
WHERE
DEATHS.continent IS NOT NULL
ORDER BY
2, 3
)
If I use offset 0 rows after by "order by 2, 3" I will get an error
incorrect syntax near ')'-
I also tried adding "TOP 1000*" next o select but then the PopVSVAC will have an error 'PopVsVac has more column than specified column list.
Please help
I got a Syntax error: Unexpected "(" at [2:1] whilst trying to create a Temp Table in Big Query. When i try to remove the "(" I get Syntax error: Expected end of input but got identifier "string" at [3:17]. How do i correct this in BigQuery.
CREATE TABLE #PercentPopulationVaccinated
(
continent string,
location string,
date datetime,
population int64,
new_vaccinations int64,
AccumVaccinations int64
)
INSERT INTO #PercentPopulationVaccinated
SELECT dea.continent, dea.location, dea.date, dea.population,vac.new_vaccinations
, SUM(vac.new_vaccinations) OVER (PARTITION BY dea.location ORDER BY dea.location,dea.date) AS AccumVaccinations
FROM `portfolio-project-356519.Covid.CovidDeaths` dea
JOIN `portfolio-project-356519.Covid.CovidVaccinations` vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
SELECT *, (AccumVaccinations/population)*100
FROM #PercentPopulationVaccinated
This is the right way to create a temporary table based on the code you shared.
CREATE TEMP TABLE PercentPopulationVaccinated
(
continent string,
location string,
date datetime,
population int64,
new_vaccinations int64,
AccumVaccinations int64
);
INSERT INTO PercentPopulationVaccinated
SELECT dea.continent, dea.location, dea.date, dea.population,vac.new_vaccinations
, SUM(vac.new_vaccinations) OVER (PARTITION BY dea.location ORDER BY dea.location,dea.date) AS AccumVaccinations
FROM `portfolio-project-356519.Covid.CovidDeaths` dea
JOIN `portfolio-project-356519.Covid.CovidVaccinations` vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL;
SELECT *, (AccumVaccinations/population)*100
FROM PercentPopulationVaccinated;
Syntax error: Expected end of input but got keyword INSERT at [11:1] error in bigquery
create table percentpopulationvaccinated
(
continent string,
Location string,
Date datetime,
population numeric,
new_vaccinations numeric,
peoplevaccinated numeric
)
insert into percentpopulationvaccinated
select
dea.continent, dea.location, dea.date, dea.population,
new_vaccinations,
sum(vac.new_vaccinations) over (partition by dea.location order by dea.location, dea.date) as peoplevaccinated
from
my-protfolio-324718.sql_code.covid_deaths dea
join
my-protfolio-324718.sql_code.covid_vac vac on dea.location = vac.location
and dea.date = vac.date
select
*,
(peoplevaccinated / population) * 100
from
percentpopulationvaccinated
The create table is one query, insert into is the second and finally the third is the select. In bigquery you should separate them using a ; so the interpreter can run every step in order.
I am working with a tool that would extract some data from an Access Database. So basically, i am working on a query to get this data.
Below is the code i am currently working on.
I am getting an error: Syntax error in FROM clause
I can't seem to find where the query is going wrong. I would appreciate any help! Thank youu.
EDIT: putting my actual query
SELECT table_freq.*, IIF(table_freq.txn_ctr > (table_ave_freq.ave_freq * 3), "T", "F") as suspicious_flag
FROM
(
SELECT tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date, Count(tbl_TransactionHistory.client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date
) AS table_freq
INNER JOIN
(
SELECT table_total_freq.client_num, total_txn_ctr as TotalTransactionFrequency, total_no_days as TotalTransactionDays,
(table_total_freq.total_txn_ctr)/(table_no_of_days.total_no_days) AS ave_freq
FROM
(
(
SELECT client_num, SUM(txn_ctr) AS total_txn_ctr
FROM
(
SELECT client_num, client_name, transaction_date, COUNT(client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY client_num, client_name, transaction_date
) AS tabFreq
GROUP BY client_num
) AS table_total_freq
INNER JOIN
(
SELECT client_num, COUNT(txn_date) as total_no_days
FROM
(
SELECT DISTINCT(transaction_date) as txn_date, client_num
FROM tbl_TransactionHistory
ORDER BY client_num
) AS table1
GROUP BY client_num
) AS table_no_of_days
ON table_total_freq.client_num = table_no_of_days.client_num
)
) AS table_ave_freq
ON table_freq.client_num = table_ave_freq.client_num
I am trying to use group by in with all fields in select too.
My query:
SELECT count(*) as totalRow,
trim(A.P_CODE) P_CODE,
INITCAP(trim(A.ALTERNATE_TEXT)) ALTERNATE_TEXT,
trim(A.LINKED_SKU_CODE) as LINKED_SKU_CODE,
UCWORD(B.BRAND_NAME) BRAND_NAME,
LOWER(TRIM(A.UNIT)) UNIT,
INITCAP(TRIM(A.PK_SIZE)) PK_SIZE,
trim(A.DF_SALE_RATE) DF_SALE_RATE,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) CAT_TYPE,
NVL(MAX_QTY,25) MAX_QTY,
A.BAL_QTY
FROM GET_PRODUCT A,
WEB_BRANDS B
WHERE A.P_CODE in (".$p_codes.")
AND A.BR_CODE = '".BR_CODE."'
AND A.BRAND_CODE = B.BRAND_CODE
GROUP BY A.P_CODE,
A.ALTERNATE_TEXT,
A.LINKED_SKU_CODE,
B.BRAND_NAME,
A.UNIT,
A.PK_SIZE,
A.DF_SALE_RATE,
A.MRP,
A.CAT_TYPE,
MAX_QTY,
A.BAL_QTY
ORDER BY CAT_TYPE,
P_NAME
Error:
ORA-00979: not a GROUP BY expression in C:
Please point me in right direction or could point me where I have done mistakes.
You should write in GROUP BY section at least all columns from SELECT section ( without aggregatable columns). I think you query should look like
SELECT count(*) as totalRow,
trim(A.P_CODE) P_CODE,
INITCAP(trim(A.ALTERNATE_TEXT)) ALTERNATE_TEXT,
trim(A.LINKED_SKU_CODE) as LINKED_SKU_CODE,
UCWORD(B.BRAND_NAME) BRAND_NAME,
LOWER(TRIM(A.UNIT)) UNIT,
INITCAP(TRIM(A.PK_SIZE)) PK_SIZE,
trim(A.DF_SALE_RATE) DF_SALE_RATE,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) CAT_TYPE,
NVL(MAX_QTY,25) MAX_QTY,
A.BAL_QTY
FROM GET_PRODUCT A,
WEB_BRANDS B
WHERE A.P_CODE in (".$p_codes.")
AND A.BR_CODE = '".BR_CODE."'
AND A.BRAND_CODE = B.BRAND_CODE
GROUP BY
trim(A.P_CODE) ,
INITCAP(trim(A.ALTERNATE_TEXT)),
trim(A.LINKED_SKU_CODE) ,
UCWORD(B.BRAND_NAME) ,
LOWER(TRIM(A.UNIT)),
INITCAP(TRIM(A.PK_SIZE)),
trim(A.DF_SALE_RATE) ,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) ,
NVL(MAX_QTY,25),
A.BAL_QTY