sql查询获取特定行

时间:2016-07-11 09:06:24

标签: sql sql-server database

我有一个样本呼叫中心系统的5个表

表是

  • 员工(身份证 - 姓名)
  • 号码(ID - 号码)
  • Call_Log(ID - EmployeeID - Number_ID - Call_Status)
  • 发票(ID - Call_Log - EmployeeID - 日期)
  • Invoice_Details(ID - InvoiceID - ProductName)

我有两种类型的员工

  1. 首先打电话告诉客户新的优惠,然后在Call_Log表中保存呼叫数据

  2. 第二类员工根据通话状态创建发票(如果客户想购买某些产品,第一位员工将与客户结束通话,第二位员工将再次打电话给他并创建发票)

  3. 我想选择

    1. 第一次致电的员工
    2. 创建发票的员工
    3. 发票ID
    4. 发票日期

3 个答案:

答案 0 :(得分:1)

我认为这是你的答案......

其他请发表评论。会帮助你

  1. 第一次通话的员工

    select Employee.ID, Employee.Name from Call_Log 
    inner join Employee on Call_Log.EmployeeID = Employee.ID
    where Call_Log.Call_Status = 'ReadyToBuy' /*or some constants which mention the status, whether the customer ready or not to buy*/
    
  2. 创建发票的员工

    select Employee.ID, Employee.Name from Invoic 
    inner join Employee on Invoic.EmployeeID = Employee.ID
    
  3. 发票

    select Invoic.*, Invoic_Details.ProductName from Invoic 
    inner join Invoic_Details on Invoic.ID = Invoic_Details.InvoiceID
    
  4. 发票日期

    select Date from Invoic
    

答案 1 :(得分:0)

select call_log.employee_id ,invoice.employee_id,invoice.id,invoice.date
from call_log inner join invoice on call_log.id=invoice.call_log_id
where call_status='ReadytoBuy';

我认为这应该有用。

答案 2 :(得分:0)

试试这个..

SELECT t2.NAME
    ,t5.NAME
    ,t3.ID
    ,t3.creation_date
FROM @call_log t1
INNER JOIN @Employee t2 ON t2.employee_id = t1.employee_id
    AND STATUS = 'invoiced'
INNER JOIN @invoice t3 ON t3.call_log_id = t1.ID
LEFT JOIN @call_log t4 ON t1.num_id = t4.num_id
    AND t4.STATUS = 'started_call'
LEFT JOIN @Employee t5 ON t4.employee_id = t5.employee_id