Symmetricds Initial Load with Order by SQL Server - sql-server

I am trying to do an initial load of data based on order by creationtime column. My sym_trigger_router (for currencies table ) has below initial_load_select :-
initial_load_select = "1 = 1 order by t.CreationTime"
which create query as below :-
select count(*)
from "board"."dbo"."currencies" t
where 1=1
order by t.CreationTime desc;
It works with MySQL but not with SQL Server because SQL Server does not support order by with count(*).
I'm getting this error :
[head-85711c60-84d5-4527-b31f-a861aa8caa9a] - JdbcSqlTemplate - SQL caused exception: [select count(*) from "board"."dbo"."currencies" t where 1=1 order by t.CreationTime desc ]
java.sql.SQLException: Column "board.dbo.currencies.creationtime" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
As per this link
When 0 is specified for initial_load_batch_count, SymmetricDS will
execute a count(*) query on the table during the extract process
but symmetricds 3.9 has removed this column, now what is workaround to ignore count(*) query for SQL Server for initial load?
Any help?

Related

SQL Server : Update WHERE xxx AND most recent record

Similar to this question, but I require this in Microsoft SQL Server.
I'm looking to update values in my database, but only the most recently added value for each date.
For example if I have 4 rows where column_day = '2023-02-01',
and they have updated datetimes in a column [Time_Stamp] of
2023-01-01, 2023-01-27, 2023-01-10, and 2023-01-05,
I would like to only update the Jan 27, 2023 2023-01-27 line.
The answer provided here which does not work in Microsoft SQL Server is:
UPDATE your_table
SET some_column = 1
ORDER BY date_time_column DESC
LIMIT 1
My code I'm trying is slightly different as it includes a WHERE:
I get an error
Incorrect syntax near the keyword 'order'
UPDATE your_table
SET some_column = 1
WHERE column_tag = 'test' AND column_day = '2023-02-01'
ORDER BY Time_Stamp DESC
LIMIT 1
How can I achieve this in Microsoft SQL Server?
You can use TOP in an UPDATE, however, you can't provide an ORDER BY; as such it's really used for batching and previously UPDATEd rows are filtered in each iteration in the WHERE clause.
For what you want, one method would be to use an UPDATEable CTE with ROW_NUMBER:
WITH CTE AS(
SELECT SomeColumn,
ROW_NUMBER() OVER (ORDER BY date_time_column DESC) AS RN
FROM dbo.YourTable
WHERE ColumnTag = 'Test'
AND ColumnDay = '20230201')
UPDATE CTE
SET SomeColumn = 1
WHERE RN = 1;

How do I create table in SQL Server using group by query

I'm using a query to create a new table in SQL Server using a select query.
My query is
create table test as
select id, sum(marks) as marks
from student
group by id
I'm getting error message in SQL Server. In Stackoverflow I found that instead of create table, SQL Server uses insert into. But I'm not getting how to write group by query using insert into. Can you please guide me?
Try this:
select id,sum(marks) as marks
INTO TEST
from student
group by id
Using INTO clause you are allowed to materialized the result in a table (it can be temporary table as well). There are few limitations - columns must have name (alias) and duplicate names are not allowed.
Select (columns Name or Functions (Max , Min , Sum ...) 1 , 2 , ...)
INTO (Destination table)
From (Source table)
GROUP BY (cloumns Name 1 , 2 , ...)

SQL error on join and group by

What is wrong with the fallowing SQL statement? I'm getting the error describe below. I'm using Microsoft SQL Server 2012.
The SQL statement:
SELECT
ContentRequests.Title,ContentRequests.ChapterOrArticleTitle,
ContentRequests.Institution_Id,
Institutions.Name
FROM
[HexDatabaseDev].[dbo].[ContentRequests]
LEFT JOIN
Institutions ON Institutions.Id = ContentRequests.Institution_Id
GROUP BY
ContentRequests.Title
The error I'm getting :
Column 'HexDatabaseDev.dbo.ContentRequests.ChapterOrArticleTitle' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Rule of Group By..
In the select clause,you can ignore only the columns involving aggregates or constants,rest of the columns in select should be in Group By
So in your case.
group by
ContentRequests.ChapterOrArticleTitle,ContentRequests.ChapterOrArticleTitle
,ContentRequests.Institution_Id
, Institutions.Name

SQL Server Pagination w/o row_number() or nested subqueries?

I have been fighting with this all weekend and am out of ideas. In order to have pages in my search results on my website, I need to return a subset of rows from a SQL Server 2005 Express database (i.e. start at row 20 and give me the next 20 records). In MySQL you would use the "LIMIT" keyword to choose which row to start at and how many rows to return.
In SQL Server I found ROW_NUMBER()/OVER, but when I try to use it it says "Over not supported". I am thinking this is because I am using SQL Server 2005 Express (free version). Can anyone verify if this is true or if there is some other reason an OVER clause would not be supported?
Then I found the old school version similar to:
SELECT TOP X * FROM TABLE WHERE ID NOT IN (SELECT TOP Y ID FROM TABLE ORDER BY ID) ORDER BY ID where X=number per page and Y=which record to start on.
However, my queries are a lot more complex with many outer joins and sometimes ordering by something other than what is in the main table. For example, if someone chooses to order by how many videos a user has posted, the query might need to look like this:
SELECT TOP 50 iUserID, iVideoCount FROM MyTable LEFT OUTER JOIN (SELECT count(iVideoID) AS iVideoCount, iUserID FROM VideoTable GROUP BY iUserID) as TempVidTable ON MyTable.iUserID = TempVidTable.iUserID WHERE iUserID NOT IN (SELECT TOP 100 iUserID, iVideoCount FROM MyTable LEFT OUTER JOIN (SELECT count(iVideoID) AS iVideoCount, iUserID FROM VideoTable GROUP BY iUserID) as TempVidTable ON MyTable.iUserID = TempVidTable.iUserID ORDER BY iVideoCount) ORDER BY iVideoCount
The issue is in the subquery SELECT line: TOP 100 iUserID, iVideoCount
To use the "NOT IN" clause it seems I can only have 1 column in the subquery ("SELECT TOP 100 iUserID FROM ..."). But when I don't include iVideoCount in that subquery SELECT statement then the ORDER BY iVideoCount in the subquery doesn't order correctly so my subquery is ordered differently than my parent query, making this whole thing useless. There are about 5 more tables linked in with outer joins that can play a part in the ordering.
I am at a loss! The two above methods are the only two ways I can find to get SQL Server to return a subset of rows. I am about ready to return the whole result and loop through each record in PHP but only display the ones I want. That is such an inefficient way to things it is really my last resort.
Any ideas on how I can make SQL Server mimic MySQL's LIMIT clause in the above scenario?
Unfortunately, although SQL Server 2005 Row_Number() can be used for paging and with SQL Server 2012 data paging support is enhanced with Order By Offset and Fetch Next, in case you can not use any of these solutions you require to first
create a temp table with identity column.
then insert data into temp table with ORDER BY clause
Use the temp table Identity column value just like the ROW_NUMBER() value
I hope it helps,

TSQL (SQL Server) Sorting and paging with row_number

I have a database (SQL Server) and app which fetches data and converts them into JSON.
I wrote a T-SQL query to order data by userid column (DESC) and take only first 10 rows, but it causes problem returning wrong results.
For example if I have following table:
UserID
---
User1
User2
User3
...
User10
..
User25
I want to to UserID to be DESC and get first ten results (then second ten results, etc). Simple saying I am looking for MySQL LIMIT substitute in SQL Server.
My query
SELECT * FROM
(SELECT
system_users_ranks.RankName,
system_users.userid,
system_users.UserName,
system_users.Email,
system_users.LastIP,
system_users.LastLoginDate,
row_number() OVER (ORDER BY system_users.userid) as myrownum
FROM
system_users
INNER JOIN
system_users_ranks
ON system_users.UserRank = system_users_ranks.rankid
) as dertbl
WHERE myrownum BETWEEN #startval AND #endval
ORDER BY userid DESC
I can't move ORDER BY to inner SELECT.
You don't need it in the inner SELECT.
ROW_NUMBER has its own ORDER BY, and the final presentation is defined by the outermost ORDER BY anyway.
Your current query will work just fine.

Resources