validation rule using LINQ - linq

It can select operators for each user and I want to check whether the input is valid or not
Here is my expected result
level
user
operator
1
A
-
2
B
AND
2
C
AND
2
D
-
3
E
-
Validation rule
the operator must be "-" when there is 1 record in the same level
If there is more than 1 record in the same level, the operator must be "-" on the last record and it must select "AND"/ "OR" on other records
How to validate it using LINQ and C#?

Related

Oracle. Leave blank line on composite key level break

I want the output of my SQL to leave a blank line when any of three columns (a b or c below) change.
So if the table had columns a b c and d, you end up with a report like this:
a b c d #<- column names
-------
1 a a a #<- V - data itself
1 a a b
#<- level break here because 1 a b is different from 1 a a
1 a b c
From Googling, I have seen BREAK might solve this.
But from what I can make out this pertains to one column.
I then thought what if I have a computed column.
a is numeric, b & c are alphanumeric data types.. so I guess I could possibly use CONCAT plus TO_CHAR..
I am wondering if anyone can give me some pointers?
Cheers.
You can break on more than one column:
SQL> break on a skip 1 duplicates on b skip 1 duplicates on c skip 1 duplicates
SQL> select * from your_table
A B C D
---------- - - -
1 a a a
1 a a b
1 a b c
3 rows selected.
If you issue a plain break it shows what is set:
SQL > break
break on a skip 1 dup
on b skip 1 dup
on c skip 1 dup
Another option is to generate an extra column expression of your composite key, break on that, and set it not to display:
SQL> break on composite skip 1
SQ>> column composite noprint
SQL> select t.*, a||':'||b||':'||c as composite from your_table t;
A B C D
---------- - - -
1 a a a
1 a a b
1 a b c
3 rows selected.
which has the advantage of not showing multiple blank lines if more than one column changes at the same time.
I've separated the values with a colon; the idea of that is to use a character that doesn't appear in the values themselves, to avoid an accidental clash. If any of the columns could actually have a colon then pick something else more obscure.

OBIEE case statement check on non excistence

I have the following situation:
column 1 column 2 column 3
A 1 1
A 1 2
B 3 4
B 3 5
I need to color my letters when the value in column 2 never occurs in column 3. But I need to color all my letters. Does anyone know how to write a case statement for this?
So I'll explain my example: I dont't need to color the letter A because there is a match between column 2 and 3 and the first row.
I do need to color my B's because there is never a match between columns 2 and 3.
I already tried this:
count(distinct(case when "Column 2" != "Column 3" then 1 else 0 end))
but this gives a result for each row and I need a result for the total package.
Thanks!
You can approach this as following:
Create a logical column on your analysis that does a case statement that returns 1 or 0 depending if the values of column2 and column3 are the same (pretty much like the case-when that you provided on your answer but without the count distinct).
Wrap that case statement with a MAX grouped by your column1. This will give you either a consistent 1 or 0 across all your different values of column1. You can use this value for your conditional formatting. The key here is to use the aggregated function with the group by.
You have here some oracle documentation on how to use the logical SQL group by.
Hope that helps! Good luck!

Filter out duplicate results in where clause in sql server

select * from user where username is equal to 'jw'
Add DISTINCT directly after SELECT, or alternatively, use the GROUP BY clause after your WHERE clause.
ex: GROUP BY works.memb_id
I think I understand what you are trying to do. WorkID, EmployeeID, and MembID all have to be 1:1 and then you want to group on all three fields.
You have asserted that each Memb_id is assigned to a unique employee. If WorkID is not then you will need to use an aggregate function to select one of the records returned after the group. Such as Min(), Max() or a window function like Row_Number and or Rank.
For example
WorkID, EmployeeID, MembID
1 1 1
2 1 1
3 2 2
4 1 1
If you use Min(WorkID), EmployeeID, MembID....Group By EmployeeID, MembID you will get:
WorkID, EmployeeID, MembID
1 1 1
3 2 2
If this is not what you are looking for some sample data as a whole and what results you expect would be necessary.

linq to entity filter after groupby caluse

What i want to achieve is simple thing like this using linq to entity. I want to filter the country id only after group by and select clause. However, i can't seem to achieve this simple linq. Applying filtering before group by will results in different key value produce, which is not something that i would expect. The key value that results before groupby is needed.
To put is simple of what i would like to achieve:
Get the key value (r.Key) of certain CountryID
Table:
ID CountryID
1 1
2 1
3 1
4 2
5 1
6 3
_db.TableA.GroupBy(q => q.CountryID).Select(r => r.Key).Where(q => q.CountryID == 2)

Logic of applying Group by in Sql Queries

I am asking a very beginner level of question but I am always confused whenever I want to use aggregate function with Group by. Actually I am getting the right results but I am not pretty sure about how group by is working here. My requirement is to get the count of sent items which is based on MessageGroup columns.
MessageId SenderId MessageGroup Message
_____________________________________________________________________________
1 2 67217969-e03d-41ec-863e-659ca26e660f Hi
2 2 67217969-e03d-41ec-863e-659ca26e660f Hello
3 2 67217969-e03d-41ec-863e-659ca26e660f bye
4 1 c45dc414-9320-40a5-8f8f-9c960d6deffe TC
5 1 8486d16b-294b-45a5-8674-e7024e55f39b shutup
Actually I want to get the count for sent messages.here SenderId=2 has sent three messages to someone but I want to show a single count so I have used MessageGroup and I am doing Groupby and getting the count.
I have used Linq query::
return DB.tblMessage.Where(m => m.SenderId == 2 ).GroupBy(m => m.MessageGroup).Count();
This returns "1" which is correct and I want to show (1) in sent messages.
But if I try to query the above in SQL Server, it returns 3
Here is my SQL query:
select count(*)
from tblMessage
where SenderId = 2
group by MessageGroup
The Linq query is right As it returns me one as Microsoft says here
Actually I am confused with Group by. Please clear my point.
When you are using GroupBy, which ever columns present in groupBy Clause should be in Select Clause
select MessageGroup,count(MessageGroup)from tblMessage
where SenderId=2
group by MessageGroup
You want to include MessageGroup as part of the select, like this:
select MessageGroup, count(*)
from tblMessage
where SenderId=2
group by MessageGroup

Resources