match columns values qlikview - expression

In one table there is column product
Product
1233-901
3945-847
3956-234
3462
2940
2357
and in table 2 i have product column and data is
product
1233
3495
3456
3462
2940
2357
i want to match values of these column and get only first 4 characters .. so i tried this
first i load this in tab1
LOAD
left(Product,5) as Product_code,
FROM
F2.xlsx
(ooxml, embedded labels, table is PM1);
and in second tab i load this
Load Product FROM
F3q.xlsx
(ooxml, embedded labels, table is PM2);
but i did not get data ... i get data like this
Product_code Product
1233 -
3945 -
3956 -
3462 3462
2940 2940
2357 2357
- 1233
- 3495
- 3456
where as i am trying to get data like this
Product_code Product
1233 1233
3945 3495
3956 3496
3462 3462
2940 2940
2357 2357

You need to:
trim(left(Product, 5))
and probably use
INNER JOIN(Table1)
LOAD ...
you can share your dataset so I will take a look

Related

Get closest date with id and value Oracle

I ran into a problem and maybe there are experienced guys here to help me figure it out:
I have a table with rows:
ID
VALUE
DATE
2827
0
20.07.2022 10:40:01
490
27432
20.07.2022 10:40:01
565
189
20.07.2022 9:51:03
200
1
20.07.2022 9:50:01
731
0.91
20.07.2022 9:43:21
161
13004
19.07.2022 16:11:01
This table has a million records, there are about 1000 ID instances, only the date of the value change and, therefore, the value itself changes in them.
When the value of the ID changes is added to this table:
ID | Tme the value was changed (DATE) | VALUE
My task is to get the all id's values closest to the input date.
I mean: if I input date "20.07.2022 10:00:00"
I want to get each ID (1-1000) with rows "value, date" with last date before "20.07.2022 10:00:00":
ID
VALUE
DATE
2827
0
20.07.2022 9:59:11
490
27432
20.07.2022 9:40:01
565
189
20.07.2022 9:51:03
200
1
20.07.2022 9:50:01
731
0.91
20.07.2022 8:43:21
161
13004
19.07.2022 16:11:01
What query will be the most optimal and correct in this case?
If you want the data for each ID with the latest change up to, but not after, your input date then you can just filter on that date, and use aggregate functions to get the most recent data in that filtered range:
select id,
max(change_time) as change_time,
max(value) keep (dense_rank last order by change_time) as value
from your_table
where change_time <= <your input date>
group by id
With your previous sample data, using midnight this morning as the input date would give:
select id,
max(change_time) as change_time,
max(value) keep (dense_rank last order by change_time) as value
from your_table
where change_time <= timestamp '2022-07-28 00:00:00'
group by id
order by id
ID
CHANGE_TIME
VALUE
1
2022-07-24 10:00:00
900
2
2022-07-22 21:51:00
422
3
2022-07-24 13:01:00
1
4
2022-07-24 10:48:00
67
and using midday today woudl give:
select id,
max(change_time) as change_time,
max(value) keep (dense_rank last order by change_time) as value
from your_table
where change_time <= timestamp '2022-07-28 12:00:00'
group by id
order by id
ID
CHANGE_TIME
VALUE
1
2022-07-24 10:00:00
900
2
2022-07-22 21:51:00
422
3
2022-07-28 11:59:00
12
4
2022-07-28 11:45:00
63
5
2022-07-28 10:20:00
55
db<>fiddle with some other input dates to show the result set changing.

Open a refcursor for a PL/SQL statement?

I'm creating a report for a business using Oracle and SSRS. The report requires me to aggregate contiguous ranges of serial numbers, which can consist of alphanumerics.
For example, say I have the following serials:
OPS114
OPS115
OPS116
OPS117
OPS145
OPS146
OPS160
890RPT
896RPT
897RPT
The report should have a single aggregate row for each contiguous range, with the count of each range, like so:
OPS114 - OPS117 (4)
OPS145 - OPS146 (2)
OPS160 - OPS160 (1)
890RPT - 890RPT (1)
896RPT - 897RPT (2)
I've pulled the data I need, and I'm bulk-collecting it into a table variable. Now, I need to aggregate the rows - this wouldn't be bad if I only needed to manipulate the data, but I need to have this available as a query for the refcursor. Can I open a refcursor for a PL/SQL FOR loop, or am I barking up the wrong tree? I've attempted to Google this, but the "cursor for loop" is not what I'm looking for. The alternative is to try to aggregate the results in SSRS using VB. (So either way, it won't be a good time.) I'm not sure if I have access to create a SQL table type for this, so this is the alternative I've sought.
If anyone has any experience with this, it would be greatly appreciated!
You could do this from a single SQL statement but you need to define the data better. Your column stores strings but you are using them as numbers to find out the range. And it seems the number part could either be before or after the string part.
If you are able to write some logic that separates out the numbers like this (and maybe keep the string part in another column)-
114
115
116
117
145
146
160
890
896
897
Then it reduces to a simple gaps and islands problem.
Step 1 - Select rownum along with this column (this would be a continuous sequence starting from 1)
Step 2 - Subtract rownum from this numeric data column.
Step 3 - Group by that result
Step 4 - Get min(numeric value) and max(numeric value) and count(numeric value) from the group which will be your result when combined as string.
Numeric_part Rownum Difference
------------ ------ ------------
114 1 113
115 2 113
116 3 113
117 4 113
145 5 140
146 6 140
160 7 153
890 8 882
896 9 887
897 10 887
Grouping this by Difference column, you get -
Difference Min(num) Max(num) count(num) Result
---------- --------- ---------- ---------- -----------------
113 114 117 4 114 - 117 (4)
140 145 146 2 145 - 146 (2)
153 160 160 1 160 - 160 (1)
882 890 890 1 890 - 890 (1)
887 896 897 2 896 - 897 (2)
That SQL statement can be used in PLSQL to return a cursor as in this link that #MickMnemonic has in the comments.
Basically -
OPEN cursor_variable FOR SELECT something FROM something...;
I spoke to a coworker about this and he had an idea that I've been able to implement.
I was able to create a pipelined function that handled my data selection and transforms for me; this allowed me to aggregate my rows as I needed to and only pass rows after a contiguous range was complete.
I am now calling that function from a procedure using the SELECT ... FROM TABLE(MYFUNCTION()) syntax. This allows me to get everything into a refcursor without much issue.
While this may not be performant (looping over a cursor, and manually aggregating), this is for a monthly report, and so I won't attempt to optimize until it's necessary (as we have other work to do).

Sorting Matrix Columns in RDLC Report

I get the following query result:
EmployeeName payelement payelementValue payelementOrder
------------ ---------- --------------- ---------------
emp1 PE1 122 2
emp1 PE2 122 1
emp2 PE1 122 2
emp2 PE2 122 1
emp3 PE1 122 2
emp3 PE2 122 1
Which results in a report that looks like:
Employee Name PE2 PE1
emp1 122 122
emp2 122 122
emp3 122 122
I have created a matrix in rdlc report and and put the column field with the ->'payelement ' and the value field with ->'payelementValue' and set the rows field with ->'employeeName ' the problem now is that I want to sort the 'payelement' upon the field named 'payelementOrder' which represents the order for paylements in their actual table while I actually get them sorted alphabetically by defualt i.e.(PE1 then PE2). Any help would be greatly appreciated.
I Solved by this...
Go to the .rdlc... Check the Row Groups(which we will find in the left-bottom) under of that we will find the grouped column name (which we are having in the tables) then right click on it-> Go to Group properties... -> Go to sorting-> on the sort by give the column name which you want to sort according to and Click Ok.
And You are Done....
When you created a matrix you got a Column group. In the group properties of the column group you can set order by specific field (payelementOrder in your case)

Aggregating the table from many rows to one

I have a table with data as follows:
Name opening receipt transfer closing
abcd 1000 40 30 1010
efg 256 109 219 146
hjk 9356 210 210 9356
mnp 2000
I need is a result like this -
opening_abcd receipt_abcd transfer_abcd closing_abcd opening_efg receipt_efg .....
1000 40 30 1010 256 109 .....
What I have tried till now is to create multiple views one by one having the one row values like -
select opening opening_abcd, receipt receipt_abcd, transfer transfer_abcd, closing closing_abcd
from this_table
where name = 'abcd'
and similarly for others and then I have merged them together by using JOIN.
Is there a solution possible if I can select all of them in one query or any other way which is better than mine?

How to consolidate overlap date in single

How would I get one row out of two overlaping dates rows from the same table for same id. I have more then 50000 records.
I have following sample data.
ID start_dt end_dt division
1212 04/01/2006 03/01/2007 second
1212 05/01/2009 01/01/2010 second
1212 04/01/2006 03/01/2008 second --- This should be selected as longest timeframe
1212 09/03/2007 03/01/2008 third
1213 05/03/2005 04/11/2009 second
1214 07/03/2007 03/01/2008 third
And the data I should get as following.
ID start_dt end_dt division
1212 04/01/2006 03/01/2008 second
1212 05/01/2009 01/01/2010 second
1213 05/03/2005 04/11/2009 second
1214 07/03/2007 03/01/2008 third
Thank you.
Ramu
Now that I understand your issue, just subtract the 2 dates to determine the time frame difference:
SELECT S.Id, S.Start_dt, S.End_dt, S.Division
FROM Sample S
JOIN (
SELECT S.Id, Max(S.end_dt-S.start_dt) as timeframe
FROM Sample S
GROUP BY S.Id ) S2 ON S.Id = S2.Id
AND S.end_dt-S.start_dt = s2.timeframe
Here is the Fiddle.
Good luck.

Resources