How to count number of same data in a column - sql-server

I have a table:
Id Catg
1 cat01
2 cat01
3 cat01
1 cat02
2 cat02
now I want to detect number of occurance of catg01 and catg02,like in this ex, catg01 is 3 times and catg02 is 2 times,I want to count that through LINQ/ simple db query.
Pls note: cant use Where clause and hardcode Catg01/catg 02,as there can n number of category.Is it possible to detect? if yes then pls help.

SELECT Catg, COUNT(*)
FROM myTable GROUP BY Catg

Select Catg, Count(*) From TableName Group By CatG
For the LINQ Version. Imagine a Class
class Table
{
public int ID { get; set; }
public string CatG { get; set; }
}
Then if you had a list of that class, you could query it as follows
List<Table> y = new List<Table>();
y.Add(new Table() { ID = 1, CatG = "a" });
y.Add(new Table() { ID = 2, CatG = "a" });
y.Add(new Table() { ID = 3, CatG = "a" });
y.Add(new Table() { ID = 4, CatG = "b" });
y.Add(new Table() { ID = 5, CatG = "b" });
var query = y.GroupBy(table => table.CatG);
// Iterate over each IGrouping in the collection.
foreach (var item in query)
{
Console.WriteLine("CatG: {0} Number of Items: {1}", item.Key, item.Count());
}

Related

Get Data from Two Tables Floor Flutter

I want to get data from two tables using floor in flutter, how can I do that?
this code for getting data from one table. this function inside database.g.dart:
Stream<List<Item>> getItems() {
return _queryAdapter.queryListStream(
'SELECT * FROM Item WHERE is_active = 1',
mapper: (Map<String, Object?> row) => Item(
id: row['id'] as int?,
item_id: row['item_id'] as String,
description: row['description'] as String,
salePrice: row['sale_price'] as double,
purchasePrice: row['purchase_price'] as double,
isActive: row['is_active'] as int),
queryableName: 'Item',
isView: false);
}
So the return will be Item objects but what if I have junction table from another tables and the command will be like this:
SELECT junctiont.*, item.*, client.*
FROM item
INNER JOIN junctiont
ON item.id = junctiont.item_id
INNER JOIN client
ON junctiont.client_id = client.id
How the function will be? and what will return?.
I found the answer by create new object to return the whole coming columns or you can return to list
#override
Stream<List<List>> getBillsMainInfo() {
return _queryAdapter.queryListStream(
'SELECT Bills.id, Biils.bill_number, Bills.total, Bills.datetime, Clients.name FROM Bills INNER JOIN Clients ON Bills.client_id = Clients.id WHERE Bills.is_active = 1',
mapper: (Map<String, Object?> row) => [
row['id'] as int,
row['bill_number'] as int,
row['datetime'] as String,
row['name'] as String,
row['total'] as double
],
queryableName: 'Bills',
isView: false);
}

How set id via Exposed

I create object and dao class for work with sql
object UserTable : IdTable<Int>("User") {
val parameters = reference("search_parameters_id", SearchParametersTable)
override val id = integer("id").entityId()
override val primaryKey = PrimaryKey(id)
}
class User(id: EntityID<Int>) : Entity<Int>(id) {
companion object : EntityClass<Int, User>(UserTable)
var searchParameters by SearchParameters referencedOn UserTable.parameters
}
But I cann't set id, beacause id is val
Do you mean that you want to insert a record with an arbitrary id
value? If yes, you can write like below.
val newId = 10
User.new(newId) {
// set values to other columns
}

Compare Two Different Structured Arrays in Swift3

I am new to IOS as well as Swift Language, now I am working in Swift3.
DetailsArray:
[{
bookId = abcd;
bookName = "MyBook";
bookThumbImage = ".jpg"
},
{
bookId = efgh;
bookName = "MyBook1";
bookThumbImage = "bookefgh.jpg"
},
{
bookId = ijkl;
bookName = "MyBook2";
bookThumbImage = ".jpg"
}
]
When i print my Existing IdListArray Object is in the below given format,
IdListArray:
▿ Optional<"NSMutableArray">
▿ some : 2 elements
- 0 : abcd
- 1 : ijkl
Now i need to match these two Arrays (IdListArray & DetailsArray), to get the matched row record from my DetailsArray
Required Output:
[{
bookId = abcd;
bookName = "MyBook";
bookThumbImage = ".jpg"
},
{
bookId = ijkl;
bookName = "MyBook2";
bookThumbImage = ".jpg"
}]
Thanks,
Edited: As per your Requirement
You can use this code:
var arrMatchingId:NSMutableArray = NSMutableArray()
var arrNotMatchingId:NSMutableArray = NSMutableArray()
for data in arrList{
let id = (data as! NSDictionary).value(forKey: "bookId")
if arrID.contains(id!){
arrMatchingId.add(data) //Id Matched
}else{
arrNotMatchingId.add(data)// Id Not Matched
}
}
print(arrMatchingId) // This is the array with matched ID
print(arrNotMatchingId) //This is the array with Unmatched array

LINQ : filter multiple string values - partial match - not full match as in multiple online examples

There are many good examples of searching multiple string values in LINQ e.g.
public static Product[] GetProducts(Guid[] prodIDs)
{
return (from p in GetProducts()
where prodIDs.Contains(p.ProductID)
select p).ToArray<Product>();
}
I have a list of Products that I need to match from a customer,
but I dont have an exact match - the Customers List Of Products contains my ProductID - but it is not exact - e.g.
Customer MyCompany
Description Description
Prod1XY Prod1
AProd2B Prod2
XXXProd3 Prod3
I thus cannot filter from the prodIDs [string array] because Prod1 does not contain Prod1XY
and thus cannot use the examples that are available.
How can I effectively change (reverse) the working examples
as to search CustomerProducts where it contains my Product Description please?
So to confirm : this is not a duplicate. The examples use the string[] x
input parameter and then searches:
where x.contains
I need help to get it : myProducts.Contains(x)
another online example modified to show the situation:
static void Main(string[] args) {
var table = new[] {
new { uid = 1 },
new { uid = 2 },
new { uid = 3 },
new { uid = 4 },
new { uid = 5 }
};
var stringarray = new[] { "1", "5", "10" };
var results = from xx in table
where table.Contains(stringarray)
select xx;
foreach (var result in results) {
Console.WriteLine("Result: " + result.uid.ToString());
}
}
It is not clear enough what you are trying to accomplish, but under assumption that you want to select all products where ProductID contains any value from specified list, it looks like that it:
public static Product[] GetProducts(string[] prodIDs)
{
return (from p in GetProducts()
where prodIDs.Any(id=>p.ProductID.Contains(id))
select p).ToArray<Product>();
}
Try this
public static Product[] GetProducts(string[] prodIDs)
{
return (
from p in GetProducts()
from q in prodIDs
where p.ProductID.IndexOf(q) > -1
select p)
.ToArray<Product>();
}

Angular UI-Grid Pivot

I have a collection of these Javascript objects: (Displayed as a DTO)
public class ParameterValueDTO : DataTransferObject
{
public int Id { get; set; }
public String Comments { get; set; }
public String Description { get; set; }
}
By default, AngularJS UI-Grid will create a row for each ParameterValue object with 3 columns: Id, Comments, Description which works fine.
IMAGE: Standard objects mapping to table
What I would like to do however is create a column for each object's "Comments" value and bind it to the corresponding "Description" value. Essentially pivoting the table so it only has 1 row (forget the ID column for now).
The javascript I've tried:
var cols = [];
var row = obj.data.ProductAttributes[0].Specifications[0].ParameterValues
var length = row.length;
for (var i = 0; i < length; i++) {
cols.push({
field: "Description",
displayName: row[i].Comments
});
}
$scope.gridOptions = {
columnDefs: cols,
data: row
};
The above results in the following which is obviously wrong:
IMAGE: One column, new row for each Description
Is it possible to accomplish this with the current data structure or what exactly is the correct approach I should be taking?
I'd modify the code to just reprocess your data. ui-grid really only likes to bind to columns and rows, it can't reverse the order.
So you'd:
var pivotData = [ {} ];
data.forEach(function(parameterDTO) {
pivotData[0][parameterDTO.comments] = parameterDTO.description;
});
Then you should be able to use that new data object as grid data.

Resources