I have a challenging problem.I am developing a web app (.net(C#)) that takes an specific excel file and export an delimited file with specifications explained below.
Here is the excel to be imported:
Loan_ID |Owner_Id | RelPerson_Id | Loan_Class
------------------------------------------------
L131231 | 1234 | 5678 | 0
L135148 | 2345 | 6789 | 2
L417128 | 1234 | 5432 | 1
L271237 | 5678 | 1112 | 5
L213781 | 2345 | 1113 | 4
L098932 | 1117 | 6789 | 6
L012213 | 1122 | 3311 | 2
L398721 | 2221 | 1112 | 3
L098766 | 5552 | | 1
Desired output:
Cust_ID | Cust_class | Gr_ID | Gr.Members | Gr_Class
---------------------------------------------------------------------
1234 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
2345 | 4 | 2 | (2345,1117,6789,1113) | 6
5678 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1117 | 6 | 2 | (2345,1117,6789,1113) | 6
1122 | 2 | 3 | (1122,3311) | 2
6789 | 6 | 2 | (2345,1117,6789,1113) | 6
5432 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
1112 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1113 | 4 | 2 | (2345,1117,6789,1113) | 6
3311 | 2 | 3 | (1122,3311) | 2
2221 | 3 | 1 | (1234,5678,5432,1112,2221) | 5
5552 | 1 | 4 | (5552) | 4
So,i will explain the case in order for you to understand.In the excel file, the first column represents the loan with unique id (no duplicates), the second column represent the Owner of the loan which is an id to identify the customer(Customer_Id).The id of Related person is also a Customer_Id which is the guarantor of the owner in this loan. A customer may have 2 or more loans and can be the guarantor in 2 or more other loans. The guarantor may be the guarantor in 2 or three loans but also may be the owner of another loan.So the owner_id and Rel_PersonId may have duplicates and the Rel_PersonId may have blank values . Class column is the rating or classification of the loan Id (Based on days in delay).
From this file i want the above output.
Cust_id is the distinct Customer Id which will be taken from both columns, Owner, and Rel Persons and will be assigned the highest class(Column :Cust_Class) of the loan he is part of(whether owner or guarantor).I already found a solution for the first two columns of the output, i am stuck with grouping.
Grouping logic:Two customers involved in one loan are grouped together, but if any of this customers is involved in another loan(wether like an Owner or a guarantor) with another Cutomer X, and in another with another customer Y both customers will be in a group with the first two, and if customer x or y are involved in other loans with other customers z and a those are added to the first group with the others too, and this cycle continues till there are no more customer connected with those that are already in the group.See the desired output for understanding.The group id will be generated automatically and sequentially(1,2,3,4) .The group class will be the class of the customer with the highest Cust_class of the group.
I am using sql server to import the excel.Any idea on how to build a query to gain this output or any other solutions?
Hope you can understand, feel free to ask for any further clarification.
Thank you.
Related
I have a table Called Lot# in my database. I used "Order by" to sort from smallest to largest. But it only counts the first digit of the number. I even used "ASC".
Ex...
------------
| Lot # |
------------
| 1 |
| 10 |
| 11 |
| 12 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
------------
Strongly recommend to convert datatype of column to Integer as suggested by #Gusman.
If that is not possile then you can type cast for sorting purpose.
SELECT *
FROM Lot
ORDER BY CAST(column_name AS UNSIGNED)
Without converting, you can also sort the data using the following code:
select * from Lot order by len(column_name), column_name
I have a table dealers which has column token(need to be unique) and has isTokenActive(bool).
I was wandering if there is a way to add unique constraint on a token column but only if there is another row that has same token and isTokenActive is set to 1. If there is a row with same token and isTokenActive is set to 0 that is a valid case too.
Basically I want to allow this
id | Token | isTokenActive |
--------------------------------------
1 | ABC | 0 |
2 | ABC | 1 |
And this
id | Token | isTokenActive |
--------------------------------------
1 | ABC | 0 |
2 | ABC | 0 |
But not this
id | Token | isTokenActive |
--------------------------------------
1 | ABC | 1 |
2 | ABC | 1 |
And it would be great if it is possible to do it code first with EF Core.
I have the following tables:
Sales Table:
+----+------------+
| ID | Date |
+----+------------+
| 1 | 11/20/2018 |
| 2 | 11/21/2018 |
+----+------------+
Items Table:
+----+------------+----------+-------+----------+
| ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----------+-------+----------+
| 1 | 1 | 5 | 100 | 500 |
| 2 | 1 | 3 | 50 | 150 |
| 3 | 2 | 5 | 60 | 300 |
+----+------------+----------+-------+----------+
Currently, I query for the Sale rows and then run another query for each to retrieve all of its Items after which, I initialize them as objects in my C# Program. My problem is that I frequently run into scenarios where I'm dealing with hundreds of sales at the same time. Because of this, I have to run hundreds of queries as well.
My question is, is there a more efficient way to query for all Sales and their Items or is this it? I thought of trying a query like
SELECT *
FROM items_table
WHERE FK_Sale_ID = '1' OR FK_Sale_ID = '2'
And then manually sorting which Item belonged to which sale for initialization but this query quickly got messy after dealing with more than a few sales. Any ideas?
You can use join clause to combine multiple tables data in your query:
SELECT *
FROM items_table
JOIN sales_table ON items_table.FK_Sale_ID = sales_table.ID
WHERE sales_table.Date = #somedate
With this query when #somedate = 11/20/2018 you basically will get this data in one go:
+----+------------+----+------------+----------+-------+----------+
| ID | Date | ID | FK_Sale_ID | Quantity | Price | Subtotal |
+----+------------+----+------------+----------+-------+----------+
| 1 | 11/20/2018 | 1 | 1 | 5 | 100 | 500 |
| 1 | 11/20/2018 | 2 | 1 | 3 | 50 | 150 |
+----+------------+----+------------+----------+-------+----------+
Use SELECT items_table.* to get all fields of items table only
I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue
I have a DB table which has some data as follows:
LINE | QTY | USERID | DATE
-----------------------------------------
1 | 5 | qb1 | 2015-03-02 11:23:25
2 | 1 | qb2 | 2015-03-02 18:24:03
3 | 3 | ch1 | 2015-03-03 05:38:49
1 | 2 | qb1 | 2015-03-03 08:47:02
2 | 4 | qb2 | 2015-03-03 14:01:31
3 | 2 | ch1 | 2015-03-03 21:11:53
1 | 4 | qb1 | 2015-03-04 09:34:04
2 | 5 | qb2 | 2015-03-04 15:29:27
3 | 1 | ch1 | 2015-03-04 19:28:33
As you can see I have only 3 unique LINE values in the DB. I require a LINQ query to select the latest record of every line. The date can be any date, I just need the latest status of the lines based on "DATE" field.
At the moment I am doing it very roughly something like this:
var line1 = db.GetTable<lnk_sts>().Where(x=> x.LINE== 1).OrderByDescending(x => x.DATE).FirstOrDefault();
Same for the other 2. What I Require is a list of lnk_sts with only the ones with a bigger date, in this case:
LINE | QTY | USERID | DATE
---------------------------------------
1 | 4 | qb1 | 2015-03-04 09:34:04
2 | 5 | qb2 | 2015-03-04 15:29:27
3 | 1 | ch1 | 2015-03-04 19:28:33
What you need to do is, group on Line and then take the first item in group after ordering in descending.
db.GetTable<lnk_sts>()
.GroupBy(x=>x.LINE)
.Select(x=>x.OrderByDescending(o=>o.DATE).FirstOrDefault())
.ToList();
First group by the line, then order by date and take the last item.
var result = db.GetTable<lnk_sts>().GroupBy(x=> x.LINE).Select(x => x.OrderBy(y => y.Date).Last())