RSA Archer - IF Statements - not returning any value - archer

I'm trying to use a calculated field to return the value in "Overall likelihood" field. The value returned by the Overall Likelihood fields depends on the values selected in "Likelihood of Occurrence" and "Likelihood of Adverse Impacts" fields. These values are selected by the users based on which the "Overall likelihood" field should be calculated.
The formula below gets validated but doesn't returns any value. Is there a problem with the way the logical operators are structured ? Please advise;
IF(
OR(
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Almost Certain"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Rare")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Likely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Rare")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Likely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Unlikely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Possible"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Unlikely")
),
AND (
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Possible"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Possible")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Unlikely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Likely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Unlikely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Possible")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Rare"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Likely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Rare"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Almost Certain")
)),
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Possible"),
IF(
OR(
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Almost Certain"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Unlikely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Almost Certain"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Possible")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Likely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Possible")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Likely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Likely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Possible"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Likely")
),
AND (
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Possible"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Almost Certain")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Unlikely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Almost Certain")
)),
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Likely"),
IF(
OR(
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Almost Certain"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Likely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Almost Certain"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Almost Certain")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Likely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Almost Certain")
)),
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Almost Certain"),
IF(
OR(
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Possible"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Rare")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Unlikely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Unlikely")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Rare"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Possible")
)),
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Unlikely"),
IF(
OR(
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Unlikely"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Rare")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Rare"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Rare")
),
AND(
[Likelihood of Occurrence]=VALUEOF([Likelihood of Occurrence],"Rare"),
[Likelihood of Adverse Impacts]=VALUEOF([Likelihood of Adverse Impacts],"Unlikely")
)),
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Rare")
)
)
)
)
)

When you are setting a value in a valuelist field, use:
VALUEOF([Overall Likelihood],"Possible")
instead of:
[Overall Likelihood]=VALUEOF([Overall Likelihood],"Possible")
Hope this helps!

Related

Compare two tables and find differences in column values

I want to compare two tables (identical columns) and find out that what column value changed.
Here is an example with sample data.
employee_original table has 6 columns.
CREATE TABLE [dbo].[employee_original](
[emp_id] [int] IDENTITY(1,1) NOT NULL,
[first_name] [varchar](100) NOT NULL,
[last_name] [varchar](100) NOT NULL,
[salary] int NOT NULL,
[city] [varchar](20) NOT NULL,
[department] [varchar](20) NOT NULL,
PRIMARY KEY CLUSTERED
(
[emp_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO employee_original VALUES ( 'Julia', 'Schultz', 100, 'New York', 'Tech');
INSERT INTO employee_original VALUES ( 'Vincent', 'Trantow', 200, 'Moscow', 'HR');
INSERT INTO employee_original VALUES ( 'Whitney ', 'Pouros', 500, 'Miami', 'Accounting');
INSERT INTO employee_original VALUES ( 'Chandler', 'Osinski', 10, 'Singapore', 'Purchasing');
INSERT INTO employee_original VALUES ( 'Sydnie', 'Green', 700, 'Ireland', 'Operations');
INSERT INTO employee_original VALUES ( 'Josefa', 'Anderson', 800, 'Berlin', 'Purchase');
INSERT INTO employee_original VALUES ( 'Brayan', 'Bergstrom', 900, 'New York', 'Operations');
INSERT INTO employee_original VALUES ( 'Shyanne', 'Kris', 900, 'New York', 'Sales');
employee_modified has same employee but some of the attributes have changed for few employees.
CREATE TABLE [dbo].[employee_modified](
[emp_id] [int] IDENTITY(1,1) NOT NULL,
[first_name] [varchar](100) NOT NULL,
[last_name] [varchar](100) NOT NULL,
[salary] int NOT NULL,
[city] [varchar](20) NOT NULL,
[department] [varchar](20) NOT NULL,
PRIMARY KEY CLUSTERED
(
[emp_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO employee_modified VALUES ( 'Julia', 'Schultz', 100, 'New York', 'Tech');
INSERT INTO employee_modified VALUES ( 'Vincent', 'Wyman', 500, 'Moscow', 'HR');
INSERT INTO employee_modified VALUES ( 'Whitney ', 'Pouros', 500, 'Miami', 'Sales');
INSERT INTO employee_modified VALUES ( 'Chandler', 'Osinski', 10, 'Singapore', 'Purchasing');
INSERT INTO employee_modified VALUES ( 'Sydnie', ' Cartwright', 900, 'Ireland', 'Operations');
INSERT INTO employee_modified VALUES ( 'Joseph', 'Anderson', 800, 'Berlin', 'Purchase');
INSERT INTO employee_modified VALUES ( 'Bryan', 'Bergstrom', 900, 'Naples', 'Operations');
INSERT INTO employee_modified VALUES ( 'Shyanne', 'Jakubowski', 900, 'New York', 'Accounting');
I am looking for a result that can tell me what field changed for which employee.
e.g. emp_id =2 has last name and salary change. So output should look like:
emp_id attribute orignial_value new_value
2 last_name Trantow Wyman
2 salary 200 500
This is what I have tried so far:
(1) Join tables and find what changed :
DROP TABLE IF EXISTS #temp;
SELECT distinct
o.emp_id,
o.first_name [original_first_name], m.first_name [modified_first_name],
o.last_name [original_last_name], m.last_name [modified_last_name],
o.salary [original_salary], m.salary [modified_salary],
o.city [original_city], m.city [modified_city],
o.department [original_department], m.department [modified_department]
into #temp from
[dbo].[employee_original] o inner join [dbo].[employee_modified] m on o.emp_id = m.emp_id
select * from #temp
Gives me
(2) Self join with #temp and find out what attribute has changed.
-- All Last Name Changes.
select distinct t1.emp_id, t1.original_last_name, t2.modified_last_name
from #temp t1
inner join #temp t2 on t1.emp_id = t2.emp_id
where t1.original_last_name <> t2.modified_last_name
-- All Department changes
select distinct t1.emp_id, t1.original_department, t2.modified_department
from #temp t1
inner join #temp t2 on t1.emp_id = t2.emp_id
where t1.original_department <> t2.modified_department
Any pointers on how I can get to my desired result.
Here is an option that will dynamically unpivot your data without actually using dynamic SQL.
Example
Select emp_id
,[key]
,Org_Value = max( case when Src=1 then Value end)
,New_Value = max( case when Src=2 then Value end)
From (
Select Src=1
,emp_id
,B.*
From [employee_original] A
Cross Apply ( Select [Key]
,Value
From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper,INCLUDE_NULL_VALUES ) )
) B
Union All
Select Src=2
,emp_id
,B.*
From [employee_modified] A
Cross Apply ( Select [Key]
,Value
From OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper,INCLUDE_NULL_VALUES ) )
) B
) A
Group By emp_id,[key]
Having max( case when Src=1 then Value end)
<> max( case when Src=2 then Value end)
Order By emp_id,[key]
Results
You can use the following code to unpivot all possible changes
SELECT
o.emp_id,
v.column_name,
v.old_value,
v.new_value
FROM employee_original o
JOIN employee_modified m ON o.emp_id = m.emp_id
CROSS APPLY (
SELECT 'first_name', CAST(o.first_name AS nvarchar(max)), CAST(m.first_name AS nvarchar(max))
WHERE o.first_name <> m.first_name
UNION ALL
SELECT 'last_name', o.last_name, m.last_name
WHERE o.last_name <> m.last_name
UNION ALL
SELECT 'salary', o.salary, m.salary
WHERE o.salary <> m.salary
UNION ALL
SELECT 'city', o.city, m.city
WHERE o.city <> m.city
UNION ALL
SELECT 'department', o.department, m.department
WHERE o.department <> m.department
) v(column_name, old_value, new_value);

How to reference / insert the IDs inserted in the last two insert statements within the same Oracle stored procedure?

I have a stored procedure that is inserting values based off a cursor that I wrote. I am trying to put in a calculated value summed by day. However, (I looked online and couldn't find any examples that do this); how do I reference a previously generated ID (both for Value and Calculation) that are both generated by a trigger?
I was thinking of possibly using a select from the table with the query pulling the most recent id from both tables? I was thinking that that may be too easy to be correct though.
This is my code:
create or replace PROCEDURE TEST_PROC IS
Cursor c1 is
SELECT SUM(v.value_tx) AS sum_of_values ,
e.entity_id AS entity_id ,
e.entity_name_tx AS entity_name ,
e.ei_id_tx AS ei_id ,
v.create_dt AS create_dt ,
v.hr_num AS hr_num ,
v.utc_offset ,
v.data_date ,
v.hr_utc ,
v.hr ,
ff.form_field_id,
s.survey_respondent_id,
v.data_code ,
'N' AS processed
FROM value v
join submission_value sv
ON v.value_id = sv.value_id
join form_field ff
ON sv.form_field_id = ff.form_field_id
join submission s
ON sv.submission_id = s.submission_id
join survey_respondent sr
ON s.survey_respondent_id = s.survey_respondent_id
join entity e
ON sr.entity_id = e.entity_id
WHERE ei_id_tx IN ('ABC', 'ZYX', 'ADA', 'AZL', 'BAN', 'CAB')
AND ff.form_field_id IN ('55', '77')
GROUP BY e.entity_id,
e.entity_name_tx,
e.ei_id_tx,
v.create_dt,
v.hr_num,
v.utc_offset,
v.data_date,
v.hr_utc,
v.hr,
v.data_code;
l_var c1%ROWTYPE;
BEGIN
OPEN c1;
LOOP
FETCH c1 into l_var;
EXIT WHEN c1%NOTFOUND;
insert into value (product_id, data_source_id, unit_cd, value_tx, utc_offset, data_date, hr_utc, hr, hr_num, data_code, create_dt, create_user_id, modify_dt, modify_user_id, effective_dt, inactive_dt)
Values ('77', '555', 'NA', l_var.sum_of_values, l_var.utc_offset, l_var.data_date, l_var.hr_utc, l_var.hr, l_var.hr_num, l_var.data_code, sysdate, '1', null, null, null, null);
insert into calculation (survey_respondent_id, calculation_dt, calculation_name_tx, create_dt, create_user_id, modify_dt, modify_user_id, effective_dt, inactive_dt, publication_issue_id, entity_survey_id)
Values (l_var.survey_respondent_id, sysdate, sysdate, sysdate, '1', null, null, null, null, null, null);
insert into calculation_value (calculation_id, value_id, form_field_id, create_dt, create_user_id, modify_dt, modify_user_id, effective_Dt, inactive_dt)
Values ( ??, ??, l_var.form_field_id, sysdate, '1', null, null, null, null);
END LOOP;
CLOSE c1;
END TEST_PROC;
*Calculation Table*
Calculation_ID Survey_Respondent_ID Calculation_Dt Calculation_Name
Create_Dt Create_User_id
*Value Table*
Value_Id Value Data_Date HR Data_Code Create_Dt ..
Don't know if I understand completly, but maybe this is what you are looking for? I may have typed it wrong.
create or replace PROCEDURE TEST_PROC IS
Cursor c1 is
SELECT SUM(v.value_tx) AS sum_of_values ,
e.entity_id AS entity_id ,
e.entity_name_tx AS entity_name ,
e.ei_id_tx AS ei_id ,
v.create_dt AS create_dt ,
v.hr_num AS hr_num ,
v.utc_offset ,
v.data_date ,
v.hr_utc ,
v.hr ,
ff.form_field_id,
s.survey_respondent_id,
v.data_code ,
'N' AS processed
FROM value v
join submission_value sv
ON v.value_id = sv.value_id
join form_field ff
ON sv.form_field_id = ff.form_field_id
join submission s
ON sv.submission_id = s.submission_id
join survey_respondent sr
ON s.survey_respondent_id = s.survey_respondent_id
join entity e
ON sr.entity_id = e.entity_id
WHERE ei_id_tx IN ('ABC', 'ZYX', 'ADA', 'AZL', 'BAN', 'CAB')
AND ff.form_field_id IN ('55', '77')
GROUP BY e.entity_id,
e.entity_name_tx,
e.ei_id_tx,
v.create_dt,
v.hr_num,
v.utc_offset,
v.data_date,
v.hr_utc,
v.hr,
v.data_code;
l_var c1%ROWTYPE;
v_value_id value.value_id%type;
v_calculation_id calculation.calculation_id%type;
BEGIN
OPEN c1;
LOOP
FETCH c1 into l_var;
EXIT WHEN c1%NOTFOUND;
insert into value (value_id,product_id, data_source_id, unit_cd, value_tx, utc_offset, data_date, hr_utc, hr, hr_num, data_code, create_dt, create_user_id, modify_dt, modify_user_id, effective_dt, inactive_dt)
Values (null,'77', '555', 'NA', l_var.sum_of_values, l_var.utc_offset, l_var.data_date, l_var.hr_utc, l_var.hr, l_var.hr_num, l_var.data_code, sysdate, '1', null, null, null, null)
returning value_id into v_value_id;
insert into calculation (calculation_id,survey_respondent_id, calculation_dt, calculation_name_tx, create_dt, create_user_id, modify_dt, modify_user_id, effective_dt, inactive_dt, publication_issue_id, entity_survey_id)
Values (null,l_var.survey_respondent_id, sysdate, sysdate, sysdate, '1', null, null, null, null, null, null)
returning calculation_id into v_calculation_id;
insert into calculation_value (calculation_id, value_id, form_field_id, create_dt, create_user_id, modify_dt, modify_user_id, effective_Dt, inactive_dt)
Values ( v_calculation_id, v_value_id, l_var.form_field_id, sysdate, '1', null, null, null, null);
END LOOP;
CLOSE c1;
END TEST_PROC;

Calculate the number of boxes needed for a given set of items (weights)

I have a ms-sql table which looks like this (weight = kg).
I want to be able to calculate number of boxes and weight per box needed for an given identifier. A box can hold up to exactly 30 Kg. All items of that identifier can be mixed within a box. I am limited to sql only (2008) but everything can be used (CTE,Functions,StoredProcs and so on). I tried different approaches (CTE, functions) but i wasn't able to get the right results. any kind of help appreciated.
Expected output
when selecting identifier 100001:
when selecting identifier 100002:
when selecting identifier 100003:
when selecting identifier 100004:
UPDATE
sample table
CREATE TABLE [dbo].[tblTest](
[position] [int] NOT NULL,
[item] [varchar](31) NOT NULL,
[quantity] [money] NOT NULL,
[weight] [money] NOT NULL,
[identifier] [varchar](50) NOT NULL,
CONSTRAINT [PK_tblTest] PRIMARY KEY CLUSTERED
(
[position] ASC,
[identifier] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
sample data
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000001', 4.0000, 10.0000, N'100001')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000003', 3.0000, 15.0000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000006', 7.0000, 25.0000, N'100003')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0000007', 1.0000, 1.5000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (1, N'0023021', 2.0000, 14.5000, N'100005')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000002', 1.0000, 15.0000, N'100001')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000004', 1.0000, 5.0000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0000008', 1.0000, 2.5000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (2, N'0023022', 3.0000, 17.5000, N'100005')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (3, N'0000005', 3.0000, 2.5000, N'100002')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (3, N'0000009', 3.0000, 6.0000, N'100004')
INSERT [dbo].[tblTest] ([position], [item], [quantity], [weight], [identifier]) VALUES (4, N'0000010', 1.0000, 1.0000, N'100004')
Here is one option:
See the demo here: http://rextester.com/THP2733
setup:
create table tbl
(position integer, item integer, quantity integer, weight decimal(10,2), identifier integer);
insert into tbl
select 1, 1, 4, 10, 100001 union all
select 2, 2, 1, 15, 100001 union all
select 1, 3, 3, 15, 100002 union all
select 2, 4, 1, 5, 100002 union all
select 3, 5, 3, 2.5, 100002 union all
select 1, 6, 7, 25, 100003 union all
select 1, 7, 1, 1.5, 100004 union all
select 2, 8, 1, 2.5, 100004 union all
select 3, 9, 3, 6, 100004 union all
select 4, 10, 1, 1, 100004 ;
query:
with cte(position, item, quantity, weight, identifier, cntr)
as(select position, item, quantity, weight, identifier, quantity
from tbl t1
union all
select t2.position, t2.item, t2.quantity, t2.weight, t2.identifier, cte.cntr - 1
from tbl t2
join cte
on t2.identifier = cte.identifier
and t2.item = cte.item
and cte.cntr > 1
)
select
identifier,
sum(flg) over (partition by identifier order by item, cntr desc) package,
case when rolling_weight - lag(rolling_weight) over (partition by identifier order by cntr desc) is NULL then rolling_weight
else rolling_weight - lag(rolling_weight) over (partition by identifier order by cntr desc)
end weight
from
(
select temp1.*,
case when rolling_weight % 30 = 0 then 1
when rolling_weight = total_weight then 1
when weight + lead(weight) over (partition by identifier order by cntr desc) > 30 then 1
else 0 end as flg
from
(
select
cte.*,
sum(weight) over (partition by identifier order by item, cntr desc) rolling_weight,
sum(weight) over (partition by identifier) total_weight
from cte
) temp1
) temp2
where flg = 1
order by identifier, package

Display the latest row only

Goal:
If you retrieve any duplicate data that is data in the column secondid, then you to retrieve one row only from the latest date. For instance in the data below I have two different datetime, I would like to retrieve the data '2016-05-02 07:34:14.377' from value 6 in column secondid.
Problem:
I code seems not to be working and what am I missing.
Info:
There are many data in and you cannot hard code the value in in the code.
CREATE TABLE [dbo].[testing2](
[id] [int] NOT NULL,
[secondid] [int] NULL,
[value] [varchar](30) NULL,
[category] [int] NULL,
[test_id] [int] NULL,
[id_type] [int] NOT NULL,
[Testing2Datetime] [datetime] not NULL,
CONSTRAINT [PK_testing2] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO [test].[dbo].[testing2]
VALUES (3, 3, 'a' ,2 ,11 ,1, '2016-05-01 07:34:14.377');
INSERT INTO [test].[dbo].[testing2]
VALUES (4, 4, 'a' ,2 ,11 ,1, '2016-05-01 07:34:14.377');
INSERT INTO [test].[dbo].[testing2]
VALUES (5, 5, 'a' ,2 ,11 ,0, '2016-05-01 07:34:14.377');
INSERT INTO [test].[dbo].[testing2]
VALUES (6, 6, 'a' ,2 ,11 ,2, '2016-05-01 07:34:14.377');
INSERT INTO [test].[dbo].[testing2]
VALUES (7, 6, 'a' ,2 ,11 ,2, '2016-05-02 07:34:14.377');
select
bb.secondid,
max(bb.Testing2Datetime)
from [dbo].[testing2] bb
group by
bb.secondid,
bb.Testing2Datetime
The maximum value of Testing2Datetime per Testing2Datetime is the Testing2Datetime itself. You should remove it from the group by clause and retrieve the maximum value per secondid only:
select
bb.secondid,
max(bb.Testing2Datetime)
from [dbo].[testing2] bb
group by
bb.secondid -- Here!
Remove bb.Testing2Datetime column from Group by
select
bb.secondid,
max(bb.Testing2Datetime) as [Max_Testing2Datetime]
from [dbo].[testing2] bb
group by
bb.secondid
or even this(Row_Number window function)
select *
from
(
select
bb.secondid,
bb.Testing2Datetime,
Row_number()over(partition by bb.secondid order by bb.Testing2Datetime desc) as RN
from [dbo].[testing2] bb
) A
Where RN = 1

Check 1:N hierarchy is maintained at 4 levels

I have hierarchy level data of 5 levels in a table
1.BRANCH
2.LEGAL-ENTITY
3.REPORT-UNIT
4.REGIONAL
5.COUNTRY
I have a table which contain records containing all the hierarchy with Branch as the Primary Key.
I need to check if the relationship of 1:n is maintained between higher level hierarchy to low level hierarchy.
WHITEFIELD|BANGALORE|KARNATAKA|INDIA|APAC
WHITEFIELD|BANGALORE|MYSORE|INDIA|APAC - WRONG RECORD
MG ROAD|BANGALORE|KARNATAKA|INDIA|APAC
MG ROAD|NEW DELHI|DELHI|INDIA|APAC - WRONG RECORD
SILK BOARD|BANGALORE|KARNATAKA|INDIA|APAC
SILK BOARD|PUNE|MAHARASTRA|INDIA|APAC - WRONG RECORD
To achieve this, I want to write one query which can give me records which are not satisfying the above rule.
I have already written a query which can give the result for 4th and 5th level.
SELECT COD_BRNC,COUNT(DISTINCT COD_LEGL_ENTT) FROM TDI_GEO_BY_ORIGIN
WHERE COD_BRNC NOT LIKE '%_D' AND YEAR(DAT_END_GEO_ORGN ) = 9999
GROUP BY COD_BRNC HAVING COUNT(DISTINCT COD_LEGL_ENTT) > 1
It gives me
SILK BOARD|2
MG ROAD|2
Your first query is a good start.
As you did not give a complete Table description I created my own testcase:
DROP TABLE level_5_test;
CREATE TABLE level_5_test( branch_id VARCHAR2( 3 )
, level_1 VARCHAR2( 3 )
, level_2 VARCHAR2( 3 )
, level_3 VARCHAR2( 3 )
, level_4 VARCHAR2( 3 )
, description VARCHAR2( 30 )
);
INSERT INTO level_5_test
VALUES ( 'B0', 'L1', 'L2', 'L3', 'L4', 'No Problems' );
INSERT INTO level_5_test
VALUES ( 'B1', 'L1', 'L2', 'L3', 'L4', 'Level 1 Problems' );
INSERT INTO level_5_test
VALUES ( 'B1', 'E1', 'L2', 'L3', 'L4', 'Level 1 Problems' );
INSERT INTO level_5_test
VALUES ( 'B2', 'L1', 'L2', 'L3', 'L4', 'Level 2 Problems' );
INSERT INTO level_5_test
VALUES ( 'B2', 'L1', 'E2', 'L3', 'L4', 'Level 2 Problems' );
INSERT INTO level_5_test
VALUES ( 'B3', 'L1', 'L2', 'L3', 'L4', 'Level 3 Problems' );
INSERT INTO level_5_test
VALUES ( 'B3', 'L1', 'L2', 'E3', 'L4', 'Level 3 Problems' );
INSERT INTO level_5_test
VALUES ( 'B4', 'L1', 'L2', 'L3', 'L4', 'Level 4 Problems' );
INSERT INTO level_5_test
VALUES ( 'B4', 'L1', 'L2', 'L3', 'E4', 'Level 4 Problems' );
INSERT INTO level_5_test
VALUES ( 'B5', 'L1', 'L2', 'L3', 'E4', 'Double Problems' );
INSERT INTO level_5_test
VALUES ( 'B5', 'L1', 'E2', 'L3', 'L4', 'Double Problems' );
COMMIT;
--
The best way to get all the answers is by querying all fields and adding the analytic variations of count(distinct...) where you partition by the branch_id:
WITH level_query
AS (SELECT branch_id
, level_1
, level_2
, level_3
, level_4
, description
, COUNT( DISTINCT level_1 ) OVER (PARTITION BY branch_id)
AS level_1_count
, COUNT( DISTINCT level_2 ) OVER (PARTITION BY branch_id)
AS level_2_count
, COUNT( DISTINCT level_3 ) OVER (PARTITION BY branch_id)
AS level_3_count
, COUNT( DISTINCT level_4 ) OVER (PARTITION BY branch_id)
AS level_4_count
FROM level_5_test)
SELECT *
FROM level_query
WHERE level_1_count > 1
OR level_2_count > 1
OR level_3_count > 1
OR level_4_count > 1;
This works for Oracle, but might not work (yet) for Hana. Then you have to combine four queries with UNION ALL
SELECT 'L1' AS level_name
, branch_id
FROM level_5_test
GROUP BY branch_id
HAVING COUNT( DISTINCT level_1 ) > 1
UNION ALL
SELECT 'L2' AS level_name
, branch_id
FROM level_5_test
GROUP BY branch_id
HAVING COUNT( DISTINCT level_2 ) > 1
UNION ALL
SELECT 'L3' AS level_name
, branch_id
FROM level_5_test
GROUP BY branch_id
HAVING COUNT( DISTINCT level_3 ) > 1
UNION ALL
SELECT 'L4' AS level_name
, branch_id
FROM level_5_test
GROUP BY branch_id
HAVING COUNT( DISTINCT level_4 ) > 1
ORDER BY 2, 1;
The first solution needs only one full table scan, the second query needs four and would need a fifth FTS to give as much information as the first!