How to request a value from table A or B?

This is a Datagridview that I need to display on a screen like this.
(The Datagrid view is bound to the main table "(RECEIVE_PLAN) )

enter image description here

Concept Design Database:

If you want to import something into the warehouse, you must plan to receive it in a day. The acceptance plan is taken directly from the "Purchase Order" Or , when your warehouse has a reception schedule before the order.

There are three tables in the database, including Table A and Table B and Main Table .

They have such an attitude.

Note. the main table has two parameters to get the value to display on the screen

  • get "PO_LIST_NO" and "PO_NO" from the table (ORDER PURCHASE table).

  • get "PO_LIST_NO" and "PO_NO" from table B (table RECEIVE SCHEDULE), then get the value from table A.
    enter image description here

Important Terms

  • In the main table (RECEIVE PLAN) there should be a value in "PO_ID" or "RS_ID"

  • If the main table (RECEIVE PLAN) has a value in the PO_ID column, the RS_ID column must be NULL. On the other hand, if the main table (RECEIVE PLAN) is set to Column RS_ID, column PO_ID must be NULL

  • (RECEIVE PLAN) NULL PO_ID, RS_ID
  • (RECEIVE PLAN) PO_ID, RS_ID

, .

enter image description here

(PO_TRAN_ID) - PO_ID

(RS_TRAN_ID) RS_ID.

: A B?

, A B , .

enter image description here

datagridview.

BindingSource: (RECEIVE_PLAN)

"PO LIST NO": A (PURCHASE_ORDER)

"PO NO": A (PURCHASE_ORDER)

PLAN QTY: (RECEIVE PLAN)

+4
2

- ?

SELECT A_TABLE.PO_LIST_NO, A_TABLE.PO_NO, SUM(MAIN_TABLE.PLAN_QTY) FROM A_TABLE
INNER JOIN B_TABLE ON A_TABLE.PO_ID = B_TABLE.PO_ID
INNER JOIN MAIN_TABLE ON MAIN_TABLE.PO_ID = B_TABLE.PO_ID OR MAIN_TABLE.RS_ID = B_TABLE.RS_ID
WHERE (MAIN_TABLE.RS_ID IS NOT NULL OR MAIN_TABLE.PO_ID IS NOT NULL) AND NOT (MAIN_TABLE.RS_ID IS NOT NULL AND MAIN_TABLE.PO_ID IS NOT NULL) 
GROUP BY A_TABLE.PO_LIST_NO, A_TABLE.PO_NO
+1

, .

( SELECT   MAIN_TABLE.*,   A_TABLE.PO_LIST_NO, A_TABLE.PO_NO
  FROM          MAIN_TABLE  
                LEFT OUTER JOIN A_TABLE
                ON MAIN_TABLE.PO_ID = A_TABLE.TRAN_ID
  WHERE  (MAIN_TABLE.RS_ID IS NULL) 
)  

  UNION 


( SELECT     MAIN_TABLE.*   , A_TABLE.PO_LIST_NO , A_TABLE.PO_NO
  FROM          MAIN_TABLE  
                LEFT OUTER JOIN B_TABLE 
                ON MAIN_TABLE.RS_ID = B_TABLE .TRAN_ID
                             LEFT OUTER JOIN A_TABLE  
                             ON B_TABLE .PO_ID = A_TABLE.TRAN_ID
  WHERE      (MAIN_TABLE.PO_ID IS NULL)  
 )  
+1

Source: https://habr.com/ru/post/1606526/


All Articles