Pervasive PSQL: Select with order by inside select - pervasive

I have an query with an select inside select.
The select inside the select has a order by.
The problem is that Pervasive gives a error on the order by.
The query:
select itemID,item1,item2 from "table2" where itemID in (select top 1 itemID from "table1" order by ID desc)
The error:
[Pervasive][ODBC Engine Interface]Syntax Error: select itemID,item1,item2 from "table2" where itemID in (select top 1 itemID from "table1" order<< ??? >> by ID desc)
What am I doing wrong?

Related

SQL Server - How to set Row ID for duplicate or similar content based on insertion first. Select query

Priority is the output column.
The group contains duplicate content.
how can I fix this using SQL query?.
enter image description here
One way to achieve the desired result with this data is to use Dense_rank() function like below:
select *, dense_rank() over (order by [Group]) as Priority
from tab
order by No
For any value, please try the following
;with cte as
(
select [Group], ROW_NUMBER() over (order by No_min) as rn
from
(
select [Group], min([No]) No_min
from tab
group by [Group]
)t
)
select t.*, x.rn as [Priority]
from cte x
join tab t on t.[Group] = x.[Group]
order by 1
Please find the db<>fiddle here.

SQL Server window function implementation issue

I have a table structure like below:
I have the following query to get the unique result from the table:
WITH Dupes AS
(
SELECT
ID, Template_ID, Address, Job_Number, Other_Info,
Assigned_By, Assignees, Active, seen,
ROW_NUMBER() OVER (PARTITION BY Template_ID,Job_Number ORDER BY ID) AS RowNum
FROM
Schedule
WHERE
Assignees IN ('9', '16', '22')
)
SELECT
ID, Template_ID, Job_Number, Address, Other_Info,
Assigned_By, Assignees, Active, seen
FROM
Dupes
WHERE
RowNum = 1
Output of the above query is:
If the Job_Number and Template_ID are same, only return one row(first row using ID). That is why I did use ROW_NUMBER() OVER(PARTITION BY Template_ID,Job_Number ORDER BY ID) AS RowNum. I am not sure how to fix this as I rarely used this function.
I need to get the output like below:
Updated Code
Tried the code below:
seems your trying to group by Job_Number, remove Template_ID on your partition by clause
WITH Dupes AS
(
SELECT ID,Template_ID,Address,Job_Number,Other_Info,Assigned_By,Assignees,Active,seen,
ROW_NUMBER() OVER(PARTITION BY rtrim(ltrim(Job_Number)) ORDER BY ID) AS RowNum
FROM Schedule
WHERE Assignees IN('9','16','22')
)
SELECT ID,Template_ID,Job_Number,Address,Other_Info,Assigned_By,Assignees, Active,seen FROM Dupes WHERE RowNum=1

Column 'ACCOUNT.ACCOUNT_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I am trying to get available balance on last(max) date. I am trying to write below query but it is showing error.
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by CUST_ID;
Column 'ACCOUNT.ACCOUNT_ID' is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY
clause.
I am new to sql. Can anyone let me know where I am wrong in this query?
Any column not having a calculation/function on it must be in the GROUP BY clause.
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE;
If you're wanting the most recent row for each customer, think ROW_NUMBER(), not GROUP BY:
;With Numbered as (
select *,ROW_NUMBER() OVER (
PARTITION BY CUST_ID
ORDER BY LAST_ACTIVITY_DATE desc) rn
from Account
)
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,LAST_ACTIVITY_DATE
from Numbered
where rn=1
I think you want to select one records having max(LAST_ACTIVITY_DATE) for each CUST_ID.
For this you can use TOP 1 WITH TIES like following.
SELECT TOP 1 WITH TIES account_id,
avail_balance,
open_date,
last_activity_date
FROM account
ORDER BY Row_number()
OVER (
partition BY cust_id
ORDER BY last_activity_date DESC)
Issue with your query is, you can't select non aggregated column in select if you don't specify those columns in group by
If you want to get the max activity date for a customer then your query should be as below
select CUST_ID, MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by CUST_ID;
You can't select any other column which is not in the group by clause. The error message also giving the same message.
with query(CUST_ID, LAST_ACTIVITY_DATE) as
(
select
CUST_ID,
MAX(LAST_ACTIVITY_DATE) as LAST_ACTIVITY_DATE
from ACCOUNT
group by CUST_ID
)
select
a.ACCOUNT_ID,
a.AVAIL_BALANCE,
a.OPEN_DATE,
a.LAST_ACTIVITY_DATE
from ACCOUNT as a
inner join query as q
on a.CUST_ID = q.CUST_ID
and a.LAST_ACTIVITY_DATE = q.LAST_ACTIVITY_DATE

SQL Server: select all duplicate rows where col1+col2 exists more than once

I have a table which has around 300,000 rows. 225 Rows are being added to this table daily since March 16,2015 till July 09,2015
My problem is that, from last 1 week or so, some duplicate rows are being entered in the table (i.e more than 225 per day)
Now I want to select (and ultimately delete!) all the duplicate rows from the table that have more than 1 siteID+ reportID combination existing against one Date column .
Example is attached in the screenshot:
When Row_Number() is used with Partition By clause, it can provide the SQL developer to select duplicate rows in a table
Please check the SQL tutorial on how to delete duplicate rows in SQL table
Below query is what is copied from that article and applied to your requirement:
;WITH DUPLICATES AS
(
SELECT *,
RN = ROW_NUMBER() OVER (PARTITION BY siteID, ReportID ORDER BY Date)
FROM myTable
)
DELETE FROM DUPLICATES WHERE RN > 1
I hope it helps,
When you want to filter duplicated rows I suggest you this type of query:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY Col3) As seq
FROM yourTable) dt
WHERE (seq > 1)
Like this:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY siteID, reportID, [Date] ORDER BY ID) As seq
FROM yourTable) dt
WHERE (seq > 1)

ORDER BY clause error

Hey guys I am getting this exception when trying to run the query
Column restaurantData.restaurantId is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
Query:
SELECT
T1.restaurantId AS ID,
COUNT(T2.post_id) AS favCount
FROM
restaurantData AS T1
INNER JOIN
favoriteData AS T2 ON T1.restaurantId = T2.post_id
ORDER BY
favCount DESC, ID DESC
It says that I have to add all select columns which are not aggregated in ORDER BY clause, which I already did for restaurantID. But its still giving me error.
You are missing GROUP BY T1.restaurantId in the query.
SELECT T1.restaurantId AS ID,
COUNT(T2.post_id) AS favCount
FROM restaurantData AS T1
INNER JOIN favoriteData AS T2
ON T1.restaurantId = T2.post_id
GROUP BY T1.restaurantId
ORDER BY favCount DESC,
ID DESC
If you have no GROUP BY clause at all then this gives you an aggregate of the whole source table (in which case inclusion of restaurantId in the select list is invalid without wrapping that in an aggregate) rather than broken into groups by restaurant as you clearly desire.
You're using an aggregate function (COUNT), so you have to use a GROUP BY clause as well:
SELECT
T1.restaurantId AS ID,
COUNT(T2.post_id) AS favCount
FROM
restaurantData AS T1
INNER JOIN
favoriteData AS T2 ON T1.restaurantId = T2.post_id
GROUP BY
T1.restaurantID
ORDER BY
favCount DESC, ID DESC
Basically, with your query, you want to count the rows found - and you want the result grouped by each restaurantId - so that's what the GROUP BY clause expresses.

Resources