SQL: create new table with distinct value and max/min values for other columns w/in an existing table - sql

Example:
Suppose the original table contains the following values
AcctNbr StatusDate
------------------
123 01/01/2012
123 01/01/2013
123 12/11/2011
987 01/01/2009
The SQL would create a new table containing
AcctNbr EarliestStatusDate LatestStatusDate
-------------------------------------------
123 12/11/2011 01/01/2013
987 01/01/2009 01/01/2009
I'm looking for an efficient way to do this. I have a method that works, but it takes an unacceptably long time. Does anyone have any optimization tips. Any help would be greatly appreciated.
SET NOCOUNT ON
DROP TABLE loopTemp
DROP TABLE reportTemp
CREATE TABLE loopTemp
( ID int IDENTITY(1, 1) NOT NULL,
AcctNbr varchar(50),
)
CREATE TABLE reportTemp
(
AcctNbr varchar(50),
EarliestStatus Date,
LatestStatus Date
)
INSERT INTO loopTemp
SELECT DISTINCT AcctNbr
FROM AutoStatusHistory
DECLARE #COUNTER AS INT
SET #COUNTER = 1
DECLARE #MAX AS INT
SET #MAX = (SELECT MAX(ID) FROM loopTemp)
WHILE #COUNTER < #MAX BEGIN
DECLARE #ACCOUNT_NUMBER AS varchar(50)
SET #ACCOUNT_NUMBER =
(SELECT AcctNbr FROM loopTemp WHERE ID = #COUNTER)
DECLARE #EARLIESTSTATUSDATE AS DATE
SET #EARLIESTSTATUSDATE = (SELECT MIN(NewStatusDate)
FROM AutoStatusHistory
WHERE AcctNbr = #ACCOUNT_NUMBER)
DECLARE #LATESTSTATUSDATE AS DATE
SET #LATESTSTATUSDATE = (SELECT MAX(NewStatusDate)
FROM AutoStatusHistory
WHERE AcctNbr = #ACCOUNT_NUMBER)
INSERT INTO reportTemp
VALUES (#ACCOUNT_NUMBER, #EARLIESTSTATUSDATE, #LATESTSTATUSDATE)
IF (#COUNTER % 1000) = 0 BEGIN
PRINT #COUNTER
END -- IF
SET #COUNTER = #COUNTER + 1
END -- WHILE
DROP TABLE loopTemp

Unless I'm missing something, this should be extremely simple:
SELECT AcctNbr, MIN(StatusDate) AS EarliestStatusDate,
MAX(StatusDate) AS LatestStatusDate
FROM myTable
GROUP BY AcctNbr

You appear to be using SQL Server. For this, you can do:
select AcctNbr, min(StatusDate) as EarliestStatusDate,
max(StatusDate) as LatestStatusDate
into ReportTemp
from AutoStatusHistory
group by AcctNbr
The into statement saves the results into a table. You might want to use an actual temporary table, with the syntax:
into #ReportTemp

You can use
SELECT *
INTO TableName
FROM (
SELECT AcctNbr, MIN([Status Date]) AS EarliestStatusDate,
MAX([Status Date]) AS LatestStatusDate
FROM myTable
GROUP BY AcctNbr
) RequiredData

Related

How to aggregate only few rows of data based on one column in SQL

Here I am trying to display the single date column as two different dates - one as start date and the other as end date when we have there is only one transaction type. If we have two credit records - we need to sum up the amounts and the start date should be the 1st-row date and the 2nd-row date as the end date. If there is only one row of transaction type then the same date will be as the start date and end date.
Input :
Id, Date,Transaction_type, amount
1,'01/09/2021','credit',10
2,'02/09/2021','credit',20
3,'03/09/2021','debit',30
4,'04/09/2021','debit',40
5,'05/09/2021','credit',50
6,'06/09/2021','debit',60
7,'07/09/2021','credit',70
8,'08/09/2021','credit',80
9,'09/09/2021','debit',90
Output :
Start_date, End_date, Transaction_type, amount
'01/09/2021','02/09/2021','credit',30
'03/09/2021','04/09/2021','debit',70
'05/09/2021','05/09/2021','credit',50
'06/09/2021','06/09/2021','debit',60
'07/09/2021','08/09/2021','credit',150
'09/09/2021','09/09/2021','debit',90
1st - Do not name columna as Date.
2nd - You should set another column to indicate which credit lines should be used as one.
At this form it's doable, but it's poorly designed. Do you read it with SQL or C#/Python/VBA etc? Any UI? What SQL are u using SQL server? Oracle? Can u use functions, procedures etc.
Escentially with procedure you could:
Create temp table
With while function insert data into that table
Read data from temp table
Destroy temp table.
Create Procedure:
Create Procedure dbo.TestExtract
As
BEGIN TRY
declare #startID as int
declare #endID as int
declare #TT1 as varchar(20)
declare #TT2 as varchar(20)
--create temp table
create table #temp(Start_date date, End_date Date, Transaction_type Varchar(50), amount Numeric(18))
--get first id
set #startID = (select top 1 ID from dbo.Test order by ID asc)
set #endID = (select top 1 ID from dbo.Test order by ID desc)
--loop from lowest to biggest id
while #startID <= #endID
begin
--get adjecent (with id) transaction types
select top 1 #TT1 = Transaction_type from dbo.Test where ID = #startID
select top 1 #TT2 = Transaction_type from dbo.Test where ID = #startID + 1
--compare transaction adjecent types
--if same get startd ID from first, and end date from second and sum both entries and iterate by 2
if #TT1 = #TT2
begin
Insert Into #Temp
select top 1
(select top 1 RecDate from dbo.Test Where ID = #startID) as Start_date,
(select top 1 RecDate from dbo.Test Where ID = #startID + 1) as End_date,
(select top 1 Transaction_type from dbo.Test Where ID = #startID)as Transaction_type,
(select sum(amount) from dbo.Test Where ID = #startID or ID = #startID + 1) as amount
from dbo.Test
set #startID = #startID + 2
end
--if different get only record on one line and iterate by 1
else
begin
Insert Into #Temp
select top 1
(select top 1 RecDate from dbo.Test Where ID = #startID) as Start_date,
(select top 1 RecDate from dbo.Test Where ID = #startID) as End_date,
(select top 1 Transaction_type from dbo.Test Where ID = #startID) as Transaction_type,
(select amount from dbo.Test Where ID = #startID) as amount
from dbo.Test
set #startID = #startID + 1
end
end
select * from #temp
drop table #temp
END TRY
BEGIN CATCH
IF ##TRANCOUNT > 0
ROLLBACK TRAN
DECLARE #ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE()
DECLARE #ErrorSeverity INT = ERROR_SEVERITY()
DECLARE #ErrorState INT = ERROR_STATE()
RAISERROR (#ErrorMessage, #ErrorSeverity, #ErrorState);
END CATCH
Go
Extract with procedure:
exec dbo.TestExtract

SQL Server: how to update a column with a value that is in that column when another number in another column is >1

I have a table with the following data:
Part Comp level item_nbr
-------------------------------
abc ab 1 1
null cd 2 2
null ef 3 3
cde gh 1 4
null ij 2 5
null kl 3 6
null mn 4 7
I would like to update the nulls to the value in each level 1, so every level that is >1 is updated with the level one value.
Part Comp level
---------------------
abc ab 1
abc cd 2
abc ef 3
cde gh 1
cde ij 2
cde kl 3
cde mn 4
I am at a loss as to how to achieve this on a very large dataset. Any help would be greatly appreciated!
To explain another way,
part level
abc 1
2
3
Then the next row is populated with another part
efg 1
2
2
etc.
Further clarification:
I need the string"abc" to be filled down with the string "abc" while the column fields below are null. The next row has a string of efg and the following column fields below are null, again, those fields should be filled down with the value "efg" and so on.
The level field = 1 will always have a part number, but all the other levels report up to the level 1 part, so should be populated identically. And repeat.
Hope this makes sense.
Use an updatable CTE with window functions:
with toupdate as (
select t.*,
max(part) over (partition by itm_nbr_not_null) as new_part
from (select t.*,
max(case when part is not null then item_nbr end) over (order by item_nbr) as itm_nbr_not_null
from t
) t
)
update toupdate
set part = new_part
where part is null;
You can run the CTE to see what is happening.
well, from your question what I understand is, you need to update the null column's value until you get a not null value. and you want to continue it up to the last row of the table.
for that scenario, I created a stored procedure, where I read the value of every n-th cell if it is null I changing it with the prev. cell's value, when the cell was not null.
Approach:
create a temporary table/ table variable.
add an extra column, which is basically identity, which will help to rank the column.
iterate a loop until the maximum row is reached.
in each iteration, read the cell value for the i-th row
4.1 if it is not null put it in a temporary variable.
4.2 else, replace/update the i-th cell's value with the temporary variable
continue it, until you reached up to the last row of the table/table variable.
look at my following snippets:
create proc DemoPost
as
begin
declare #table table(serial_no int identity(1,1), name varchar(30), text varchar(30), level int)
insert #table
select Name, Text, Level from Demo
declare #max as int = (select max(serial_no) from #table)
--select #max
declare #i as int =0
declare #temp as varchar(30)
declare #text as varchar(30)
while #i < #max
begin
set #i = #i +1
set #temp = (select name from #table where serial_no = #i)
-- if #temp is not null, fetch its value, otherwise, update/replace it with
-- previously gotten not-null cell's value.
if #temp is not null
begin
set #text = (select name from #table where serial_no = #i)
end
else
begin
update #table
set name = #text where serial_no = #i
end
end
select name, text, level from #table
end
You can update it using temporary table according to the given scenario i thought item_nbr is unique in row Hope this will help
SELECT *
INTO #TEMP
FROM URTablehere
DECLARE #PRev VARCHAR(MAX)
WHILE ( SELECT COUNT(*)
FROM URTablehere
) > 0
BEGIN
DECLARE #ID INT
DECLARE #Part VARCHAR(MAX)
DECLARE #Num INT
SELECT TOP ( 1 )
#ID = level ,
#Part = Part ,
#Num = item_nbr
FROM #TEMP
IF ( #ID = 1 )
BEGIN
SELECT #PRev = #Part
END
IF ( #ID > 1
AND #Part IS NULL
)
BEGIN
UPDATE URTablehere
SET Part = #PRev
WHERE item_nbr = #Num
END
DELETE
FROM #TEMP WHERE item_nbr=#Num
END

How do I modify this T-SQL query so that its ouput can be joined with a table in my database?

I am using SQl Server 2014. I have the following T-SQL query (with its corresponding output given below):
USE mydatabase
declare #t table (StayID int)
insert into #t select distinct StayID from DateOfBirth
while ((select count(1) from #t) > 0)
begin
declare #Id int = (select top 1 StayID from #t)
declare #age_tab table (StayID int, Age int, Year1 date)
declare #cage1 smallint = 2, #cage2 smallint = 5
declare #i_a int = (select AdultCount from DateOfBirth where StayID = #Id)
declare #i_c1 int = (select [0-3] from DateOfBirth where StayID = #Id)
declare #i_c2 int = (select [4-6] from DateOfBirth where StayID = #Id)
declare #age int = (select Age from DateOfBirth where StayID = #Id)
declare #Year1 date = (select cast(datepart(year,ArrivalDate) as varchar) from DateOfBirth where StayID = #Id)
while (#i_a>1)
begin
insert into #age_tab select #Id, (#age + 2), #Year1
set #age-=2
set #i_a-=1
end
while (#i_c1>0)
begin
insert into #age_tab select #Id, #cage1, #Year1
set #i_c1-=1
end
while (#i_c2>0)
begin
insert into #age_tab select #Id, #cage2, #Year1
set #i_c2-=1
end
insert into #age_tab select StayID, Age, cast(datepart(year,ArrivalDate) as varchar) from DateOfBirth where StayID = #Id
delete #t where StayID = #Id
end
select * from #age_tab
order by StayID, Age desc
The output of the above query is as follows:
StayID Age Year1
101 54 2016-01-01
101 52 2016-01-01
102 42 2016-01-01
102 40 2016-01-01
102 14 2016-01-01
...
I want to join the output of this query with values from another table in my database. Given the above query uses temporary tables to perform its operation, I am unable to wrap it into a CTE.
The additional information I want to bring to my output is stored in, say, table "t2".
Let's say the output from the query above is temporarily stored in a table called "t1"
So, I want to do something like this:
Select a.*
, b.Cty
from t1 a
LEFT JOIN t2 b on b.StayID = a.StayID
How can I do this?

Update table with new value for each row

I need to update a column (type of datetime) in the top 1000 rows my table. However the catch is with each additional row I must increment the GETDATE() by 1 second... something like DATEADD(ss,1,GETDATE())
The only way I know how to do this is something like this:
UPDATE tablename
SET columnname = CASE id
WHEN 1 THEN DATEADD(ss,1,GETDATE())
WHEN 2 THEN DATEADD(ss,2,GETDATE())
...
END
Obviously this is not plausible. Any ideas?
How about using id rather than a constant?
UPDATE tablename
SET columnname = DATEADD(second, id, GETDATE() )
WHERE id <= 1000;
If you want the first 1000 rows (by id), but the id has gaps or other problems, then you can use a CTE:
with toupdate as (
select t.*, row_number() over (order by id) as seqnum
from tablename
)
update toupdate
set columnname = dateadd(second, seqnum, getdate())
where seqnum <= 1000;
I don't know what your ID is like, and I'm assuming you have at least SQL Server 2008 or else ROW_NUMBER() won't work.
Note: I did top 2 to show you that you that the top works. You can change it to top 1000 for your actual query.
DECLARE #table TABLE (ID int, columnName DATETIME);
INSERT INTO #table(ID)
VALUES(1),(2),(3);
UPDATE #table
SET columnName = DATEADD(SECOND,B.row_num,GETDATE())
FROM #table A
INNER JOIN
(
SELECT TOP 2 *, ROW_NUMBER() OVER (ORDER BY ID) row_num
FROM #table
ORDER BY ID
) B
ON A.ID = B.ID
SELECT *
FROM #table
Results:
ID columnName
----------- -----------------------
1 2015-03-31 13:11:59.760
2 2015-03-31 13:12:00.760
3 NULL
You don't make explicit the SQL Server version you're using, so I will assume SQL Server 2005 or above. I believe the WAITFOR DELAY command would be a good option to keep adding 1 second to each rows of the datetime column.
See this example:
-- Create temp table
CREATE TABLE #Client
(
RecordID int identity(1,1),
[Name] nvarchar(100) not null,
PurchaseDate datetime null
)
-- Fill in temp table with example values
INSERT INTO #Client
VALUES ( 'Jhon', NULL)
INSERT INTO #Client
VALUES ( 'Martha', NULL)
INSERT INTO #Client
VALUES ( 'Jimmy', NULL)
-- Create local variables
DECLARE #currentRecordId int,
#currentName nvarchar(100)
-- Create cursor
DECLARE ClientsCursor CURSOR FOR
SELECT RecordID,
[Name]
FROM #Client
OPEN ClientsCursor
FETCH FROM ClientsCursor INTO #currentRecordId, #currentName
-- Check ##FETCH_STATUS to see if there are any more rows to fetch.
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE #Client
SET PurchaseDate = DATEADD(ss,1,GETDATE())
WHERE RecordID = #currentRecordId
AND [Name] = #currentName
WAITFOR DELAY '00:00:01.000'
FETCH NEXT FROM ClientsCursor INTO #currentRecordId, #currentName
END
CLOSE ClientsCursor;
DEALLOCATE ClientsCursor;
SELECT *
FROM #Client
And here's the result:
1 Jhon 2015-03-31 15:20:04.477
2 Martha 2015-03-31 15:20:05.473
3 Jimmy 2015-03-31 15:20:06.470
Hope you find this answer helpful
This should be what you need (at least a guidline)
DELIMITER $$
CREATE PROCEDURE ADDTIME()
BEGIN
DECLARE i INT Default 1 ;
simple_loop: LOOP
UPDATE tablename SET columnname = DATE_ADD(NOW(), INTERVAL i SECOND) where rownumber = i
SET i=i+1;
IF i=1001 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END $$
To call that stored procedure use
CALL ADDTIME()

Loop through a recordset and use the result to do another SQL select and return the results

I am completely new to stored procedure. This time, I need to create a stored procedure in MS SQL.
Let's say I have the following table.
Table name: ListOfProducts
--------------------------
SomeID, ProductID
34, 4
35, 8
35, 11
How do I pass in a SomeID. Use this SomeID to select a recordset from table, ListOfProducts. Then loop through this record set.
Let's say I pass in SomeID = 35.
So, the record set will return 2 records with SomeID 35. In the loop, I will get ProductID 8 and 11, which will be used to do another select from another table.
The stored procedure should return the results from the 2nd select.
How can I do this in MS SQL stored procedure?
Sorry, for this newbie question. Thanks for any help.
If you want looping through the records. You can do like:
--Container to Insert Id which are to be iterated
Declare #temp1 Table
(
tempId int
)
--Container to Insert records in the inner select for final output
Declare #FinalTable Table
(
Id int,
ProductId int
)
Insert into #temp1
Select Distinct SomeId From YourTable
-- Keep track of #temp1 record processing
Declare #Id int
While((Select Count(*) From #temp1)>0)
Begin
Set #Id=(Select Top 1 tempId From #temp1)
Insert Into #FinalTable
Select SomeId,ProductId From ListOfProducts Where Id=#Id
Delete #temp1 Where tempId=#Id
End
Select * From #FinalTable
There is probably no point in writing an explicit loop if you don't need to preform some action on the products that can't be done on the whole set. SQL Server can handle stuff like this much better on its own. I don't know what your tables look like, but you should try something that looks more like this.
CREATE PROC dbo.yourProcName
#SomeID int
AS
BEGIN
SELECT
P.ProductId,
P.ProductName
FROM
Product P
JOIN
ListOfProducts LOP
ON LOP.ProductId = P.ProductId
WHERE
LOP.SomeId = #SomeID
END
I had to do something similar in order to extract hours from a select resultset start/end times and then create a new table iterating each hour.
DECLARE #tCalendar TABLE
(
RequestedFor VARCHAR(50),
MeetingType VARCHAR(50),
RoomName VARCHAR(MAX),
StartTime DATETIME,
EndTime DATETIME
)
INSERT INTO #tCalendar(RequestedFor,MeetingType,RoomName,StartTime,EndTime)
SELECT req as requestedfor
,meet as meetingtype
,room as rooms
,start as starttime
,end as endtime
--,u.datetime2 as endtime
FROM mytable
DECLARE #tCalendarHours TABLE
(
RequestedFor VARCHAR(50),
MeetingType VARCHAR(50),
RoomName VARCHAR(50),
Hour INT
)
DECLARE #StartHour INT,#EndHour INT, #StartTime DATETIME, #EndTime DATETIME
WHILE ((SELECT COUNT(*) FROM #tCalendar) > 0)
BEGIN
SET #StartTime = (SELECT TOP 1 StartTime FROM #tCalendar)
SET #EndTime = (SELECT TOP 1 EndTime FROM #tCalendar)
SET #StartHour = (SELECT TOP 1 DATEPART(HOUR,DATEADD(HOUR,0,StartTime)) FROM #tCalendar)
SET #EndHour = (SELECT TOP 1 DATEPART(HOUR,DATEADD(HOUR,0,EndTime)) FROM #tCalendar)
WHILE #StartHour <= #EndHour
BEGIN
INSERT INTO #tCalendarHours
SELECT RequestedFor,MeetingType,RoomName,#StartHour FROM #tCalendar WHERE StartTime = #StartTime AND EndTime = #EndTime
SET #StartHour = #StartHour + 1
END
DELETE #tCalendar WHERE StartTime = #StartTime AND EndTime = #EndTime
END
Do something like this:
Declare #ID int
SET #ID = 35
SELECT
p.SomeID
,p.ProductID
FROM ListOfProducts p
WHERE p.SomeID = #ID
-----------------------
--Or if you have to join to get it
Declare #ID int
SET #ID = 35
SELECT
c.SomeID
,p.ProductID
,p.ProductName
FROM ListOfProducts p
INNER JOIN categories c on p.ProductID = c.SomeID
WHERE p.SomeID = #ID
You can use option with WHILE loop and BREAK/CONTINUE keywords
CREATE PROC dbo.yourProcName
#SomeID int
AS
BEGIN
IF OBJECT_ID('tempdb.dbo.#resultTable') IS NOT NULL DROP TABLE dbo.#resultTable
CREATE TABLE dbo.#resultTable
(Col1 int, Col2 int)
DECLARE #ProductID int = 0
WHILE(1=1)
BEGIN
SELECT #ProductID = MIN(ProductID)
FROM ListOfProducts
WHERE SomeID = #SomeID AND ProductID > #ProductID
IF #ProductID IS NULL
BREAK
ELSE
INSERT dbo.#resultTable
SELECT Col1, Col2
FROM dbo.yourSearchTable
WHERE ProductID = #ProductID
CONTINUE
END
SELECT *
FROM dbo.#resultTable
END
Demo on SQLFiddle