【问题标题】:SQL Query Help Needed with custom column自定义列需要 SQL 查询帮助
【发布时间】:2013-08-23 14:38:25
【问题描述】:

表结构如下:

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

我应该如何放置类型的自定义列并在特定日期显示挑战或销售?

【问题讨论】:

  • 如何判断是销售还是挑战?

标签: sql-server join union


【解决方案1】:

看来你在找联合声明:

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

【讨论】:

  • 即使他试图联合,这个查询也是错误的,因为它会在两种情况下都将 Sales 显示为一种类型。
猜你喜欢
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 2016-07-04
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
相关资源
最近更新 更多