Oracle Recursion - c#

I want list all SOURCE_ID if the source has destination, the query below not work probably !!
select SOURCE_ID,
LINK_TYPE,
DESTINATION_ID
from LINK_TABLE
where link_table.link_type=1
and link_table.destination_is_deleted=0
START WITH link_table.source_id='100'
CONNECT BY PRIOR link_table.source_id=link_table.destination_id
Sample data
SOURCE_ID | DESTINATION_ID | LINK_TYPE| DESTINATION_IS_DELETED|
----------|-----------------|----------|-----------------------|
100 | 1500 | 1 | 0 |
100 | 1200 | 1 | 0 |
100 | 1300 | 1 | 1 |
1500 | 600 | 1 | 0 |
1500 | 700 | 1 | 0 |
700 | 88 | 1 | 0 |

Assuming you want to walk the hierarchy from the root nodes to the leaves this is what you need:
select SOURCE_ID,
LINK_TYPE,
DESTINATION_ID
from LINK_TABLE
where link_table.link_type=1
and link_table.destination_is_deleted=0
START WITH link_table.source_id='100'
CONNECT BY PRIOR link_table.destination_id = link_table.source_id
/
It's just a matter of swapping the referenced columns in the CONNECT BY PRIOR clause. Unfortunately the Oracle syntax is not intuitive here: I've been using it for over twenty years and I still have to test a query to make sure I've got them the right way round :)

I changed the query structure to solve the issue.
select SOURCE_ID,DESTINATION_ID,link_table.source_class,link_table.linktype,link_table.destination_class
from LINK_TABLE
where (link_table.source_id in( select link_table.DESTINATION_ID from link_table where link_table.source_id='100'and link_table.linktype=1 and link_table.destination_isdeleted=0 ))
and (link_table.linktype=1) or (link_table.source_id='100')

Related

How to combine Duplicate data into single Data MySql

I was wondering if what is this called or is this even possible in MySql, or what should I do to achieve this. This is what Mysql Table Looks Like:
Desired Output:
you can use a query like this:
SELECT customerid
, CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
FROM myitems
GROUP BY SUBSTRING_INDEX(item, ',', -1);
Sample
MariaDB [bernd]> SELECT * FROM myitems;
+----+------------+---------------+
| id | customerid | item |
+----+------------+---------------+
| 1 | 15 | 7pcs, Car |
| 2 | 15 | 2pcs, Car |
| 3 | 15 | 3pcs, Engine |
| 4 | 15 | 2pcs, Engine |
| 5 | 15 | 5pcs, Exhaust |
+----+------------+---------------+
5 rows in set (0.07 sec)
MariaDB [bernd]> SELECT customerid
-> , CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
-> FROM myitems
-> GROUP BY SUBSTRING_INDEX(item, ',', -1);
+------------+---------------+
| customerid | item |
+------------+---------------+
| 15 | 9pcs, Car |
| 15 | 5pcs, Engine |
| 15 | 5pcs, Exhaust |
+------------+---------------+
3 rows in set, 5 warnings (0.02 sec)
MariaDB [bernd]>
Your table structures wont allow (anything near as easy). A better format of your table might be more like
id customerid item qty
1 15 Car 7
2 15 Car 2
3 15 Engine 3
4 15 Engine 2
5 15 Exhaust 5
Then, you could get what you are looking for easily such as
select
t.customerid,
t.item,
sum( t.qty )
from
YourTable t
group by
t.customerid,
t.item
This WOULD give your your
customerid item qty
15 Car 9
15 Engine 5
15 Exhaust 5
Having a properly formatted table vs trying to have a description requires parsing numeric values vs rest of description is never a good solution such as what you have here. Too many opportunities for bad answers.

How to get SAP DDIC table structure in C#?

I'm writing some codes about extract table's structure from SAP using C#, and I've got a question about RFC_READ_TABLE, when I define a table's name and the FIELDS table returned 5 columns, like : FIELDNAME, OFFSET, LENGTH, T and FIELDTEXT, actually I couldn't know what's the decimal places of a numeric field according to these 5 columns, is there any other way to get this? Please be noted that my SAP account is a public one because other peoples are also using it, I'm not supposed to create new function module in SAP.
Below info was what I got from FIELDS table of module RFC_READ_TABLE:
MANDT | 000000 | 000003 | C | Client**: length * 3**
BUKRS | 000003 | 000004 | C | Company Code: **length * 3**
ANLN1 | 000007 | 000012 | C | Main Asset Number: **length * 3**
ANLN2 | 000019 | 000004 | C | Asset Subnumber: **length * 3**
GJAHR | 000023 | 000004 | N | Fiscal Year: **numeric(4,0)**
LNRAN | 000027 | 000005 | N | Sequence Number of Asset Line Items in Fiscal Year: **numeric(5,0)**
AFABE | 000032 | 000002 | N | Real depreciation area: **numeric(2,0)**
ZUJHR | 000034 | 000004 | N | Asset acquisition year (currently not used): **numeric(4,0)**
ZUCOD | 000038 | 000004 | N | Sub-classification of asset acquisitions(currently not used): **numeric(4,0)**
AUFWV | 000042 | 000013 | P | Proportional cumulative revaluation on replacement value: **numeric(13,??=2)**
The datatype of this field is numeric(13,2) according to DB, but how can I get this clue?
thanks in advance!
RFC_READ_TABLE FM returns only summary of field. You can use DDIF_FIELDINFO_GET FM for details.

Displaying customer bi-weekly from start date for this week in SQL

I'm trying to figure out how to start with this. So each customer should be display bi-weekly starting from the start date. I want the current week to display those that should be display for this week.
Here's my table.
Customers_Id(PK)|First|Last | Address | Phone | Start_Date | Tech_Id (FK) |
----------------+-----+------+--------------+---------+-------------+---------------+
1 | Bob |Smith | 123 Fake St. | 3298492 | 8/4/2010 | 1 |
2 | John|Man | 123 Noe St. | 2930482 | 4/15/2008 | 1 |
3 | Tom |Lee | 123 Polk St. | 9308523 | 6/21/2012 | 2 |
Hopefully this is clear enough.
in mysql, you can do something like:
SELECT * FROM Customers WHERE WEEK(NOW())=WEEK(Start_Date)
This is the answer I have.
SELECT *
FROM Customers
Where DATEDIFF (ww, Start_Da, GETDATE())%2 = 0

how to dynamically create multiple select statement and store it in a datatable

the case here is the number of columns i would like to store to the datatable depends on the number of available data. i would need a multiple select statements to collect all the data.
select test_result.stud_id,test_info.max_score,
test_result.test_score
from test_result left join test_info
on test_result.test_info_id = test_info.test_info_id
where test_info.test_type_id = 1 and test_info.test_num = 1;
i would have to repeat this code over and over again until the test_num reaches the current maximum count. i was thinking of looping this code while storing it to the datatable.this is what i would like the datagridview would display.
|Quiz Number | Quiz#1 | Quiz #2 | Quiz #3 | Quiz #4 | Quiz #5 |
|Max score | 20 | 25 | 30 | 15 | 15 |
|student 1 | 18 | 22 | 25 | 12 | 14 |
|student 2 | 19 | 20 | 25 | 11 | 13 |
|student 3 | 20 | 24 | 20 | 12 | 11 |
the display of data would be in rows. so each row would require a different select statement in order to display the required data.
Something like below will woek for you
select
test_result.stud_id,
max(case when test_result.test_info_id =1 then test_result.test_score end) as quiz_1,
max(case when test_result.test_info_id =2 then test_result.test_score end) as quiz_2,
max(case when test_result.test_info_id =3 then test_result.test_score end) as quiz_3,
.
.
.
from test_result left join test_info
on test_result.test_info_id = test_info.test_info_id
where test_info.test_type_id = 1 and test_info.test_num = 1
group by test_result.stud_id;

Sql- ordering based on two columns in a table

consider a database having
checkID record_number data Order_number
a 0 1 2
a 1 2 0
a 2 3 1
a 3 4 3
Find a query which fetches the records according to Order_number column i.e.procedure should return data :
having record _number 2 then
having record _number 0 then
having record _number 1 then
having record _number 3
Is there any join or other query for this?
SELECT *
FROM tablename
ORDER BY CASE WHEN record_number = 2 THEN 0 ELSE 1 END, record_number;
SQL Fiddle Demo
This will give you your data in the following order:
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
select *
from YourTable
order by
case record_number
when 2 then 1
when 0 then 2
when 1 then 3
when 3 then 4
end
You can use CASE WHEN on this situation.;
SELECT *
FROM tbl
ORDER BY CASE record_number
WHEN 2 THEN 1
WHEN 0 THEN 2
WHEN 1 THEN 3
WHEN 3 THEN 4
END
Here is SQL Fiddle DEMO.
| CHECKID | RECORD_NUMBER | DATA | ORDER_NUMBER |
-------------------------------------------------
| a | 2 | 3 | 1 |
| a | 0 | 1 | 2 |
| a | 1 | 2 | 0 |
| a | 3 | 4 | 3 |
The best solution would be to create additional table for sorting checks, like this:
record_number sort_seq
2 1
0 2
1 3
3 4
then join it with your table
select c.*
from check as c
join sortSeq as s on c.record_number=s.record_number
order by s.sort_seq
SELECT *
FROM tablename
ORDER BY Order_number

Categories

Resources