CAPTURE CHANGE AT SOURCE LEVEL - oracle11g

Consider a source table(ORA 11g) with BATCH ID 1 with 3 records for day 1. Say these are getting loaded into a target table. Imagine on day 2 there are 3 more customer entries with batch ID 2. Can I write a SQL query which will be enable the source node to check the target if the BATCH_ID is existing and if not read and process that BATCH_IDs records through the code?
SRC TBL(say day1)
Batch_no | ID
1 | xx
1 | yy
1 | zz
TGT TBL(EOD Day1)
Batch_no | ID
1 | xx
1 | yy
1 | zz
SRC TBL(Day 2)
Batch_no|ID
1 |xx
1 |yy
1 |zz
2 |aa
2 |bb
2 |cc

This is what I found. Thank you for your help.
SELECT required fields
FROM
"SRC TBL"
LEFT JOIN "TGT TBL"
ON "SRC TBL".BATCH_ID="TGT TBL".BATCH_ID
WHERE "TGT TBL".BATCH_ID IS NULL

Related

Returning count for each id in table

Im using sqlite3 with my node.js API.
I have a DB talbe structured below:
id | colour
___|_______
1 | blue
1 | red
1 | green
2 | yellow
2 | green
5 | red
I want to return a count of the IDs in my table such that
1 - 3 occurences
2 - 2 occurences
5 - 1 occurence
Is there a sql qualifier I can use count like this, or will this need to be done within the js iteself?
Any help here would be awesome!
You can use COUNT with GROUP BY
select id, COUNT(id) from tbl GROUP BY id

Get frequency counts for a subset of elements in a column

I may be missing some elegant ways in Stata to get to this example, which has to do with electrical parts and observed monthly failures etc.
clear
input str3 (PartID Type FailType)
ABD A 4
BBB S 0
ABD A 3
ABD A 4
ABC A 2
BBB A 0
ABD B 1
ABC B 7
BBB C 1
BBB D 0
end
I would like to group by (bysort) each PartID and record the highest frequency for FailType within each PartID type. Ties can be broken arbitrarily, and preferably, the lower one can be picked.
I looked at groups etc., but do not know how to peel off certain elements from the result set. So that is a major question for me. If you execute a query, how do you select only the elements you want for the next computation? Something like n(0) is the count, n(1) is the mean etc. I was able to use contract, bysort etc. and create a separate data set which I then merged back into the main set with an extra column There must be something simple using gen or egen so that there is no need to create an extra data set.
The expected results here will be:
PartID Freq
ABD 4 #(4 occurs twice)
ABC 2 #(tie broken with minimum)
BBB 0 #(0 occurs 3 times)
Please let me know how I can pick off specific elements that I need from a result set (can be from duplicate reports, tab etc.)
Part II - Clarification: Perhaps I should have clarified and split the question into two parts. For example, if I issue this followup command after running your code: tabdisp Type, c(Freq). It may print out a nice table. Can I then use that (derived) table to perform more computations programatically?
For example get the first row of the table.
Table. ----------------------
Type| Freq ----------+-----------
A | -1
B | -1
C | -1
D | -3
S | -3
---------------------- –
I found this difficult to follow (see comment on question), but some technique is demonstrated here. The numbers of observations in subsets of observations defined by by: are given by _N. The rest is sorting tricks. Negating the frequency is a way to select the highest frequency and the lowest Type which I think is what you are after when splitting ties. Negating back gets you the positive frequencies.
clear
input str3 (PartID Type FailType)
ABD A 4
BBB S 0
ABD A 3
ABD A 4
ABC A 2
BBB A 0
ABD B 1
ABC B 7
BBB C 1
BBB D 0
end
bysort PartID FailType: gen Freq = -_N
bysort PartID (Freq Type) : gen ToShow = _n == 1
replace Freq = -Freq
list PartID Type FailType Freq if ToShow
+---------------------------------+
| PartID Type FailType Freq |
|---------------------------------|
1. | ABC A 2 1 |
3. | ABD A 4 2 |
7. | BBB A 0 3 |
+---------------------------------+

looping through csv file

I'm very new to R and Rstudio. What I'm trying to do is loop through a csv file.
The file has 3 columns. 1) user 2) event (success or fail) 3) randNum
So basically every user starts off with a fail and once they reach a success it moves on to the next user.
Ex:
user: | event: | randNum
user1 | fail | 6
user1 | fail | 4
user1 | fail | 1
user1 | success | 2
user2 | ... |
Basically what I need to do is this. I need to store the first random number (6) and than the last random number (2) which will be whenever the user succeeds. How would I do that? And i need to do this for each user because I will be doing something with these numbers.
The quickest way would be to use table to get counts:
table(df$user)
Example code:
> df <- data.frame(user=c(rep("john",4),rep("jane",3)), event=c(rep("failed",3), "success", rep("failed",2), "success"))
> df
user event
1 john failed
2 john failed
3 john failed
4 john success
5 jane failed
6 jane failed
7 jane success
> table(df$user)
jane john
3 4
EDIT: To address latest edits you made that substantially modified the question:
> df <- data.frame(user=c(rep("john",4),rep("jane",3)), event=c(rep("failed",3), "success", rep("failed",2), "success"), randNum=c(4,6,1,2,9,3,5))
> library(dplyr)
> df <- df %>% group_by(user) %>% mutate(trial = 1:n())
> df[df$trial==1 | df$event=="success",]
Source: local data frame [4 x 4]
Groups: user [2]
user event randNum trial
<fctr> <fctr> <dbl> <int>
1 john failed 4 1
2 john success 2 4
3 jane failed 9 1
4 jane success 5 3
If every user eventually succeeds and you want to consider the first and last row of every user try following code:
df<-split(df,df$user)
df<-lapply(df,function(x){
x<-rbind(head(x,1),tail(x,1))
x
})
df<-do.call("rbind",df)
From this, you will get the first fail and success of each user

find the dir_name who has less than 2 games played in Oracle 11g

For example we have this 2 tables
table1:
Dir_num | dir_name
10 | john
11 | vlad
table2:
game | dir_num
101 | 10
111 | 10
102 | 11
104 | 10
104 | 10
Now I try to find the dir_name who has less than 2 games played.
select * from table1 where dir_num ....
Please help me find pl/sql code.
The output should show
Vlad.
Try something like this -- use GROUP BY and HAVING:
SELECT t.dir_num, t.dir_name
FROM Table1 t
LEFT JOIN Table2 t2 ON t.dir_num = t2.dir_num
GROUP BY t.dir_num, t.dir_name
HAVING COUNT(DISTINCT t2.game) < 2
And here is the SQL Fiddle.
If you want those in both tables (meaning at least 1 game), then change LEFT JOIN to INNER JOIN.
Good luck.
Please try:
SELECT t.dir_num, t.dir_name
FROM Table1 t
INNER JOIN Table2 t2 ON t.dir_num = t2.dir_num
GROUP BY t.dir_num, t.dir_name
HAVING COUNT(DISTINCT t2.game) < 2

how to select and sort in sqlite and js

Hi I have the following table
buildingcode flatname flatdescription date
01 A 1 name 1 12-2012
01 B 2 name 1 12-2012
02 A 0 name 2 12-2012
01 A 1 name 1 11-2012
I want to display as follow
B 2 name 1
A 1 name 1
And to explain what I want to do:
display only buildingcode 01,
display once by flatname,
sort by flatname desc
What is the sqlite command to do this?
I try this but the order is wrong
'SELECT DISTINCT flatdescription, flatname, buildingcode FROM bill WHERE buildingcode = ? '
Please advice
SELECT flatname, flatdescription
FROM bill
WHERE buildingcode = '01'
ORDER BY flatname DESC

Resources