Clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions - ssms

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

Related

Query error: Column name ICUSTAY_ID is ambiguous. Using multiple subqueries in BigQuery

Hi, I receive the following query error "Query error: Column name ICUSTAY_ID is ambiguous" referred to the third last line of code (see the following code). Please can you help me? Thank you so much!
I am an SQL beginner..
WITH t AS
(
SELECT
*
FROM
(
SELECT *,
DATETIME_DIFF(CHARTTIME, INTIME, MINUTE) AS pi_recorded
FROM
(
SELECT
*
FROM
(
SELECT * FROM
(SELECT i.SUBJECT_ID, p.dob, i.hadm_id, p.GENDER, a.ETHNICITY, a.ADMITTIME, a.INSURANCE, i.ICUSTAY_ID,
i.DBSOURCE, i.INTIME, DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) AS age,
CASE
WHEN DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) <= 32485
THEN 'adult'
WHEN DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) > 32485
then '>89'
END AS age_group
FROM `project.mimic3.ICUSTAYS` AS i
INNER JOIN `project.mimic3.PATIENTS` AS p ON i.SUBJECT_ID = p.SUBJECT_ID
INNER JOIN `project.mimic3.ADMISSIONS` AS a ON i.HADM_ID = a.HADM_ID)
WHERE age >= 6570
) AS t1
LEFT JOIN
(
SELECT ITEMID, ICUSTAY_ID, CHARTTIME, VALUE, FROM `project.mimic3.CHARTEVENTS`
WHERE ITEMID = 551 OR ITEMID = 552 OR ITEMID = 553 OR ITEMID = 224631
OR ITEMID = 224965 OR ITEMID = 224966
) AS t2
ON t1.ICUSTAY_ID = t2.ICUSTAY_ID
)
)
WHERE ITEMID IN (552, 553, 224965, 224966) AND pi_recorded <= 1440
)
SELECT ICUSTAY_ID #### Query error: Column name ICUSTAY_ID is ambiguous
FROM t
GROUP BY ICUSTAY_ID;
Both t1 and t2 have a column called ICUSTAY_ID. When you join them together into a single dataset you end up with 2 columns with the same name - which obviously can't work as there would be no way of uniquely identify each column.
You need to alias these columns in you code or not include one or the other if you don't need both

Why I got this error in execution of this script (plsql oracle)

I'm writing plsql script and I get an error in execution in the part when I use ROW_NUMBER() OVER (PARTITION BY... :
this the line of my code that give me this error : inconsistent datatypes: expected - got CLOB
This is my code :
CREATE OR REPLACE VIEW "VW_APP_PU_LOCATION_CLUSTER" ("ID", "BATCH_ID", "PRODUCTION_SYSTEM_ID", "CODE", "URGENCY", "CURRENT_STATE", "VALUE", "ELEMENT_ID","VIEW_ID") AS
SELECT app.id AS id
, app.batch_state AS batch_id
, app.production_system_id AS production_system_id
, atp.code AS code
, app.urgency_code AS urgency
, app.current_state AS current_state
, dbms_lob.substr(el.value, 4000, 1) AS value
, el.element_id AS element_id
, ROW_NUMBER() OVER (PARTITION BY LOWER(el.value) ORDER BY loc.code) AS view_id
FROM application app
CROSS JOIN application_type atp
CROSS JOIN location loc
CROSS JOIN application_order ord
LEFT JOIN data_element el ON (app.id = el.application_id AND el.element_id = isexistsparam('Pu Generation','SORTING_DATA_ELEMENT'))
WHERE app.application_type_id = atp.id
AND app.location_id = loc.id
AND app.application_order_id = ord.id
AND app.current_state in
(
select distinct sd.state
from process_state_definition psd,
state_definition sd,
application_type appType,
application appli,
process_definition pd
Where
((appType.code is null)or (appType.id = appli.application_type_id AND sd.id = psd.state_definition_id)
AND psd.application_type_id = appType.id
AND psd.process_definition_id = pd.id
AND psd.state_type='START'
AND pd.name = 'Pu Generation')
)
ORDER BY loc.code asc,
CASE WHEN isexistsparam('Pu Generation', 'SORTING_DATA_ELEMENT') != -1
THEN dbms_lob.substr(el.value, 4000, 1) ELSE NULL
END
, ord.date_ordered asc
, app.application_number asc;
can someone explain to me what is wrong with ROW_NUMBER() function Thanks
There is restriction:
You cannot specify LOB columns in the ORDER BY clause of a query, or
in the GROUP BY clause of a query or in an aggregate function.
You can transform CLOB into CHAR if value less 4000 characters.
*My explanation or how I understand:
ROW_NUMBER is the analytic function. Analytic functions compute an aggregate value based on a group of rows. We have PARTITION BY that grouping data based on LOB column.
So, returning to restriction:
You cannot specify LOB columns ... in the GROUP BY clause of a query ...
**Sorry for my English.

Error in SQL Server query - inner join

I am trying to add Percentile value for each record as new column. But i am getting error in my SQL query. Can anyone please help to solve it.
Error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'inner'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b where [Country] = 'AUSTRALIA'
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
You have your where statement in the wrong place. Joins are formally part of the from statement and thus come before criteria. To have your criterion at the bottom check the correct table you use the alias.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
where b.[Country] = 'AUSTRALIA'

databasename.d.first_column_in_the_table' isn't in GROUP BY

i always get the same error whenever i run this query.
i tried using simple query to test if there is something wrong in the query.
I also noticed that error happened when im using the view in my query.
CREATE OR REPLACE VIEW v_vss_car_wash AS
SELECT
max(r.id) AS id
,d.id AS dealer_id
,d.dealer_code
,d.dealer_name
,count(*) AS total_respondents
,sum(car_washed) AS car_washed -- Car Washed
,count(*) - sum(car_washed) AS car_unwashed -- Car Unwashed
,sum(IF (car_washed AND car_satisfied, 1, 0)) AS car_satisfied
,sum(IF (car_washed AND NOT car_satisfied, 1, 0)) AS car_unsatisfied
,MONTH(r.create_date) AS create_month
,YEAR(r.create_date) AS create_year
FROM t_vss_survey_response r
LEFT JOIN t_vss_dealer d ON (r.dealer_id = d.id)
WHERE survey_code = "ASS"
GROUP BY dealer_code, YEAR(r.create_date), MONTH(r.create_date);
then this is my query im using(very simple) but i always get the same error.
select a.dealer_name, a.dealer_code
from v_vss_car_wash a
now, this is the error message
Caused by: org.eclipse.birt.data.engine.odaconsumer.OdaDataException: Cannot get the result set metadata.
org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object.
SQL error #1:'crmsdbdev.d.dealer_name' isn't in GROUP BY
;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 'crmsdbdev.d.dealer_name' isn't in GROUP BY
Just add the appropriate columns to the group by:
CREATE OR REPLACE VIEW v_vss_car_wash AS
SELECT
max(r.id) AS id
,d.id AS dealer_id
,d.dealer_code
,d.dealer_name
,count(*) AS total_respondents
,sum(car_washed) AS car_washed -- Car Washed
,count(*) - sum(car_washed) AS car_unwashed -- Car Unwashed
,sum(IF (car_washed AND car_satisfied, 1, 0)) AS car_satisfied
,sum(IF (car_washed AND NOT car_satisfied, 1, 0)) AS car_unsatisfied
,MONTH(r.create_date) AS create_month
,YEAR(r.create_date) AS create_year
FROM t_vss_survey_response r
LEFT JOIN t_vss_dealer d ON (r.dealer_id = d.id)
WHERE survey_code = "ASS"
GROUP BY d.id, d.dealer_code, d.dealer_name,
YEAR(r.create_date), MONTH(r.create_date);

SQL Server GROUP BY troubles!

I'm getting a frustrating error in one of my SQL Server 2008 queries. It parses fine, but crashes when I try to execute. The error I get is the following:
Msg 8120, Level 16, State 1, Line 4
Column
'customertraffic_return.company' is
invalid in the select list because it
is not contained in either an
aggregate function or the GROUP BY
clause.
SELECT *
FROM (SELECT ctr.sp_id AS spid,
Substring(ctr.company, 1, 20) AS company,
cci.email_address AS tech_email,
CASE
WHEN rating IS NULL THEN 'unknown'
ELSE rating
END AS rating
FROM customer_contactinfo cci
INNER JOIN customertraffic_return ctr
ON ctr.sp_id = cci.sp_id
WHERE cci.email_address <> ''
AND cci.email_address NOT LIKE '%hotmail%'
AND cci.email_address IS NOT NULL
AND ( region LIKE 'Europe%'
OR region LIKE 'Asia%' )
AND SERVICE IN ( '1', '2' )
AND ( rating IN ( 'Premiere', 'Standard', 'unknown' )
OR rating IS NULL )
AND msgcount >= 5000
GROUP BY ctr.sp_id,
cci.email_address) AS a
WHERE spid NOT IN (SELECT spid
FROM customer_exclude)
GROUP BY spid,
tech_email
Well, the error is pretty clear, no??
You're selecting those columns in your inner SELECT:
spid
company
tech_email
rating
and your grouping only by two of those (GROUP BY ctr.sp_id, cci.email_address).
Either you need group by all four of them (GROUP BY ctr.sp_id, cci.email_address, company, rating), or you need to apply an aggregate function (SUM, AVG, MIN, MAX) to the other two columns (company and rating).
Or maybe using a GROUP BY here is totally the wrong way to do - what is it you're really trying to do here??
The inner query:
SELECT ctr.sp_id AS spid,
Substring(ctr.company, 1, 20) AS company,
cci.email_address AS tech_email,
CASE
WHEN rating IS NULL THEN 'unknown'
ELSE rating
END AS rating
FROM customer_contactinfo cci
INNER JOIN customertraffic_return ctr
ON ctr.sp_id = cci.sp_id
WHERE cci.email_address <> ''
AND cci.email_address NOT LIKE '%hotmail%'
AND cci.email_address IS NOT NULL
AND ( region LIKE 'Europe%'
OR region LIKE 'Asia%' )
AND SERVICE IN ( '1', '2' )
AND ( rating IN ( 'Premiere', 'Standard', 'unknown' )
OR rating IS NULL )
AND msgcount >= 5000
GROUP BY ctr.sp_id,
cci.email_address
has 4 non-aggregate things in the select (sp_id, company, email_address, rating) and you only group on two of them, so it is throwing an error on the first one it sees
So you either need to not group by any of them or group by all of them
i suggest replacing the * with a fully specified column list.
you can either group by all selected columns or use the other columns (not in group by clause) in a aggregate function (like sum)
you cannot: select a,b,c from bla group by a,b
but you can: select a,b,sum(c) from bla groupy by a,b