stored procedure for fetching data from multiple tables - stored-procedures

I have Stored procedure like this
Select k.HBarcode, m.make,t.plateno ,v.vtype ,l.locname,mdl.model,c.Colname
from transaction_tbl t,
KHanger_tbl k,
make_tbl m,
vtype_tbl v,
Location_tbl l,
Model_tbl mdl,
Color_tbl C
where t.tbarcode=#carid and
t.mkid=m.mkid and
v.vtid=t.vtid and
t.locid=l.locid and
mdl.mdlid=t.mdlid and
t.colid=c.colid and
t.transactID=k.transactID
while executing this am getting output
HBarcode make plateno vtype locname model Colname
34 BMW 44554 Normal Fashion Avenue 520 Red
I have two more tables, from transaction table I can get transactid (above ex:t.transactID),then I can get the corresponding
tid from "KHanger_tbl",then i want show uniquename for corresponding tid from "Terminal" table
1-KHanger_tbl
transactid HBarcode tid
--------------------------------------- ----------------------------------
19 34 7
22 002 5
21 1 7
23 200005 6
2- Terminals_tbl
tid UniqueName
----------- --------------------------------------------------
5 Key Room-1
6 Podium -1
7 Key Room - 2
Expected output
UniqueName HBarcode make plateno vtype locname model Colname
-------------------------------------------------------------------------------
KeyRoom-2 34 BMW 44554 Norma Fashion Avenue 520 Red
so how I can write stored procedure for this, if any one knows,please help me

Something like this maybe ?
Select term.UniqueName ,
k.HBarcode, m.make,t.plateno ,v.vtype ,l.locname,mdl.model,c.Colname
from transaction_tbl t,
KHanger_tbl k,
make_tbl m,
vtype_tbl v,
Location_tbl l,
Model_tbl mdl,
Color_tbl C
,
Terminals_tbl Term
where t.tbarcode=#carid and
t.mkid=m.mkid and
v.vtid=t.vtid and
t.locid=l.locid and
mdl.mdlid=t.mdlid and
t.colid=c.colid and
t.transactID=k.transactID
and
term.tid = t.transactID

Related

Why does SCAN/LAMBDA give unexpected results?

It is very possible that I dont understand the lambda logic or do I? I have dataset A2:A5 like:
1
3
6
10
If I do: =SCAN(0, A2:A5, LAMBDA(aa, bb, aa+bb)) i get:
1
4
10
20
If I do: =SCAN(0, A2:A5, LAMBDA(aa, bb, ROW(bb)-1)) I get
1
2
3
4
if I run: =SCAN(0, A2:A5, LAMBDA(aa, bb, (aa+bb)*(ROW(bb)-1))) the result is
1
8
42
208
Why there is 42 and 208 ? How this results in such values? How can it be 42 and 208 ?
Expected result is
1
8
30
80
And I can get it with:
=ArrayFormula(SCAN(0, A2:A5, LAMBDA(aa, bb, aa+bb))*(ROW(A2:A5)-1))
But not with
=SCAN(0, A2:A5, LAMBDA(aa, bb, (aa+bb)*(ROW(bb)-1)))
SCAN is a great intermediate results function. To understand how SCAN operates, you need to understand how REDUCE operates. The syntax is:
=REDUCE(initial_value, array, LAMBDA(accumulator, current_value, some_function()))
Going through =SCAN(0, A2:A5, LAMBDA(aa, bb, (aa+bb)*(ROW(bb)-1))) step by step,
A2:A5 is 1,3,6,10
Step 1:
aa = 0(initial_value)
bb = 1(current_value:A2)
Result((aa+bb)*(ROW(bb)-1)): (0+1)*(2-1)=1
Step 2:
aa = 1(accumulator(previous return value))
bb = 3(current_value:A3)
Result((aa+bb)*(ROW(bb)-1)): (1+3)*(3-1)=8
Step 3:
aa = 8(accumulator(previous return value))
bb = 6(current_value:A4)
Result((aa+bb)*(ROW(bb)-1)): (8+6)*(4-1)=42
Step 4:
aa = 42(accumulator(previous return value))
bb = 10(current_value:A5)
Result((aa+bb)*(ROW(bb)-1)): (42+10)*(5-1)=52*4=208
aa stores the result of the previous calculation, so you have:
above answers pretty much contain all so I will add only this:
you probably expected that by doing (aa+bb)*(ROW(bb)-1) you will get:
(aa+bb)
*
(ROW(bb)-1)
1
*
1
=
1
4
*
2
=
8
10
*
3
=
30
20
*
4
=
80
but that's not how it works. to get your expected result and by not using your formula where ROW is outside of SCAN:
=ArrayFormula(SCAN(0, A2:A5, LAMBDA(aa, bb, aa+bb))*(ROW(A2:A5)-1))
you would need to do:
=INDEX(MAP(SCAN(0, A2:A5, LAMBDA(aa, bb, (aa+bb))), ROW(A2:A5)-1, LAMBDA(cc, dd, cc*dd)))
where cc is the entire SCAN and dd is ROW(A2:A5)-1 eg. first do the running total and then multiplication, which is not so feasible length-wise.
or shorter but with SEQUENCE:
=MAP(SCAN(0, A2:A5, LAMBDA(aa, bb, (aa+bb))), SEQUENCE(4), LAMBDA(cc, dd, cc*dd))

QUERY in ARRAYFORMULA to FILTER, JOIN and LEN

Given a table like this one:
A
B
C
D
E
1
Tese
(2021/039-07964)
(8146.000336/2021-60)
Tema 1092
2
Afetado
Tema 1092
Controvérsia 251
|3|Tese|(2021/0390796-6)||Tema 1093|Controvérsia 258|
|4|Afetado|||Tema 1093|Controvérsia 258|
|5|Tese|(2021/0390796-7)|(8146.000338/2021-50)||Controvérsia 238|
As long strings in Col A's rows are equal to "Tese", Col F should return a string (with line breaks) that compiles Col B to D strings that are not null in the same row.
So, row 1 of Col F should be:
F
(2021/0390796-4)(8146.000336/2021-60)Tema 1092
Row 2 of Col F should be:
F
(2021/039-07966)Tema 1093Controvérsia 258
And so on
I'm using a dropdown formula to do the job, which is working fine:
=IFERROR(IF(A1="Tese",JOIN(CHAR(10),FILTER(B1:E1, LEN(B1:E1))),""))
But it would be easier if it could be replaced by an array formula (the table is over 4,000 rows!).
I think it is possible to use a query, but I could not write the formula down.
The real table is here: link
Clear all contents of column G and try in G1
={"Informações completa da tese"; INDEX(IF(A2:A="Tese"; SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(SUBSTITUTE(B2:F; " "; "$"));;ROWS(A2:A)))); " "; char(10)); "$"; " ");))}
See if that helps?

How to join two table with a conditon

TABLO1
Inst_count Inst_Type Card_Group Txn_Type Comm_rate
---------- --------- ---------- -------- ---------
2 N -1 -1 1,70
2 N 36 -1 1,71
2 N 37 -1 1,72
2 V -1 -1 1,73
2 V 36 -1 1,74
2 V 37 -1 1,75
TABLO2
Inst_count Inst_Type Card_Group Txn_Type Isk_rate Day
---------- --------- ---------- -------- -------- ---
2 N -1 -1 1,0 10
2 N 36 -1 1,1 11
2 N 37 -1 1,2 12
2 V -1 -1 1,3 13
2 V 36 -1 1,4 14
2 V 38 -1 1,5 15
Result_Table
Inst_count Inst_Type TABLO1.Card_Group TABLO2.Card_Group TABLO1.Txn_Type TABLO2.Txn_Type Isk_rate Day Comm_rate
---------- --------- ----------------- ----------------- --------------- --------------- -------- --- ---------
2 N -1 -1 -1 -1 1,0 10 1,70
2 N 36 36 -1 -1 1,1 11 1,71
2 N 37 37 -1 -1 1,2 12 1,72
2 V -1 -1 -1 -1 1,3 13 1,73
2 V 36 36 -1 -1 1,4 14 1,74
2 V -1 38 -1 -1 1,5 15 1,73
2 V 37 -1 -1 -1 1,3 13 1,75
Inst_count and Inst_Type must be always equal.
But Card_Group and Txn_Type columns must be match in the other table with default value(-1) if not exist.
How can write this sql?
Thanks.
Here we are using OUTER JOIN which sets all your tables values into one resulset(You can also use UNION ALL for this purpose). Then we are using a left join for each table to make sure each column will have its values which absence of other. Now you can try this with your own query. If you can not achieve your goal then i suggest you to gather your samples into dbfiddle link. Then we can make further explanations.
SELECT DISTINCT NVL (A.COL1, A1.COL1) COL1A,
NVL (B.COL1, B1.COL1) COL1B,
NVL (A.COL2, A1.COL2) COL2,
NVL (A.COL3, A1.COL3) COL3,
NVL (B.COL3, B1.COL3) COL4
FROM TABLEA A
FULL OUTER JOIN TABLEB B ON B.COL1 = A.COL1
LEFT OUTER JOIN TABLEB B1 ON B1.COL1 = -1
LEFT OUTER JOIN TABLEA A1 ON A1.COL1 = -1

Inverting table

This is the table I have:
A B
1 Title1 | Title
2
3 0 | # of teachers
4
5 11 | # of students
6
7 Not active | Active?
8
9
10
11 Title2 | Title
12
13 3 | # of teachers
14
15 5 | # of students
16
17 Not active | Active?
18
19
20
21 Title3 | Title
22
23 10 | # of teachers
24
25 22 | # of students
26
27 Not active | Active?
I'd like to "invert" it in another sheet to have Title, # of teachers, # of students and Active? as headers and then the values under the right column (each entry in a separate row).
I was trying to use MATCH without much luck..
This retrieves just the first Title (every time):
=index(SheetWithTable!$A:$A,match("Title",SheetWithTable!$B:$B,0))
Please copy your sheet and in that copy select B1:B7, Copy, Paste special into D1 with Paste transpose. In D3, copied across to J3 and down to suit:
=index($A1:$A11,match(D$1,$B1:$B11,0))
Select all, Copy, Paste special, Paste values only. Filter Column B to deselect # of teachers only and delete all rows but Row 1. Clear filter. Delete Columns I, G, E, C, B, A.
Try the following formula in cell D1:
={{B1,B3,B5,B7};{filter(A:A,B:B=B1),filter(A:A,B:B=B3),filter(A:A,B:B=B5),filter(A:A,B:B=B7)}}
Or you can try:
={{"Title","# of teachers","# of students","Active?"};{filter(A:A,B:B="Title"),filter(A:A,B:B="# of teachers"),filter(A:A,B:B="# of students"),filter(A:A,B:B="Active?")}}
Have a look at the following screenshot:
If you have the 3 titles in a table, you can rotate it:
rotateArray = function(array) {
var newArray = [];
for (var i = 0; i < array[0].length; i++) {
newArray[i] = [];
for (var j = 0; j < array.length; j++) {
newArray[i].push(array[j][i]);
}
}
return newArray;
};
http://dtab.io/sheets/560b8efd6faeb39d2a70ad1e

Remove rows that are near duplicate of previous row in Deedle dataframe

I have a Deedle Data frame that looks like this.
val it : Frame<int,string> =
Date size1 size2
13 -> 2013-12-12T00:00:00.103336Z 133 35
14 -> 2013-12-12T00:00:00.105184Z 83 35
15 -> 2013-12-12T00:00:00.107205Z 83 35
16 -> 2013-12-12T00:00:00.109566Z 83 34
17 -> 2013-12-12T00:00:00.115260Z 83 34
18 -> 2013-12-12T00:00:00.133546Z 83 34
20 -> 2013-12-12T00:00:00.138204Z 82 34
22 -> 2013-12-12T00:00:00.140125Z 81 34
I would like to remove rows that have the same values for both size1 and size2 as the previous row. In pseudo code...
if row?size1 = prevRow?size1 && row?size2 = prevRow?size2 then dropRow
So in the example above I would end up with:
val it : Frame<int,string> =
Date size1 size2
13 -> 2013-12-12T00:00:00.103336Z 133 35
14 -> 2013-12-12T00:00:00.105184Z 83 35
16 -> 2013-12-12T00:00:00.109566Z 83 34
20 -> 2013-12-12T00:00:00.138204Z 82 34
22 -> 2013-12-12T00:00:00.140125Z 81 34
I believe I want to use
Frame.filterRowValues(row - > )
But I don't see how to compare one row against the previous row. Is there a simple way to do this? Perhaps I need to shift and join?
This can be done using a number of ways and I'm not quite sure which is the best one:
Use shift and join (as you say) would certainly work - you'd need to rename the columns in one of the frames so that you can join them, but it sounds like quite a good solution to me
You can use frame.Rows |> Series.pairwise to get tuples containing the current and the previous row, then use Series.filter and Series.map (to select the second row from the tuple) and re-construct frame using Frame.ofRows. The only issue is that you'll always lost the first row this way (and you'll have to add it back).
You can use Frame.filter and find the previous row. The recent release supports Lookup.Smaller which lets you do that easily.
The code for the third option looks like this (note that the frame rows need to be ordered frame.Rows.IsOrdered = true) for this to work:
frame |> Frame.filterRows (fun k row ->
let prev = frame.Rows |> Series.tryLookup k Lookup.Smaller // New in v1.0
match prev with
| Some prev -> prev?Something <> row?Something
| _ -> true (* always return true for the first row *) )

Resources