自定义列需要SQL查询帮助

时间:2013-08-23 14:38:26

标签: sql-server join union

表结构如下:

Sales: Sales_id, sDate,ccode,qty,amt;     
Challan: Ch_id, ch_date,qty,amt;

所需信息是:

Id | Date | CCode | Type | Qty | Amt

If sales then type = sales; if challan then type = challan ; order by date

我尝试了以下内容:

select s.cust_code,s.cust_name,s.sales_qty,s.sales_amt,c.cust_code,
c.cust_name,c.challan_qty ,c.challan_amt 
from sales s inner 
join challan c on s.sales_date  = c.challan_date

如何在特定日期放置类型的自定义列并显示challan或sales?

1 个答案:

答案 0 :(得分:2)

看来你正在寻找工会声明:

select 
  Sales_id as Id, sDate as [date], ccode as CCode, 
  'sales' as Type, qty as Qty, amt as Amt
From Sales
Union
select
  Ch_id as Id, ch_date as [date], Null as CCode, 
  'Challan' as Type, qty as Qty, amt as Amt
from Challan