Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
Ok so I have this two table "NewDB" and "NewSAP" here is the data sample NewDB is DB and NewSAP is SAP in the sheet
Schema of NewDB
CREATE TABLE "NewDB" (
"Employee_ID" nvarchar(500),
"Name" nvarchar(500),
"Date_Out" nvarchar(500),
"Status" nvarchar(500)
);
Schema of NewSAP
CREATE TABLE "NewSAP" (
"Employee_No" nvarchar(500),
"Type" nvarchar(500),
"Start_Date" nvarchar(500),
"End_Date" nvarchar(500)
);
I want to perform this thing
foreach employeed_ID in the NewDB if it matches the the Employee_NO then it will compare the Date_Out of NewDB to the Start_Date & and the End_Date of the NewSAP table if one of them matches the date given
(also if it doesn't match the start_date & end_date it will compare with the range of dates in between them , also there are possibilities that the both the start_date and the end_date are same , mostly it will be same , but there could be range too)
for eg start_date = 20220901 and end_date = 20220910 in both of this column the format of date is like YYYYMMDD without any separator
and second scenario of date could be like
start_date = 20220720 & end_date = 20220720 here both are having the same date
if the date is matched
1:- Get the Employee_Id from DB
2:- get the Date_Out for that particualr emoployeed_id from DB
3:- Get the Status from the DB (Note! in the sheets there many diffrent string in the satus but in the sql table i have replaced every thing that is != 'Absent' to 'Present' so in the table it is just have 'Present' & 'Absent')
4:- get the Type from the SAP
(all this for that particualr Employee_ID)
if it dosent macth the date for that particiaur Employee_ID
1:- Get the Employee_Id from DB
2:- get the Date_Out for that particualr emoployeed_id from DB
3:- Get the Status from the DB
(You will not get the Type if date doesn't match)
Ater this in the output table
This three coulmn will never be null
(!:- Emp_ID 2:-Date (Date is from the DB)& 3:- DBType (DBType refers to the Status in the DB))
(Note! i will highly recommend you to see the sample of output in the Output&Condtion Sheet )
In the Database i have made some changes in the DB table Status i have made some change now there is only P , A and HA in the status rather that Present, Absent , HalfDay and some other you may see in the sheets right now
Now what should be the condition
all the condition i have tried to explain it in the Output&Condition Sheet
please let me know if you have any difficulty with doing that
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a stored procedure that queries a linked server and performs inserts into a local table. I would like to store the max(RID) from the data that I have inserted so that I can use that RID from my tableL as a pointer for my next run.
I cannot use SCOPE_IDENTITY() as I'm not planning to store the identity column of my local but I would like to store max(RID) from the linked table.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER PROCEDURE [dbo].[procedure_name]
DECLARE #RID INT
AS
BEGIN
INSERT INTO tableA (RID, Name)
SELECT *
FROM OPENQUERY(linkname, 'SELECT RID, Name FROM linktable WHERE RID > #RID')
END
TableA (ID is sequential PK)
| ID | RID | Name |
|----|-----|------|
TableL (ID is sequential PK)
| ID | RID | Time |
|----|-----|------|
Note: I realized after posting this that you can't pass variables into OPENQUERY, so I changed it to just be a direct query. Depending on your usage, this may or may not be okay.
I'm personally a fan of temp tables, so I would probably do something like this:
-- Get the RID of the last successful run for this table
DECLARE #LastRID int = 0;
SELECT #LastRID = LastRID
FROM dbo.SomeETLTrackingTable
WHERE TableName = 'linktable';
-- Pull the data from linkname over to this server
SELECT RID, [Name]
INTO #data
FROM linkname.linkdb.dbo.linktable
WHERE RID > #LastRID;
-- Insert the data we pulled
INSERT INTO tableA ([Name])
SELECT [Name]
FROM #data;
-- Get the max RID of the batch
SELECT #LastRID = MAX(RID)
FROM #data;
-- Update our ETL tracking table
UPDATE e SET e.LastRID = #LastRID
FROM dbo.SomeETLTrackingTable e
WHERE e.TableName = 'linktable';
Note: I'm not including any transactions or error handling, but you should to ensure you only update the tracking table upon successful import.
I would also suggest you learn other methods for ETL'ing data. I'm not going to say that what you're doing is wrong, because it works perfectly fine for many applications. But if you are trying to do more complex ETL processes in the future, it would be beneficial to learn what other options you have.
I'm only going to name a couple because there are TONS of things out there:
rowversion - https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql
Change Tracking - https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-tracking-sql-server
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am inserting production orders into a SQL Server database with a stored procedure called via Microsoft PowerApps. Currently, the build date column is just using today's date as the build date. Attached below is a sample of records:
As you can see, all 10 of these orders have today's date as the set build date. However, I'd like to dynamically set this build date based upon the necessary output for the day. This will most likely be passed a variable to the stored procedure. For example - if the build output was 5 per day, rows 1-5 would have a build date of '02/17/2020' while the next 5 would have have a build date of '02/18/2020'.
This is simple enough since the number of orders is divisible by the build output. However, let's say the build output is 3 instead of 5. The first three orders would be '02/17/2020', the next three would be '02/18/2020', and the next three would be '02/19/2020'. This would leave one order to be set to '02/20/2020'. Then if I added more orders, it would stack upon each date filling each up to 3. Is there a way to do this via a stored procedure?
You might need to do some adjustments but based on my understanding, this will do the trick ([Date] is the date column in your sample table above):
CREATE PROC yourProcName #pNUM AS INT = 1 --PARAMETER
AS
(
DECLARE #pDate DATE;
SET #pDate = COALESCE((SELECT TOP 1 CONVERT(DATE, MAX([Date])), GETDATE()) FROM yourDestTable); --SETTING THE DATE AS MAX DATE ALREADY EXISTS IN THE TABLE. IF NOTHING IS THERE, WE'LL USE TODAY
DECLARE #pLead INT = COALESCE((SELECT TOP 1 COUNT(*) FROM yourDestTable WHERE [Date] = #pDate), 0);--SEE HOW MANY RECORDS IN THE DATABASE EXIST WITH THE DATE WE DEFINED
WITH cte
AS
(
SELECT *,
FLOOR((
ROW_NUMBER() OVER(ORDER BY #pDate) -
0.001)/#pNUM) AS RN -- SEPARATING RECORDS USING ROW_NUMBER DIVIDED BY DEFINED NUMBER OF PRODS PER DAY. USING FLOOR AND MINUS A SMALL AMOUNT TO ROUND IT RIGHT
FROM yourSourceTable
)
INSERT INTO yourDestTable
SELECT
C.*,
DATEADD(DAY, LEAD(C.RN, #pLead), #pDate) AS [Date] --PUSHING THE NUMBER BASED ON ROWS ALREADY EXISTED WITH THE MAX_DATE
INTO yourDestTable
FROM cte AS C
)
You might want to use MOD in CTE based on your usecase. But the idea should work. Then call it like this EXEC yourProcName <number of transactionsday>
I'm working on a Access project and I need help with something..
I have 2 tables, first one named tblHoliday, with fields (ID, OfficerID, OffType, From, To) [From] and [To] are Date Fields.
The second table is tblService with fields (ID, OfficerID, Date).
There is a relationship between [OfficerID] in each table.
Every [OfficerID] may have two records or more in a table tblService,,
I designed a form for tblHoliday to enter the officer's holiday, my question is (I need to check if any [Date] for the same [OfficerID] in the table tblService between [From] and [To], for OfficerID in the table tblHoliday), example: an officer ask for a holiday from 1/1/2017 to 1/10/2017 if he has a service in 1/3/2017 will give a message "the officer has a service on 1/3/2017"
My code works, but only for the first record in tblService for the same OfficerID
Dim dtmMyDate As Date
dtmMyDate = DLookup("Date", "tblService", "OfficerID = " & Me.OfficerID)
If dtmMyDate > Me.From And dtmMyDate < Me.To Then
MsgBox "The Officer has a Service"
Else
MsgBox "Done"
End If
Create a query:
Select
tblService.*
tblHoliday.*
Where
tblService.OfficerID = tblHoliday.OfficerID
And
tblService.Date Between tblHoliday.From And tblHoliday.To
Open a recordset from this or assign it to a listbox to either loop or list the offending service days.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new in TSQL.
I need to write a stored procedure which returns filtered records.
So the user has two dates to enter. He may enter only one date, or two, or both, or any.
In stored proc I have two params #From, #To - date type.
I need to search records.
If user didn't enter dates - I don't care about dates in the select query.
If the user entered 2 dates - I need to search with them inclusively.
If the user entered #From date - I need to search up to today inclusively.
If the user entered #To date - I need to search dates less than #To.
Thanks for your help.
SELECT ColumnList
FROM MyTable
WHERE
(#FromDate IS NULL OR FromDateColumn >= #FromDate)
AND (#ToDate IS NULL OR ToDateColumn <= #ToDate )
(But be aware that this can suffer from the effects of an unsuitable cached query plan due to parameter sniffing, especially if there is a large number of parameter conditions in the WHERE clause)
SELECT
Columns
FROM
Table
WHERE
DateColumn BETWEEN (CASE WHEN #FromDate IS NULL THEN DateColumn ELSE #FromDate END)
AND (CASE WHEN #ToDate IS NULL THEN DateColumn ELSE #ToDate END)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get date/time information from a TIMESTAMP column?
How to convert SQL Server’s timestamp column to datetime format
I am trying to execute a simple query that inserts a timestamp. The query runs with no problems but this is what is inserted into the column rather than the current time:
0x00000000000007D2
This is the query:
INSERT INTO auctions (title, description, finish_date)
VALUES ('Some title', 'Some description', '05/03/2003 11:15:45')
From what I've read Timestamp has been deprecated by something called Rowversion but I can't find such a data type in the management studio.
The auctions table:
auction_id - bigint
title - nvarchar(MAX)
description - nvarchar(MAX)
start_date - timestamp
finish_date - datetime
You can't use the Timestamp type to store dates.
The timestamp is basically a counter of insert and update operations done within a single database, so it is local to a given database and has no real time value.
To conform with your insert statement, you should use a DateTime instead - both for your start_date and for your finish_date.
See this link: http://msdn.microsoft.com/en-us/library/ms182776(v=sql.90).aspx