Data returned from Oracle Proc in vb.net - oracle

I have a VB.Net function which executes a Oracle Stored Proc and returns a dataset.
Below is a list of sample records
OrderID OrderDetail Qty Date Supplier Price
1 Books 10 10-Aug-08 ABC Inc 100.00
1 Pens 20 10-Aug-08 ABC Inc 300.00
2 Keys 1 20-Aug-09 Blue cross 100.00
2 Nots 3 30-Aug-09 Blue Cross 200.00
The above records are returned as a dataset in my function.
Using the above dataset int two different functions how can I return data as shown below.
First function should return only distinct orderID records
Second function should take OrderID as input and return records based on orderID
Any suggestions?
Thanks

For the select distinct, check out Select DISTINCT on DataTable. For the other, use the DataTable.Select method. Assuming that the DataTable is the first in the DataSet ...
var dt = ds.Tables[0];
var rows = dt.Select( "OrderID = 1" );
(Sorry, I don't know any VB :)

Related

Laravel Eloquent query on first relation of one to many

I have Two Tables A and B
A can have more than B
the relation between them is that B has A_id
columns in B have A_id and date
I have a filter with status (status alpha = first B of A date is less than 30 days,beta = first B of A date is less than 60)
when trying to get the status I want to query on the first row in B which has A_id
public function alpha(){
return $this->hasOne("App/B")->where('date','<',now()->subMonth(1)->toDateString())->where('date','>',now()->subMonth(2)->toDateString());
}
public function beta(){
return $this->hasOne("App/B")->where('date','<',now()->subMonth(2)->toDateString());
}
This seems to get the job done but failed in specific situation
imagine table B has two rows
id = 1 A_id=1 and date is 45 days old
id =2 A_id=1 and date is 70 days old
This should be an alpha since I'm only interested in the First date
but the query I've written returns on both alpha and beta
how can I fix my query to only check the first occurrence only
mind that I can't loop over A's after getting them since I'm using pagination

Different number of rows for different columns per page

I have a SSRS report in which there are 3 columns each contain 3 different subreports in a table. Requirement is 1st subreport column should return 27 rows, 2nd : 25 rows and 3rd:26 rows. Is it possible in SSRS ? If yes How ?
You can do this.. using row_number and Mod.
I'm simply generating a list of numbers from 1 - 100 below.. lets assume that this is your dataset. Create a new column using row_number and partition it by mod 25 (27 or 26 as you require) against this dataset. Now you have a unique value every X number of rows..
declare #start int = 1
declare #end int = 100
;with mycte as (
select distinct n = number
from master..[spt_values]
where number between #start and #end
)
Select
*
,ROW_NUMBER() OVER (PARTITION BY (mycte.n % 25) ORDER BY (n) )rn
from mycte
order by 1,2
Now in SSRS, against each subreport add this column, add a parent group, grouping by this newly generated row number (RN in this case). Remove any columns that SSRS adds after grouping, but keep the grouping..
Set the group property to pagebreak in between each instance of the groups.. Done!

Alternativ to multiple AND conditions in oracle

I know there is IN as alternative to multiple ORs:
select loginid from customer where code IN ('TEST1','TEST2','TEST3','TEST4'))
This will return all loginids with code that mach any of the four TEST elements.
Is there something similar for AND? I will need to find out all loginids that have code: TEST10,TESTA,TEST1,TESTB,AIFK,AICK....(there are 20 codes)
You cannot compare that. With ORs or IN you look for records that match one of the values. With AND you would look for a record where the column matches all those values, but this field can of course only hold one value, so will never find any record.
So obviously you are looking for something entirely else. Probably you want to aggregate your data, to find IDs for which records for each of the values exist. This would be:
select loginid
from customer
where code IN ('TEST1','TEST2','TEST3','TEST4')
group by loginid
having count(distinct code) = 4;
You could do the following:
SELECT loginid
FROM customer
WHERE code IN ('TEST1', ... , 'TEST20')
GROUP BY loginid
HAVING COUNT(DISTINCT code) = 20;
The difference between Jon's answer and this one is that if you use other codes in the table, my query will return all loginids for which there are rows for these 20 codes, and Jon's answer will return all loginids for which there are 20 distinct codes.
No
A short answer, I know, but sometimes it's the only real answer.
However
Depending on what exactly you're trying to achieve, you may be able to use a count of a query grouping the items, to check that all match.
This assumes that your CustomerCodes are kept in a separate relational table.
SELECT loginID
FROM Customer
WHERE loginID IN (
SELECT loginID, count(*) as codeCount
FROM CustomerCodes
GROUP BY loginID
HAVING codeCount = 20
)
It doesn't work if you have Code1, Code2 Code3 etc fields... you'd have to split the data out into a separate table, for example:
loginID | code
---------------
1 | code1
1 | code2

PL/SQL select returns more than one line

I'm embarrassed to admit this is a totally noob question - but I take shelter in the fact that I come from a T-SQL world and this is a totally new territory for me
This is a simple table I have with 4 records only
ContractorID ProjectID Cost
1 100 1000
2 100 800
3 200 1005
4 300 2000
This is my PL SQL function which should take a contractor and a project id and return number of hours ( 10 in this case )
create or replace FUNCTION GetCost(contractor_ID IN NUMBER,
project_ID in NUMBER)
RETURN NUMBER
IS
ContractorCost NUMBER;
BEGIN
Select Cost INTO ContractorCost
from Contractor_Project_Table
where ContractorID= contractor_ID and ProjectID =project_ID ;
return ContractorCost;
END;
But then using
select GetCost(1,100) from Contractor_Project_Table;
This returns same row 4 times
1000
1000
1000
1000
What is wrong here? WHy is this returning 4 rows instead of 1
Thank you for
As #a_horse_with_no_name points out, the problem is that Contractor_Project_Table has (presumably) 4 rows so any SELECT against Contractor_Project_Table with no WHERE clause will always return 4 rows. Your function is getting called 4 times, one for each row in the table.
If you want to call the function with a single set of parameters and return a single row of data, you probably want to select from the dual table
SELECT GetCost( 1, 100 )
FROM dual
Because you have 4 rows in Contractor_Project_Table table. Use this query to get one record.
select GetCost(1,100) from dual;

Need Only Differing column values from two DataTables

I have two DataTables in which I need only differing column values in a final DataTable as shown below
DataTable(DT1)
ID Name Salary
1 ABC 2000
2 XYZ 4000
3 Suresh 6000
DataTable(DT2)
ID Name Salary
1 ABC 3000
2 XYZ 5000
3 Suresh 6000
DataTable(DT3)
ID Salary(DT1) Salary(DT2)
1 2000 3000
2 4000 5000
How can I accomplish this using linq queries on Datatables or by any other way? I tried looping and comparing each column values both datatables, but I'm not getting how to store in final DataTables.
First we need to join the tables (assuming the ID's have a PK/FK relationship), Then compare the values and place them in a new table.
var unMatchedRows = from DataRow dt1Row in dt1.Rows
join DataRow dt2Row in dt2.Rows
on dt1Row["id"] equals dt2Row["id"]
where Convert.ToDoubl(dt1Row["salary"]) != Convert.ToDouble(dt2Row["salary"])
select new { dt1Row, dt2Row };
This gives us a collection of DataRow objects joined on the ID where the salary's are different. Now we just need to populate a new DataTable with the rows.
foreach (var m in unMatchedrows)
{
dt3.ImportRow(m);
}
It is worth noting that DT3 will need to have the same schema as the other two tables. And this will only place the unmatched rows in the new table. There are several ways to do this and I am not in front of an IDE right now to test it out, but this should get you going. Hope this helps!

Resources