find rows with same value using LINQ - linq

The issue with the image above is that
I want to apply a bonus share to the shareholders based on the TransLog_Date
Because the shareholder in row 1 has already transferred a portion of his shares to the holder in row 3, his new share value is what shows in row 2 column 5 as 100000
I want the bonus share to be calculated based on the value in row 2, column 5 but not on row 1, column 5.
I have tried several codes in LINQ but getting it wrong as the value is multiplied on all the 3 records which is totally wrong.
var transQuery = (from tq in db.TransactionsLogDbSet
where (tq.TransactionType == TransactionType.PurchaseOfShares)
|| (tq.TransactionType == TransactionType.TransferOfShares)
|| (tq.TransactionType == TransactionType.BonusSharesDeclared)
select tq);
var query = from row in db.TransactionsLogDbSet
group row by row.Transholder_ID into g
select new { Count = g.Count() };

OK, i think I finally got what you want.
Is that right?: Group all rows of some TransactionTypes by the Transholder_ID, then from each group select the row with the maximum Translog_Date, giving you the most recent row per Transholder_ID. Once you have the result of that, you can iterate over it to calculate whatever you need.
from t in db.TransactionLogDbSet
where where tq.TransactionType == TransactionType.PurchaseOfShares || tq.TransactionType == TransactionType.TransferOfShares || tq.TransactionType == TransactionType.BonusSharesDeclared
group t by t.Transholder_ID into g
select (from t1 in g orderby t1.Translog_Date descending select t1).First();
EDIT: I'll continue to edit the following part of my answer as long as we finally arrive at the correct query.
From you last comment so far it follows that you just want to select all rows with a TransLog_Date <= a given DateTime qualifiedDate.
from t in db.TransactionLogDbSet
where t.TransactionLog_Date <= qualifiedDate
select t
Is that it?

Related

Need to return max value using case in subquery

I have records with several line items. I need to return the record and the max value of a flag that reviews the line items. A simplified version of the code without getting the max value for the flag is:
Select nvl(order.order_value, order2.order_value) as "order",
case when item.item_no in ('abc','def','ghi') then 'Y' else 'N' end as "Flag",
field 3,
field 4
from order
left join order2 on order.order_value=order2.order_value
left join item on item.order_value=order.order_value
The actual current code does have a lot of other fields selected from other various tables/subqueries.
My issue is that there are multiple items in the item table per order_value, and I only want one line per order. I want the flag to say yes if any item falls in the my 'y' list.

PowerBI: same IDs for consecutive data

I have a question about generating IDs in power BI.
Question: I have the table like following:
I'd like to generate same IDs for consecutive data (it means that end date of previous row equals to start date of current row), that has equal values in column 'Type', but IDs have to be different, if data is not consecutive. The example of correct IDs are below.
(Note that 'Bike' has different Ids because these rows are NOT consecutive)
Can you help me please with writing DAX command or Query? Every advice would be very helpful.
I created 3 separate calculated columns in order to accomplish this , with the final IDs column being the result you're looking for. I've included the DAX below, just replace the "Table1" references with your table's name. Skip down to the bottom to see an image of how the final table looks (Results Table).
First Calculated Column
Create a unique string ID for each row in the table. Specify "Same" for rows that will inherit the previous unique ID. This makes use of the EARLIER function and a solution similar to Get Value from Previous Row post on the Community page in order to evaluate the previous row in a calculation.
First Iteration IDs =
VAR previousRow =
TOPN(
1,
FILTER(
Table1,
Table1[Start Date] < EARLIER(Table1[Start Date])
&& Table1[Type] = EARLIER(Table1[Type])
),
[Start Date],
DESC
)
VAR previousEndDate = MINX(previousRow, [End Date])
VAR previousType = MINX(previousRow, [Type])
RETURN
IF(
Table1[Start Date] = previousEndDate && Table1[Type] = previousType,
"Same",
CONCATENATE(FORMAT(Table1[Start Date], "dd/mm/yyyy"), CONCATENATE(" ", Table1[Type]))
)
Second Calculated Column
Convert all of the "Same" IDs from the previous column to their respective IDs in that column.
Second Iteration IDs =
VAR previousRow =
TOPN(
1,
FILTER(
Table1,
Table1[Start Date] < EARLIER(Table1[Start Date])
&& Table1[Type] = EARLIER(Table1[Type])
&& NOT(Table1[First Iteration IDs] = "Same")
),
[First Iteration IDs],
DESC
)
VAR previousID = MINX(previousRow, [First Iteration IDs])
RETURN
IF(
Table1[First Iteration IDs] = "Same",
previousID,
Table1[First Iteration IDs]
)
Third Calculated Column
Use the RANKX function to create more user-friendly number IDs from the string IDs in the previous column. This is the original column that you had asked for.
IDs = RANKX(Table1, Table1[Second Iteration IDs], , 1, Dense) - 1
I've included a results table below based on the sample table that you provided. You may need to play around with the unique string ID format or the RANKX function depending on how your actual data looks.

How can I display Row Number in Cross-Tab?

I'm looking for a way to display row number in my cross-tab.
I tried searching online for the answer on how to do it but I haven't found anything useful.
So I'm turning to the good people on Stack Overflow.
The reason that I want to do this, if it's even possible, is because many clients in the company I started working at asked to have a row number in the cross-tab.
I am using Visual Studio 2013 and Crystal Reports.
So is there any basic ( easy ) way to do this in Crystal Reports?
For example, I have a cross-tab that displays unit of measure and amounts.
https://imgur.com/a/lOjCq
But I would like my cross-tab to be like:
Amount
1. Total -38
2. KG
3. kut 9
4. LIT. 4
5. m -32
6. proc
7. KoŠ¼ -19
Please keep in mind that I only started working with Crystal Reports this week, so this is all new to me. And the cross-tab in the picture is just a random one I made to explain what I need.
Thank You in advance.
In order to show Row Number in your CrossTab you will need to first put Row Number in the Stored Procedure that sends data to your report.
In order to understand it better i will first show you how my data looks before i add a Row number(Pic 1).
Code:
select
a.S_ID as ID,
osn.sifra as BasicGoodsCode,
osn.naziv as BasicGoods,
null,
a.RobaSifra as GoodsCode,
a.Roba as Goods,
a.Detalj as Detail,
a.DetaljDodatak as DetailsAddon
from NP_Stavke s
left join RobaGrupe osn on osn.id = s.RobaId
left join #A a on a.S_ID = s.Id
order by BasicGoodsCode, ID
Pic 1: As you can see I have 3 different Ids for BasicGoods which means that I have 3 Rows in my CrossTab
Columns ID, BasicGoodsCode and BasicGoods are going to be Rows in my CrossTab.
Values from column DetailsAddon are going to be my columns in CrossTab.
Columns GoodsCode, Goods and Detail are going to be values in my CrossTab.
Column Pieces is not important.
Now that you know how everything looks we can start with adding a Row Number to our CrossTab.
Step 1:
First thing that you need to do is to add a Row number in table in your stored procedure.
To do this I used DENSE_RANK()
depending on your data you might need to use ROW_NUMBER() or maybe even something else. I used DENSE_RANK() because I needed my row number to change once S_ID changes.
Code:
select
a.S_ID as ID,
DENSE_RANK() OVER (ORDER BY osn.sifra, s.Id asc) as BasicGoodsRowNo, // THIS IS ADDED
osn.sifra as BasicGoodsCode,
osn.naziv as BasicGoods,
null as Pieces,
a.RobaSifra as GoodsCode,
a.Roba as Goods,
a.Detalj as Detail,
a.DetaljDodatak as DetailsAddon
from NP_Stavke s
left join RobaGrupe osn on osn.id = s.RobaId
left join #A a on a.S_ID = s.Id
order by BasicGoodsCode, ID
Lets take a look at how our data looks now(Pic 2)
As you can see we added a Row Number that changes when Id changes.
IMPORTANT: Row Number has to be ether Integer or Decimal in the DataTable that you are using in your report if it's not it will not work correctly.
Step 2:
We've done the 'hard' part now it's time to put Row number in our CrossTab.
When you create a CrossTab or when right click CrossTab and then click on 'Cross-Tab Expert...' it will open a window like this one and in it in the Row section you will insert your Row Number Column(in my case and as you can see in the code above the name of my Row Number Column is 'BasicGoodsRowNo').
Step 3:
Since you don't want to show only the Row number in the report left click on your Row and then click on 'Group Options...'(Pic 4)
Once the new window appears click on 'Options' tab then check the 'Customise group name field' then click on 'Use a formula as group name' and then on 'x-2'(Pic 5)
Step 4:
Enter a formula like this one:
toText( {myTbl.BasicGoodsRowNo}, 0, "" ) + '. ' + {myTbl.BasicGoodsCode} + ' ' + {myTbl.BasicGoods}
Of course your formula will not be exactly like mine since you will not have the same columns as I do. The only part of this formula that you HAVE to have is toText( {myTbl.BasicGoodsRowNo}, 0, "" ) where instead of {myTbl.BasicGoodsRowNo} you will put your row number column. You will need toText since if you dont have that and you want to show a String after your Row Number it will give you an error because RowNumber is an integer field.
GJ YOU ARE ALL DONE AND IT WASN'T THAT HARD WAS IT
How My CrossTab looks once RowNumber is added
Now there is a way to simplify this process and that is:
Step 1:
In your stored procedure create 2 columns. One will show Row Number and the other will show Value that will be displayed as CrossTab row.
Code:
select
a.S_ID as ID,
DENSE_RANK() OVER (ORDER BY osn.sifra, s.Id asc) as BasicGoodsRowNo, // RowNumber
CONVERT(varchar(10), DENSE_RANK() OVER (ORDER BY osn.sifra, s.Id asc)) + '. ' + osn.sifra + ' ' +osn.naziv as BasicGoods, // Value that will be displayed in CrossTab Row
null as Pieces,
a.RobaSifra as GoodsCode,
a.Roba as Goods,
a.Detalj as Detail,
a.DetaljDodatak as DetailsAddon
from NP_Stavke s
left join RobaGrupe osn on osn.id = s.RobaId
left join #A a on a.S_ID = s.Id
order by BasicGoods, ID
As you can see Column BasicGoodsRowNo did not change and will still display the same values as before and I have deleted the clumns BasicGoodsCode and BasicGoods and replaced them with this
CONVERT(varchar(10), DENSE_RANK() OVER (ORDER BY osn.sifra, s.Id asc)) + '. ' + osn.sifra + ' ' +osn.naziv as BasicGoods,
The BasicGoods column will show BasicGoodsRowNo + BasicGoodsCode + BasicGoods.
Step 2:
Step 2 is the same as before.
Step 3:
Once you click on your row and on 'Group Options' go to 'Options' tab again then check the 'Customise group name field' check box again and after that instead clicking on 'Use a formula as group name' click on 'Choose from existing field' and from a combo box select the column you want to show as Row Value in your CrossTab. In my Case that is 'BasicGoods' column (Pic 7).
I used the first method since depending on what user decides I may not show CrossTab at all and I may not show BasicGoods but if you only have CrossTab in your report you can use the second, shorter and easier, method.
If you have any questions feel free to ask.

How to add running ID in a single UPDATE statement (Oracle)

let's assume I have a table tab1 in my Oracle DB 12.1, which has a column record_id (type NUMBER) and many other columns, among them a column named exchg_id.
This record_id is always empty when a batch of new rows gets inserted into the table. What I need to do is to populate the record_id with values 1..N for all rows that satisfy a condition ...WHERE EXCHG_ID = 'something' and number of such rows is N. Of course I know how to do this procedurally (in a for-loop), but I'd like to know if there's an faster way using a single UPDATE statement. I imagine something like this:
UPDATE tab1 SET record_id = {1..N} WHERE exchg_id = 'something';
Many thanks for your help!
UPDATE: the order of the rows is not important, I need no specific ordering. I just need unique record_id's 1..N for any given exchg_id.
You could use rownum to set record_id to 1 to N :
UPDATE tab1 SET record_id = rownum WHERE exchg_id = 'something';
If you have some offset, say 10, then use rownum + 10

LINQ TOP rows and exclude TOP rows

I have to write two LINQ query,which one returns the TOP 6 rows and the another which return data excluding the TOP 6 rows:
TOP 6:
from m in MyTable
take 6
select m.Foo
I need help how to figure out the second query.
Try MyTable.Skip(6). I don't think there is a way to do this in the query syntax. Read more about Take and Skip here and here.
Use Except (according to your C# like syntax):
var fullList = from m in MyTable select m.Foo;
var top6 = from m in MyTable take 6 select m.Foo;
var top6except = fullList.Except(top6);
This exclude the TOP 6 rows retrieved and not generally the top 6 rows. Use Skip method to achieve the generic "select starting from 7th row"
To take all but the first 6 rows, use Skip():
var allButFirst6 = (from m in MyTable
select m.Foo).Skip(6);
Please note that some LINQ providers require the list to be ordered in order to use Skip and Take. If this is the case, then use OrderBy to order the list before using Skip or Take.

Resources