Insert a batch number into a table in Oracle SQL - sql

I have a temp table where I insert modified data extracted from a SELECT query.
In this temp table I want to group my rows into batches, so I added an indexed INT column called "BATCH_NUM"
The idea that I am hoping to achieve is this (for say 1000 results in my SELECT statement).
Pseudo Code
Batch Size = 100
Count = 0
For batch size in results set
Insert Into Temp Table (a , b , y , count)
Count++
Current SQL - inputs static value of 1 into BATCH_NUM column
INSERT INTO TEMP_TABLE
(
ASSET_ID,
PAR_PROM_INTEG_ID,
IGNORE
BATCH_NUM
)
SELECT carelevel.row_id, pstn.PROM_INTEG_ID,
CASE
WHEN promoprod.fabric_cd = 'Disabled'
THEN 'Y'
ELSE 'N'
END
'1'
FROM SIEBEL.S_ASSET carelevel
INNER JOIN SIEBEL.S_ASSET pstn
ON pstn.row_id = carelevel.par_asset_id
INNER JOIN SIEBEl.S_ASSET promotion
ON pstn.prom_integ_id = promotion.integration_id
INNER JOIN SIEBEL.S_PROD_INT prod
ON prod.row_id = carelevel.prod_id
INNER JOIN SIEBEL.S_ORG_EXT bill
ON carelevel.bill_accnt_id = bill.row_id
INNER JOIN SIEBEL.S_INV_PROF invoice
ON bill.row_id = invoice.accnt_id
INNER JOIN SIEBEL.S_PROD_INT promoprod
ON promotion.prod_id = promoprod.row_id
WHERE prod.part_num = 'Testproduct'
But if the select statement has 1000 results, then I want BATCH_NUM to go from 1,2,3,4,5,6,7,8,9,10 per 100 records.
Can this be done?

To map record to batch, you might simply want to use integer division. Or slightly more complicated as row are numbered from 1, but something like TRUNC((ROWNUM-1)/100)+1 will do the trick.
Here is a test for that mapping:
select level, trunc((ROWNUM-1)/100)+1 from dual connect by level <= 1000
Result:
ROWNUM TRUNC((ROWNUM-1)/100)+1
1 1
...
100 1
101 2
...
200 2
201 3
...
...
900 9
901 10
...
1000 10
Given your query:
INSERT INTO TEMP_TABLE
(
ASSET_ID,
PAR_PROM_INTEG_ID,
IGNORE,
BATCH_NUM
)
SELECT carelevel.row_id, pstn.PROM_INTEG_ID,
CASE
WHEN promoprod.fabric_cd = 'Disabled'
THEN 'Y'
ELSE 'N'
END,
TRUNC((ROWNUM-1)/100)+1,
-- ^^^^^^^^^^^^^^^^^^^^
-- map rows 1-100 to batch 1, rows 101-200 to batch 2 and so on
FROM SIEBEL.S_ASSET carelevel
INNER JOIN SIEBEL.S_ASSET pstn
ON pstn.row_id = carelevel.par_asset_id
INNER JOIN SIEBEl.S_ASSET promotion
ON pstn.prom_integ_id = promotion.integration_id
INNER JOIN SIEBEL.S_PROD_INT prod
ON prod.row_id = carelevel.prod_id
INNER JOIN SIEBEL.S_ORG_EXT bill
ON carelevel.bill_accnt_id = bill.row_id
INNER JOIN SIEBEL.S_INV_PROF invoice
ON bill.row_id = invoice.accnt_id
INNER JOIN SIEBEL.S_PROD_INT promoprod
ON promotion.prod_id = promoprod.row_id
WHERE prod.part_num = 'Testproduct'

Related

How to find difference between table with multiple conditions

I have exact two tables but some value differences. So I would like to find those differences with condition that if the column value has a difference of more than 10.
For example, all 9 columns have the same values in both tables, but the difference between the values column is 11, so this record is different. If the value difference is 9 so records are the same.
So I wrote this query to get differences:
select *
from test.test m
inner join test.test1 t
on
m.month_date = t.month_date and
m.level_1 = t.level_1 and
m.level_2 = t.level_2 and
m.level_3 = t.level_3 and
m.level_4 = t.level_4 and
m.level_header = t.level_header and
m.unit = t.unit and
m.model_type_id = t.model_type_id and
m.model_version_desc = t.model_version_desc
where m.month_date = '2022-11-01' and abs(m.value - t.value) > 10)
so this returns me all records that all column values are matched but did not pass the value difference condition.
Second, i have full outer join to get all differences
select *
from test.test m
full outer join test.test1 t
on
m.month_date = t.month_date and
m.level_1 = t.level_1 and
m.level_2 = t.level_2 and
m.level_3 = t.level_3 and
m.level_4 = t.level_4 and
m.level_header = t.level_header and
m.unit = t.unit and
m.model_type_id = t.model_type_id and
m.model_version_desc = t.model_version_desc
where m.month_date is null or t.month_date is null and m.month_date = '2022-11-01'
How can I combine the results of these two queries without UNION? I want to have only one query (sub query is acceptable)
Assuming that for a given day, you need to find
rows that match between the tables but exceed the value difference threshold
AND
rows present in either left or right table, that don't have a corresponding row in the other table
select *
from test.test m
full outer join test.test1 t
using (
month_date,
level_1,
level_2,
level_3,
level_4,
level_header,
unit,
model_type_id,
model_version_desc )
where (m.month_date is null
or t.month_date is null
and m.month_date = '2022-11-01' )
or (m.month_date = '2022-11-01' and abs(m.value - t.value) > 10);
Online demo
Since the columns used to join the tables have the same names, you can shorten their list by swapping out the lengthy table1.column1=table2.column1 and... list of pairs for a single USING (month_date,level_1,level_2,level_3,...) (doc). As a bonus, it will avoid listing the matching columns twice in your output, once for the left table, once for the right table.
select *
from (select 1,2,3) as t1(a,b,c)
full outer join
(select 1,2,3) as t2(a,b,c)
on t1.a=t2.a
and t1.b=t2.b
and t1.c=t2.c;
-- a | b | c | a | b | c
-----+---+---+---+---+---
-- 1 | 2 | 3 | 1 | 2 | 3
select *
from (select 1,2,3) as t1(a,b,c)
full outer join
(select 1,2,3) as t2(a,b,c)
using(a,b,c);
-- a | b | c
-----+---+---
-- 1 | 2 | 3
In your first query, you can replace the null values for a specific number. Something like this:
where m.month_date = '2022-11-01' and abs(ISNULL(m.value,-99) - ISNULL(t.value,-99)) > 10)
The above will replace the nulls for -99 (choose an appropriate value for your data), so if you have that m.value is 10 and t.value is null, then should be returned in your first query.

How do you properly query the result of a complex join statement in SQL?

New to advanced SQL!
I'm trying to write a query that returns the COUNT(*) and SUM of the resulting columns from this query:
DECLARE #Id INT = 1000;
SELECT
*,
CASE
WHEN Id1 >= 6 THEN 1
ELSE 0
END AS Tier1,
CASE
WHEN Id1 >= 4 THEN 1
ELSE 0
END AS Tier2,
CASE
WHEN Id1 >= 2 THEN 1
ELSE 0
END AS Tier3
FROM (
SELECT
Org.OrgID,
App.AppID,
App.FirstName,
App.LastName,
MAX(AppSubmitU_Level.Id1) AS Id1
FROM Org
INNER JOIN AppEmployment
ON AppEmployment.OrgID = Org.OrgID
INNER JOIN App
ON App.AppID = AppEmployment.AppID
INNER JOIN AppSubmit
ON App.AppID = AppSubmit.AppID
INNER JOIN AppSubmitU_Level
ON AppSubmit.LevelID = AppSubmitU_Level.Id1
INNER JOIN AppEmpU_VerifyStatus
ON AppEmpU_VerifyStatus.VerifyStatusID = AppEmployment.VerifyStatusID
WHERE AppSubmitU_Level.SubmitTypeID = 1 -- Career
AND AppEmpU_VerifyStatus.StatusIsVerified = 1
AND AppSubmit.[ExpireDate] IS NOT NULL
AND AppSubmit.[ExpireDate] > GETDATE()
AND Org.OrgID = #Id
GROUP BY
Org.OrgID,
App.AppID,
App.FirstName,
App.LastName
) employees
I've tried to do so by moving the #Id outside the original query, and adding a SELECT(*), SUM, and SUM to the top, like so:
DECLARE #OrgID INT = 1000;
SELECT COUNT(*), SUM(employees.Tier1), SUM(employees.Tier2), SUM(employees.Tier3)
FROM
(SELECT *,
...
) AS employees
);
When I run the query, however, I'm getting the errors:
The multi-part identifier employees.Tier1 could not be bound
The same errors appear for the other identifiers in my SUM statements.
I'm assuming this has to do with the fact that the Tier1, Tier2, and Tier3 columns are being returned by the inner join query in my FROM(), and aren't values set by the existing tables that I'm querying. But I can't figure out how to rewrite it to initialize properly.
Thanks in advance for the help!
This is a scope problem: employees is defined in the subquery only, it is not available in the outer scope. You basically want to alias the outer query:
DECLARE #OrgID INT = 1000;
SELECT COUNT(*), SUM(employees.Tier1) TotalTier1, SUM(employees.Tier2) TotalTier2, SUM(employees.Tier3) TotalTier3
FROM (
SELECT *,
...
) AS employees
) AS employees;
--^ here
Note that I added column aliases to the outer query, which is a good practice in SQL.
It might be easier to understand what is going on if you use another alias for the outer query:
SELECT COUNT(*), SUM(e.Tier1), SUM(e.Tier2), SUM(e.Tier3)
FROM (
SELECT *,
...
) AS employees
) AS e;
Note that you don't actually need to qualify the column names in the outer query, since column names are unambigous anyway.
And finally: you don't actually need a subquery. You could write the query as:
SELECT
SUM(CASE WHEN Id1 >= 6 THEN 1 ELSE 0 END) AS TotalTier1,
SUM(CASE WHEN Id1 >= 4 THEN 1 ELSE 0 END) AS TotalTier2,
SUM(CASE WHEN Id1 >= 2 THEN 1 ELSE 0 END) AS TotalTier3
FROM (
SELECT
Org.OrgID,
App.AppID,
App.FirstName,
App.LastName,
MAX(AppSubmitU_Level.Id1) AS Id1
FROM Org
INNER JOIN AppEmployment
ON AppEmployment.OrgID = Org.OrgID
INNER JOIN App
ON App.AppID = AppEmployment.AppID
INNER JOIN AppSubmit
ON App.AppID = AppSubmit.AppID
INNER JOIN AppSubmitU_Level
ON AppSubmit.LevelID = AppSubmitU_Level.Id1
INNER JOIN AppEmpU_VerifyStatus
ON AppEmpU_VerifyStatus.VerifyStatusID = AppEmployment.VerifyStatusID
WHERE AppSubmitU_Level.SubmitTypeID = 1 -- Career
AND AppEmpU_VerifyStatus.StatusIsVerified = 1
AND AppSubmit.[ExpireDate] IS NOT NULL
AND AppSubmit.[ExpireDate] > GETDATE()
AND Org.OrgID = #Id
GROUP BY
Org.OrgID,
App.AppID,
App.FirstName,
App.LastName
) employees

SQL getting averages with multiple joins

I'm trying to write a single query using 3 tables.
The tables and their columns that I will be using are:
Sec – ID, Symbol
Hss – Code, HDate, Holiday
Fddd – ID, Date, Price
Given a symbol AAA, I need to get the ID from the first table and match it with the ID from the third table. The second table's date must match the third table's dates with the condition of Code=1 and Holiday=1.
The Dates in the second and third table are in ascending order with most recent dates at the bottom. I want to get the average 50 day and 200 day prices. The dates in the tables are in ascending order so I want to make it descending and select the top 50 and 200 to get the average prices.
So far I can only get one average. I cannot add a second SELECT TOP 50 or add a subquery within the second avg().
SELECT AVG(TwoHun)TwoHunAvg --, AVG(Fifty) AS FiftyAvg
FROM (SELECT TOP 200 Fddd.price AS TwoHun --, TOP 50 Fddd.price AS Fifty
FROM Sec
JOIN Fddd
ON Sec.ID = Fddd.ID AND Sec.symbol = 'AAA'
JOIN Hss
ON Fddd.date = Hss.Hdate AND Hss.Code = 1 AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp;
Thanks in advance!
Consider a union query which even scales for other Averages. I add a Type column to indicate the Averages.
SELECT '200 DAY AVG' As Type, AVG(TwoHun) As Avg
FROM
(SELECT TOP 200 Fddd.price AS TwoHun
FROM Sec
INNER JOIN Fddd ON Sec.ID = Fddd.ID
INNER JOIN Hss ON Fddd.date = Hss.Hdate
WHERE Sec.symbol = 'AAA' AND Hss.Code = 1 AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp;
UNION
SELECT '50 DAY AVG' As Type, AVG(FiftyHun) As Avg
FROM
(SELECT TOP 50 Fddd.price AS FiftyHun
FROM Sec
INNER JOIN Fddd ON Sec.ID = Fddd.ID
INNER JOIN Hss ON Fddd.date = Hss.Hdate
WHERE Sec.symbol = 'AAA' AND Hss.Code = 1 AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp;
Also, I moved some of your join expressions to where clause which should not change performance but does in readability.
I suspect your using SQL Server or MS Access.
One quick solution would be to have your total query as a subquery and then copy a modified version as a second subquery.
Quick rough example:
SELECT (SELECT
AVG(Fifty) AS FiftyAvg
FROM (SELECT TOP 50
Fddd.price AS Fifty
FROM Sec
JOIN Fddd
ON Sec.ID = Fddd.ID
AND Sec.symbol = 'AAA'
JOIN Hss
ON Fddd.date = Hss.Hdate
AND Hss.Code = 1
AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp)
AS FiftyAvg,
(SELECT
AVG(TwoHun) TwoHunAvg
FROM (SELECT TOP 200
Fddd.price AS TwoHun
FROM Sec
JOIN Fddd
ON Sec.ID = Fddd.ID
AND Sec.symbol = 'AAA'
JOIN Hss
ON Fddd.date = Hss.Hdate
AND Hss.Code = 1
AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp)
AS TwoHundredAverge;

T-SQL cursor or if or case when

I have this table:
Table_NAME_A:
quotid itration QStatus
--------------------------------
5329 1 Assigned
5329 2 Inreview
5329 3 sold
4329 1 sold
4329 2 sold
3214 1 assigned
3214 2 Inreview
Result output should look like this:
quotid itration QStatus
------------------------------
5329 3 sold
4329 2 sold
3214 2 Inreview
T-SQL query, so basically I want the data within "sold" status if not there then "inreview" if not there then "assigned" and also at the same time if "sold" or "inreview" or "assigned" has multiple iteration then i want the highest "iteration".
Please help me, thanks in advance :)
This is a prioritization query. One way to do this is with successive comparisons in a union all:
select a.*
from table_a a
where quote_status = 'sold'
union all
select a.*
from table_a a
where quote_status = 'Inreview' and
not exists (select 1 from table_a a2 where a2.quoteid = a.quoteid and a2.quotestatus = 'sold')
union all
select a.*
from table_a a
where quote_status = 'assigned' and
not exists (select 1
from table_a a2
where a2.quoteid = a.quoteid and a2.quotestatus in ('sold', 'Inreview')
);
For performance on a larger set of data, you would want an index on table_a(quoteid, quotestatus).
You want neither cursors nor if/then for this. Instead, you'll use a series of self-joins to get these results. I'll also use a CTE to simplify getting the max iteration at each step:
with StatusIterations As
(
SELECT quotID, MAX(itration) Iteration, QStatus
FROM table_NAME_A
GROUP BY quotID, QStats
)
select q.quotID, coalesce(sold.Iteration,rev.Iteration,asngd.Iteration) Iteration,
coalesce(sold.QStatus, rev.QStatus, asngd.QStatus) QStatus
from
--initial pass for list of quotes, to ensure every quote is included in the results
(select distinct quotID from table_NAME_A) q
--one additional pass for each possible status
left join StatusIterations sold on sold.quotID = q.quotID and sold.QStatus = 'sold'
left join StatusIterations rev on rev.quotID = q.quotID and rev.QStatus = 'Inreview'
left join StatusIterations asngd on asngd.quotID = q.quotID and asngd.QStatus = 'assigned'
If you have a table that equates a status with a numeric value, you can further improve on this:
Table: Status
QStatus Sequence
'Sold' 3
'Inreview' 2
'Assigned' 1
And the code becomes:
select t.quotID, MAX(t.itration) itration, t.QStatus
from
(
select t.quotID, MAX(s.Sequence) As Sequence
from table_NAME_A t
inner join Status s on s.QStatus = t.QStatus
group by t.quotID
) seq
inner join Status s on s.Sequence = seq.Sequence
inner join table_NAME_A t on t.quotID = seq.quotID and t.QStatus = s.QStatus
group by t.quoteID, t.QStatus
The above may look like complicated at first, but it can be faster and it will scale easily beyond three statuses without changing the code.

Link tables based on column value

Is it possible to pull values from 2 different tables based on the value of a column? For example, I have a table with a boolean column that either returns 0 or 1 depending on what the end user selects in our program. 0 means that I should pull in the default values. 1 means to use the user's data.
If my table Table1 looked like this:
Case ID Boolean
====================
1 0
2 1
3 1
4 0
5 0
Then I would need to pull Case IDs 1,4,and 5's corresponding data from table Default and Case IDs 3 and 4's corresponding data from table UserDef. Then I would have to take these values, combine them, and reorder them by Case ID so I can preserve the order in the resulting table.
I am fairly inexperienced with SQL but I am trying to learn. Any help or suggestions are greatly appreciated. Thank you in advance for your help.
Something like this:
SELECT
t1.CaseID
,CASE WHEN t1.Boolean = 1 THEN dt.Col1 ELSE ut.Col1 END AS Col1
,CASE WHEN t1.Boolean = 1 THEN dt.Col2 ELSE ut.Col2 END AS Col2
FROM Table1 t1
LEFT JOIN DefaultTable dt ON dt.CaseID = t1.CaseID
LEFT JOIN UserDefTable ut ON ut.CaseID = t1.CaseID
ORDER BY t1.CaseID
You join on both tables and then use CASE in SELECT to choose from which one to display data.
Option B:
WITH CTE_Combo AS
(
SELECT 0 as Boolean, * FROM Default --replace * with needed columns
UNION ALL
SELECT 1 AS Boolean, * FROM UserDef --replace * with needed columns
)
SELECT * FROM Table1 t
LEFT JOIN CTE_Combo c ON t.CaseID = c.CaseID AND t.Boolean = c.Boolean
ORDER BY t.CaseID
This might be even simpler - using CTE make a union of both tables adding artificial column, and then join CTE and your Table using both ID and flag column.
SELECT t1.CaseID,
ISNULL(td.data, tu.data) userData -- pick data from table_default
-- if not null else from table_user
FROM table1 t1
LEFT JOIN table_default td ON t1.CaseID = td.CaseID -- left join with table_default
AND t1.Boolean = 0 -- when boolean = 0
LEFT JOIN table_user tu ON t1.CaseID = tu.CaseID -- left join with table_user
AND t1.Boolean = 1 -- when boolean = 1
ORDER BY t1.CaseID