用于获取以下条件的数据的SQL查询

时间:2015-10-14 06:12:44

标签: sql oracle

我需要从包含类型为邮件的表人获取人员详细信息。

应仅提取那些类型为邮件但没有HOME且具有有效person_address的员工。

有效的person_address是没有句号或逗号或引号字符或不是po box的东西。

现有数据:

╔═══════════╦═════════════════╦══════╗
║ person_ID ║ Person_address  ║ Type ║
╠═══════════╬═════════════════╬══════╣
║         1 ║ 5/40 The Avenu  ║ Mail ║
║         2 ║ 5/40 The Avenu  ║ Mail ║
║         2 ║ P O BOX 567     ║ Home ║
║         3 ║ post office 76  ║ Mail ║
║         4 ║ 5/40 The Avenu  ║ Mail ║
║         5 ║ 5/40 The Avenu  ║ Mail ║
║         5 ║ POST OFFICE 67  ║ Home ║
║         6 ║ PO BOX 567      ║ Mail ║
║         7 ║ 5/40 The Avenu  ║ Mail ║
║         8 ║ 5/40 The Avenu. ║ Mail ║
║         9 ║ P O BOX 567     ║ Mail ║
║        10 ║ post office 76  ║ Mail ║
║        11 ║ PO BOX 567      ║ Mail ║
║        12 ║ 5/40 The Avenu  ║ Home ║
║        12 ║ POST OFFICE 67  ║ Mail ║
╚═══════════╩═════════════════╩══════╝

期望的输出:

╔═══════════╦════════════════╦══════╗
║ person_ID ║ Person_address ║ Type ║
╠═══════════╬════════════════╬══════╣
║         1 ║ 5/40 The Avenu ║ Mail ║
║         4 ║ 5/40 The Avenu ║ Mail ║
║         7 ║ 5/40 The Avenu ║ Mail ║
╚═══════════╩════════════════╩══════╝

4 个答案:

答案 0 :(得分:1)

你的数据库是什么。不管怎么试试......

select * from test
minus
select * from test
where id in (
select id  from test
where Typee ='Home'
or upper(address) like  any ('%''%' , '%.%' , '%,%' ,'%P O BOX%' , '%POST%' ,'%PO BOX%'));

刚看到你的输出被改变了......:)

答案 1 :(得分:1)

您可以使用以下查询来实现相同的

select * from person 
where person_id not in (select person_id from person where type='Home')
and person_address not like '%.%' 
and person_address not like '%,%'
and person_address not like 'P%O BOX%'
and upper(person_address) not like 'POST%OFFICE%';

您可以使用Regexp_like简化这些多重条件,如下所示:

select * from person 
         where person_id not in (select person_id from person where type='Home')
           and not regexp_like (person_address,'(P*O*BOX|POST OFFICE|[.,])','i');

您可以在DEMO SQL FIDDLE

中查看所需的输出

答案 2 :(得分:1)

你可以尝试一下

SELECT * FROM (select * from datas group by id having count(*)=1) as temp 

WHERE type REGEXP "^Mail$"

AND address NOT REGEXP "P O BOX|post office|[.,'\"]+"

<强>输出

  person_ID  Person_address  Type 
 -----------------------------------------
      1      5/40 The Avenu  Mail

      4      5/40 The Avenu  Mail

      7      5/40 The Avenu  Mail 

答案 3 :(得分:1)

试试这个:

string content = File.ReadAllText(@"C:\YourDirectory\product.json");

JObject jObj = JObject.Parse(content);
string entityFrameworkSqlServer = (string)jObj["dependencies"]["EntityFramework.SqlServer"];

OUTPUT将是:

ALTER PROCEDURE [dbo].[sp_save_transaction]
    -- Parameter
    @SourceCode nvarchar(255),
    @DepositorName nvarchar(255),
    @TransactionDate date
AS
    SET XACT_ABORT ON  

    --BEGIN TRAN
    DECLARE @PaymentDate date
    DECLARE @PaymentDepositorName nvarchar(255)
    DECLARE @PaymentNationId int

    SET @PaymentDate = @TransactionDate
    SET @PaymentDepositorName = @DepositorName
    SET @PaymentNationId = (SELECT [NAT_SYS_ID] 
                            FROM [MB_TB_NATION] 
                            WHERE [NAT_CODE] = @DepositorCountry)

    INSERT INTO [dbo].[MB_TB_TRANSACTION]
           ([TRA_PAYMENT_DATE], [TRA_PAYMENT_DEPOSITOR_NAME], [TRA_PAYMENT_NATION_ID])
    VALUES (@PaymentDate, @PaymentDepositorName, @PaymentNationId, )