简体   繁体   English

SQL-根据某些条件动态生成的Insert或Select语句

[英]SQL - Dynamically generated Insert or Select statement based on some condition

Lets say i have a table with the following data 可以说我有一个包含以下数据的表

Customer table: 客户表:

Name      amount    date_created    invoice_number
--------------------------------------------------
John     50         11April2012          12
Bob      150        15April2012          32
David    506        10May2012            52
Paul     80         12Aug2012            12
Mark     10         11Jan2012            52

Summary table: 汇总表:

Name        amount  
---------------------
Sally       250 
Darren-32   150 

I would like to select all the rows where date_created is between the start_date and end_date of the current_quarter. 我想选择date_created在current_quarter的start_date和end_date之间的所有行。 If date_created is within the current quarter i would like to append the invoice_number to the name before doing the insert statement (See example in the summary table above). 如果date_created在当前季度内,我想在执行插入语句之前将invoice_number附加到名称上(请参见上面汇总表中的示例)。

INSERT summary(name, amount)
SELECT name|| '-' || invoice_number, date_created, invoice_number
From Customer;
  • How can i modify the above to use either "Decode" function or a "Case" function (or any other "IF statement" type function) to check the value of date_created and append invoice_number if date_created is within the current quarter. 我如何修改以上内容以使用“解码”功能或“案例”功能(或任何其他“ IF语句”类型的功能)来检查date_created的值,如果date_created在当前季度内,则追加发票编号。

  • Obviously i will need to know the start and end dates of the current quarter and will need to store them somewhere before doing the comparison. 显然,我将需要知道当前季度的开始和结束日期,并且需要在进行比较之前将它们存储在某个位置。 Is this possible at all with pure SQL? 使用纯SQL完全可行吗? PL/SQL is not an option. 不能使用PL / SQL。

Assuming we are in Q1 (Apr - Jun) the end result should be: 假设我们在第一季度(4月至6月),则最终结果应为:

Name        amount  
---------------------
Sally       250 
Darren-32   150
John-12     50
Bob-32      150
David-52    506
Paul        80
Mark        10

I am reading the Customer table from an Oracle 10G database and populating the summary into a "Summary" table which resides in a SQL server database. 我正在从Oracle 10G数据库中读取Customer表,并将摘要填充到驻留在SQL Server数据库中的“摘要”表中。 The fact that i am inserting into an SQL Server database should not really matter. 我要插入SQL Server数据库中的事实并不重要。 I am reading the data from an Oracle database so the syntax should be Oracle compatible. 我正在从Oracle数据库读取数据,因此语法应与Oracle兼容。

declare @CurrentQtr int
set @CurrentQty = CASE
  WHEN datepart(month, getdate()) in (1,2,3) THEN 1
  WHEN datepart(month, getdate()) in (4,5,6) THEN 2
  WHEN datepart(month, getdate()) in (7,8,9) THEN 3
  ELSE 4 END

SELECT name + case 
  WHEN datepart(month, date_created) in (1,2,3) AND @CurrentQtr = 1 THEN '-' + invoice_number 
  WHEN datepart(month, date_created) in (4,5,6) AND @CurrentQtr = 2 THEN '-' + invoice_number 
  WHEN datepart(month, date_created) in (7,8,9) AND @CurrentQtr = 3 THEN '-' + invoice_number 
  WHEN datepart(month, date_created) in (10,11,12) AND @CurrentQtr = 4 THEN '-' + invoice_number 
  ELSE '' end as Name, amount
From Customer;

The Oracle syntax to do what you describe in your two bullet-points would be 执行您在两个要点中描述的操作的Oracle语法将是

SELECT (CASE WHEN     date_created >= trunc(sysdate,'Q') 
                  AND date_created < trunc(add_months(sysdate,3),'Q')
             THEN name || '-' || invoice_number
             ELSE name
         END),
       date_created, 
       invoice_number
  FROM Customer;

Something like this? 像这样吗

 insert summary
 Select 
     Name || case 
         when datepart(dp_quarter, sysdate)=datepart(dp_quarter, date_Created)
   and extract(year from sysdate) = extract(year from date_Created) 
         then '-' || invoice_number
         else '' end,
 amount from Customer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM