How to merge these queries that insert columns to table

sql merge two tables with same columns
sql merge two tables with different fields
how to combine two select queries in sql with different columns
excel merge two tables based on one column
how to retrieve data from two tables with one sql statement
merge statement in sql w3schools
sql join
power query combine tables with different columns

I have selected these data using cursor. And like this, I can insert that data each one. I need to merge that queries in only one insert command.

CURSOR moc_local_call_records
      IS
      SELECT RECIPIENT, COUNT(*) N_CNT
      FROM (SELECT RECIPIENT,
                   SERVICE_TYPE,
                   MPS_SWTCH_CD,
                   MPS_FTR_CD1
            FROM ABA_RM_INB_USAGE
            WHERE SERVICE_TYPE = 100
               OR SERVICE_TYPE = 0
              AND MPS_SWTCH_CD = 'T11'
              AND MPS_FTR_CD1 = 1033)
      GROUP BY RECIPIENT;

CURSOR moc_local_home_records
      IS
      SELECT RECIPIENT, COUNT(*) N_CNT
      FROM (SELECT RECIPIENT,
                   SERVICE_TYPE,
                   MPS_FTR_CD1
            FROM ABA_RM_INB_USAGE
            WHERE SERVICE_TYPE = 100
               OR SERVICE_TYPE = 0
              AND MPS_FTR_CD1 = 1031)
      GROUP BY RECIPIENT;

And like this i can insert that data each one.

FOR mocLocalCallRecords IN moc_local_call_records
      LOOP
        IF mocLocalCallRecords.N_CNT > 0 THEN
          INSERT INTO TRAFFIC_PREPROCESS_INB(TADIG, MOC_LOCAL_CALL_RECORDS)
          VALUES (mocLocalCallRecords.RECIPIENT, mocLocalCallRecords.N_CNT);
        END IF;
    END LOOP;

FOR mocLocalHomeRecords in moc_local_home_records
      LOOP
        IF mocLocalHomeRecords.N_CNT > 0 THEN
          INSERT INTO TRAFFIC_PREPROCESS_INB(moc_local_home_records)
          VALUES (mocLocalHomeRecords.N_CNT);
        end if;
end loop;

I need to merge that queries in only one insert command. Give me some advice, I'm beginner here by the way

why you are using cursor for this ? you easily can insert with 1 insert command like that :

INSERT INTO TRAFFIC_PREPROCESS_INB(TADIG, MOC_LOCAL_CALL_RECORDS)
select RECIPIENT, N_CNT 
from (
SELECT RECIPIENT, COUNT(*) N_CNT
      FROM (SELECT RECIPIENT,
                   SERVICE_TYPE,
                   MPS_SWTCH_CD,
                   MPS_FTR_CD1
            FROM ABA_RM_INB_USAGE
            WHERE SERVICE_TYPE = 100
               OR SERVICE_TYPE = 0
              AND MPS_SWTCH_CD = 'T11'
              AND MPS_FTR_CD1 = 1033)
      GROUP BY RECIPIENT
      HAVING COUNT(*) > 0
union all
SELECT NULL as RECIPIENT, COUNT(*) N_CNT
FROM (SELECT RECIPIENT,
             SERVICE_TYPE,
             MPS_FTR_CD1
      FROM ABA_RM_INB_USAGE
      WHERE SERVICE_TYPE = 100
           OR SERVICE_TYPE = 0
          AND MPS_FTR_CD1 = 1031)
 GROUP BY RECIPIENT
 HAVING COUNT(*) > 0
  )X

i hope it helps

Combine results from several SQL tables, How do you combine results from more than one SQL table? mind that the end result will be to either add more columns to a result, perhaps from another related table, JOIN – You can use joins to combine columns from one or more queries into one result. Second, the data types of these columns must be compatible. I have selected these data using cursor. And like this, I can insert that data each one. I need to merge that queries in only one insert command. CURSOR moc_local_call_records IS SELECT

You can directly insert it like this if you are not performing any transformations in between:

INSERT INTO TRAFFIC_PREPROCESS_INB(TADIG, MOC_LOCAL_CALL_RECORDS,MOC_LOCAL_HOME_RECORDS)
SELECT RECIPIENT, COUNT(*) N_CNT, NULL
      FROM (SELECT RECIPIENT,
                   SERVICE_TYPE,
                   MPS_SWTCH_CD,
                   MPS_FTR_CD1
            FROM ABA_RM_INB_USAGE
            WHERE SERVICE_TYPE = 100
               OR SERVICE_TYPE = 0
              AND MPS_SWTCH_CD = 'T11'
              AND MPS_FTR_CD1 = 1033)
      GROUP BY RECIPIENT
      HAVING COUNT(*) > 0;

UNION ALL 

SELECT NULL, NULL, COUNT(*) N_CNT
  FROM (SELECT RECIPIENT,
               SERVICE_TYPE,
               MPS_FTR_CD1
        FROM ABA_RM_INB_USAGE
        WHERE SERVICE_TYPE = 100
           OR SERVICE_TYPE = 0
          AND MPS_FTR_CD1 = 1031)
  GROUP BY RECIPIENT
  HAVING COUNT(*) > 0;

Merge Tables in Excel Using Power Query (Easy Step-by-Step Guide), In this tutorial, I will show you how to merge two or more tables in Excel. Also, note that there should be no repetition in these connecting columns. Note: Power Query can be used as an add-in in Excel 2010 and 2013, and is an inbuilt� I have 2 tables: Table [A] and TimeZone. How do I add another colum in [A] that will use a column a vlaue in the TimeZone table without using a Merge Query? The Timezone table only contains one value which is the number of hours to offset. Ideally I would like to use this value as a parameter but have not figured out how to do it.

Both your queries are very similar with little difference of predicates in where clause. Since you are grouping by RECIPIENT and want a Count(*), they could both be merged into a much simpler query, like so

SELECT RECIPIENT, COUNT(*) N_CNT       
FROM ABA_RM_INB_USAGE
WHERE SERVICE_TYPE IN (100, 0)    
  AND ((MPS_FTR_CD1 = 1033 AND MPS_SWTCH_CD = 'T11')      
        OR MPS_FTR_CD1 = 1031)  
GROUP BY RECIPIENT

Further, you want to insert those records from this SELECT for which COUNT(*) is greater than zero in another table. This could be achieved with a single INSERT statement without use of cursor hence with better performance, like so

INSERT INTO TRAFFIC_PREPROCESS_INB
(TADIG, MOC_LOCAL_CALL_RECORDS)
SELECT RECIPIENT, COUNT(*) N_CNT       
FROM ABA_RM_INB_USAGE
WHERE SERVICE_TYPE IN (100, 0)    
  AND ((MPS_FTR_CD1 = 1033 AND MPS_SWTCH_CD = 'T11')      
        OR MPS_FTR_CD1 = 1031)  
GROUP BY RECIPIENT
HAVING N_CNT > 0  

MySQL combine two columns and add into a new column, This way, if the data in the fields changes, you don't have to update your combined field Add new column to your table and perfrom the query: To create a query for that source, start by going to the Data (or Power Query) tab and selecting From Table/Range. On the Home tab of the Ribbon, select Merge Queries. This brings up the Merge window. First, in the top part, you can select the column that you want to use for merging.

Excel: Merge tables by matching column data or headers, Power Query can merge two tables by matching one or several columns. The source tables In Excel 2010 and Excel 2013, it can be downloaded as an add- in. The detailed guidance can be found in this tutorial: How to join tables with Excel Power Query. As you said, you want to combine from these 3 tables only specific columns in a new table but to do it in the Query Editor instead of using DAX function “ union and selectedcolumns”. Here is a simple example to do this. Select the first two columns from each table of the three tables(eg. From excel) and add them into a new table. Table1. Table2

How to join tables in Excel: Power Query vs. Merge , Select the columns to add from the second table. At this point, you have a table resembling the one in the screenshot below. To complete the� Combine Merge1 with your third table (Commissions) by performing these steps (Data tab > Get Data > Combine Queries > Merge). The screenshot below shows my settings: Clicking OK in the Merge dialog box opens the Power Query Editor , where you select the columns to be added from table 3.

Merge columns (Power Query) - Excel, Select two or more columns that you need to merge. Press the CTRL key, and then click on the column headers to select each of the columns that you'll include in the merge. NOTE: The order in which you select the columns sets the order of the values in the merged column. In the first step of the Tables Merge wizard, please select the main table and lookup table separately, (Note: the column data in lookup table will be added to the main table), see screenshot: 3. In the step 2 of the Tables Merge wizard, please check the key column name that you want to merge the tables based on, see screenshot: 4.

Comments
  • Which RDBMS is this for? Please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely.
  • ORACLE PL/SQL ----------------------------------------------------------------
  • Well then please add that oracle tag to your question!
  • And how to insert next one ?
  • Same way. Insert into traffic_preprocess_inb select n_cnt from (your query)
  • Did this help you..??
  • Actually no :( I need to insert filtered data to table column by column that's my problem. Then I can insert that table into another table directly.
  • if you need to insert data into two tables, you should have two insert queries. The condition of count > 0 is performed by 'HAVING' clause and so hopefully the updated answer should suffice your requirements.