Share via

select -group

arkiboys 9,711 Reputation points
2022-05-07T15:42:09.67+00:00

Hello,
I would like to carry out rules on a sample table as below.
Can you see how to return the final result please?
Thank you

create table #tblMain
(
V_Number varchar(50),m_name varchar(50),party varchar(50),m_shipment varchar(50),[Name] varchar(50)
,estimated date,actual date,ml_name varchar(50),Doc_Name varchar(50),[weight] float
)

insert into #tblMain (V_Number,m_name,party,m_shipment,[Name],estimated,actual,ml_name,Doc_Name,[weight])
values ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','t DateName','7/1/2021','7/1/2021','destination','uk',33.2),
 ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','t DateName','7/1/2021','7/1/2021','source','france',33.2),
 ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','l DateName','7/1/2021','7/1/2021','source','uk',33.2),
 ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','l DateName','7/1/2021','7/1/2021','destination','scotland',33.2),
 ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','v DateName','7/30/2021',null,'source','scotland',33.2),
 ('V_98xx','companyNamexyz','partyNamexxx','SHIP_xyz','v DateName','7/30/2021',null,'destination','belgium',33.2),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','t DateName','7/1/2021','7/2/2021','destination','uk',40.12),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','t DateName','7/1/2021','7/2/2021','source','france',40.12),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','l DateName','7/1/2021','7/2/2021','source','uk',40.12),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','l DateName','7/1/2021','7/2/2021','destination','scotland',40.12),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','v DateName','7/30/2021','01/8/2021','destination','belgium',40.12),
 ('V_98xx','company999','partyNamexxx','SHIP_xyz','v DateName','7/30/2021','01/8/2021','source','scotland',40.12)

select * from #tblMain
--drop table #tblMain
/*
select V_Number, m_name from the #tblMain where [RULE as below] group by V_Number, m_name

check rule:
if
[actual] date column of (t DateName) row values in [Name] column is not null
and [actual] date column of (l DateName) row values is not null
and [actual] date column of (v DateName) rows values is not null
and weight is not null

--The above rule should return:
V_Number, m_name
----------------
V_98xx companyNamexyz
*/
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

Answer accepted by question author

Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
2022-05-07T20:00:34.393+00:00

It would have helped to have some sample data where my previous query gave the wrong result. But here is my next guess:

SELECT A.V_Number, A.m_name
 FROM   (SELECT DISTINCT V_Number, m_name FROM #tblMain) AS A
 WHERE  EXISTS (SELECT *
                FROM   #tblMain B
                WHERE  B.V_Number = A.V_Number
                  AND  B.m_name   = A.m_name
                  AND  B.actual IS NOT NULL 
                  AND  B.Name = 't DateName')
   AND  EXISTS (SELECT *
                FROM   #tblMain B
                WHERE  B.V_Number = A.V_Number
                  AND  B.m_name   = A.m_name
                  AND  B.actual IS NULL 
                  AND  B.Name IN ('v DateName', 'l DateName'))

I note that in this round of the game the column weight has fallen out of the rules.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2022-05-07T17:20:57.353+00:00

    I can't say that I understand the rule fully, but I tried to read between the lines. Maybe this is what you are looking for:

    SELECT A.V_Number, A.m_name
    FROM   (SELECT DISTINCT V_Number, m_name FROM #tblMain) AS A
    WHERE  EXISTS (SELECT *
                    FROM   #tblMain B
                    WHERE  B.V_Number = A.V_Number
                      AND  B.m_name   = A.m_name
                      AND (B.actual IS NULL OR
                           B.Name = 'v DateName' AND weight IS NULL))
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.