Here's what I am trying to run in BigQuery, but keep getting error code. What am I doing wrong?
#Show number of deaths of is person was smoker
select dea.location, dea.population, gen.female_smokers, gen.male_smokers
sum(female_smokers) as TotalFemales, sum(gen.male_smokers) as TotalMale over (PARTITION BY dea.location order by dea.location, dea.population) as RollingWomen
FROM capstone-project-317016.Portfolio1.CovidDeaths dea;
join capstone-project-317016.Portfolio1.CovidbyGender gen
on dea.location = gen.location
and dea.population = gen.population
where dea.location is not null
group by dea.population
Add comma after gen.male_smokers line 1
There must be some aggregate function before over (PARTITION BY dea.location order by dea.location, dea.population) as RollingWomen
Related
This question already has answers here:
Column name or number of supplied values does not match table definition
(16 answers)
Closed 4 months ago.
I am getting an error when trying to run the code below. I have tried almost everyting and have no idea why it's wrong. Can you please have a look at that?
Create Table #PercentPopulationVaccinated
(
continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_vaccinations numeric,
RollingPeopleVaccinated numeric
)
Insert into #PercentPopulationVaccinated
select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, sum(convert(bigint,vac.new_vaccinations)) OVER (Partition by dea.Location order by dea.location, dea.date) as RollingPeopleVaccinated
from PortfolioProject..CovidDeaths dea
join PortfolioProject..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
Select *, (RollingPeopleVaccinated/population)*100
From #PercentPopulationVaccinated
Insert into #PercentPopulationVaccinated(continent,location,
date,population,New_vaccinations,RollingPeopleVaccinated )
select dea.continent, dea.location,
dea.date, dea.population, vac.new_vaccinations,
sum(convert(bigint,vac.new_vaccinations)) OVER (Partition by dea.Location order by dea.location, dea.date)
from PortfolioProject..CovidDeaths dea
join PortfolioProject..CovidVaccinations vac
on dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
--Looking At Total Population vs Vaccinations
SELECT dea.continent, dea.location, dea.population, dea.Date
, vac.new_vaccinations
, SUM(CAST(vac.new_vaccinations AS int)) OVER (Partition BY dea.location ORDER BY dea.location, dea.Date) AS RollingPopVaccinated
FROM PortfolioProject..CovidDeaths dea
JOIN PortfolioProject..CovidVaccination vac
ON dea.location = vac.location
AND dea.Date = vac.Date
WHERE dea.continent IS NOT NULL
OUTPUT:
Msg 8115, Level 16, State 2, Line 70
Arithmetic overflow error converting expression to data type int.
Warning: Null value is eliminated by an aggregate or other SET operation.
Try casting new_vaccinations to bigint instead of regular int:
SELECT dea.continent, dea.location, dea.population, dea.Date, vac.new_vaccinations,
SUM(CAST(vac.new_vaccinations AS bigint)) OVER (PARTITION BY dea.location
ORDER BY dea.location, dea.Date) AS RollingPopVaccinated
FROM PortfolioProject..CovidDeaths dea
INNER JOIN PortfolioProject..CovidVaccination vac
ON dea.location = vac.location AND dea.Date = vac.Date
WHERE dea.continent IS NOT NULL;
Hi i am trying to pull Top 3 account Id for every sales person which they are dealing in columns wise and trying to run below query but getting error " Incorrect syntax near '1' "
Can someone please suggest why that error is
SQL Code :
Select *
from openquery([server01],'select distinct Seller_id, Account_id, Account_Name,
Account_rank from
(Select distinct Seller_ID, Account_id,Account_Name,Revenue,
dense_rank () over( partition by Seller_ID order by
Revenue desc ) as Account_Rank
from Table_Account ) a
PIVOT (
Max (Account_id) FOR Account_Rank in( ''1'' ,''2'',''3'' )
)AS pvt
where account_rank <=3 and
order by Seller_ID ')
I tried below query as per the above comment and it worked
SQL Code :
Select * from openquery([server01],'select distinct Seller_id,
Account_id, Account_Name, Account_rank from (Select distinct
Seller_ID, Account_id,Account_Name,Revenue,
dense_rank () over( partition by Seller_ID order by Revenue desc )
as
Account_Rank from Table_Account ) a
PIVOT (
Max (Account_id) FOR
Account_Rank in( '[1]' ,'[2]','[3]' )
)AS pvt
where account_rank <=3 order by Seller_ID ')
I run the below SQL query and get this error: Errors: Ambiguous column name 'EmailAddress'.
Have tried to use select R.EmailAddress..., but it got me the same error.
Without the Join the query works well.
what am I missing?
select EmailAddress, [Order Number], order_date, rank,
ROW_NUMBER() OVER (PARTITION BY order_date, EmailAddress ORDER BY order_date DESC) AS rankDate,
R.firstname as 'FirstName',R.lastname as 'LastName', 'IL' as 'Locale'
from(
select
O.FirstName, O.LastName, O.email as 'EmailAddress',
O.order_number as 'Order Number',O.order_date,
ROW_NUMBER() OVER (PARTITION BY O.email ORDER BY O.order_date DESC) AS rank,
O.order_number as 'Order Number Compare'
from [TEST ORDER] O
where O.order_date>='12/27/2020' and O.email is not null
) as R
join [All Blend Subscribers] as ABS
on R.EmailAddress = ABS.emailAddress
Because of your need to tell SQL server what EmailAddress want to get from tables on SELECT part, there are two kind of EmailAddress from your query.
select R.EmailAddress, [Order Number], order_date, rank,
ROW_NUMBER() OVER (PARTITION BY order_date, R.EmailAddress ORDER BY order_date DESC) AS rankDate,
R.firstname as 'FirstName',R.lastname as 'LastName', 'IL' as 'Locale'
from(
select
O.FirstName, O.LastName, O.email as 'EmailAddress',
O.order_number as 'Order Number',O.order_date,
ROW_NUMBER() OVER (PARTITION BY O.email ORDER BY O.order_date DESC) AS rank,
O.order_number as 'Order Number Compare'
from [TEST ORDER] O
where O.order_date>='12/27/2020' and O.email is not null
) as R
join [All Blend Subscribers] as ABS
on R.EmailAddress = ABS.emailAddress
I would get columns by an alias clearly because it can avoid ambiguous error.
I have the following table (SQL Server 2012):
DID - cust id
GID - order id
AMT - order amt
Gf_Date - order date
SC - order reversal amount
I'm trying to calculate a running count of orders and a running total of sales by customer so that I can assign a flag to the point in time where a customer achieved cumulative sales of $1,000. As a first step, I've run this query:
Select
[DID]
, [AMT]
, [Gf_Date]
, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]
But I get the error message:
Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'
I posted this earlier with the wrong error message pasted in. Regrets and apologies. What you see above is the result I'm getting. Someone commented that this syntax is incorrect. Now that all is in order, can someone tell me what I'm doing wrong?
You should use ROW_NUMBER (link) instead of COUNT:
DECLARE #Threshold NUMERIC(19,2)=1000; -- Use the same data type as `[AMT]`'s data type
Select
[DID]
, [AMT]
, [Gf_Date]
--, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, ROW_NUMBER() OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
, CASE
WHEN SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) >= #Threshold THEN 1
ELSE 0
END IsThresholdPassed
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]