Two query in single result table?

2019-09-09 12:09发布

问题:

I am newbie in SQL Server 2008.There is a Proc.In this Proc, I have two select statements.When I execute my Proc, I will get results in two table which I accepted. But I want to this return in single table.

My Proc -

 ALTER PROC [GetPaymentGateway]  
 @CompanyID VARCHAR(3),  
 @ChannelType varchar(15)=null
AS  

IF @ChannelType='BODC' OR @ChannelType='BO-DC'  
BEGIN
    SELECT [card_name], [card_type], [BODC_Amount], [BODC_Amount_Type], PGM.PG_Type FROM credit_card_master CCM
    INNER JOIN PaymentGateway_master PGM
    ON PGM.payment_gateway_code = CCM.payment_gateway_code  
    WHERE CCM.company_id = @CompanyID and CCM.disabled = '1'

    SELECT PGM.Payment_Gateway_Name, PGNBC.BODC_Charge_Amt, PGNBC.BODC_Charge_type, PGM.PG_Type 
    FROM PG_NetBanking_Charges PGNBC
    INNER JOIN PaymentGateway_master PGM
    ON PGM.payment_gateway_code = PGNBC.payment_gateway_code
    WHERE PGNBC.company_id = @CompanyID
END

IF @ChannelType='B2C' OR @ChannelType='ONLINE-DC'  
BEGIN
    SELECT [card_name], [card_type], [charge_amount], [B2C_Amount_type], PGM.PG_Type FROM credit_card_master CCM
    INNER JOIN PaymentGateway_master PGM
    ON PGM.payment_gateway_code = CCM.payment_gateway_code 
    WHERE CCM.company_id = @CompanyID and CCM.disabled = '1' 

    SELECT PGM.Payment_Gateway_Name, PGNBC.Online_DC_Charge_Amt, PGNBC.Online_DC_Charge_type, PGM.PG_Type 
    FROM PG_NetBanking_Charges PGNBC
    INNER JOIN PaymentGateway_master PGM
    ON PGM.payment_gateway_code = PGNBC.payment_gateway_code
    WHERE PGNBC.company_id = @CompanyID
END 

Please suggest me how is it possible??

Thanks in advance.

回答1:

To compbine two queries in one table, you need the UNION operation. That takes two result sets, and basically glues them together.
Union has few restrictions, and the most important is that it's necessary that the queries have the same number of columns.

In you queries you have different number of columns selected, the credit_card_master queries have 5 columns each, and the PG_NetBanking_Charges queries have 4 columns each.

From what I can see, i guess that the card_type column from the first query has no equivalent in the second query, so you could rewrite the second query as:

SELECT card_name, card_type, charge_amount, B2C_Amount_type, PGM.PG_Type 
  FROM ...
  WHERE ...
UNION
SELECT PGM.Payment_Gateway_Name, null, PGNBC.Online_DC_Charge_Amt,
       PGNBC.Online_DC_Charge_type, PGM.PG_Type
  FROM ...
  WHERE ...

Also note that the columns in the result set will take the names of the columns from the first query, so you might want to add a column alias to get a more meaningful / generic name for the column. Also I usually add a "Source" column that enables me to trace the origin of the row in the union, so my final query would look like:

SELECT 1 as Source, card_name as Name, card_type as Type, 
       charge_amount as Ammount, B2C_Amount_type as AmmountType,
       PGM.PG_Type as PG_Type
  FROM ...
  WHERE ...
UNION
SELECT 2, PGM.Payment_Gateway_Name, null, PGNBC.Online_DC_Charge_Amt,
       PGNBC.Online_DC_Charge_type, PGM.PG_Type
  FROM ...
  WHERE ...

and the result will have the columns Source, Name, Type, Ammount, AmmountType and PG_Type, where Source will be 1 for rows from the first, and 2 for rows from the second query.