SELECT EXISTS(SELECT 1 FROM table WHERE...) in Exposed - kotlin-exposed

Is it possible to do something like this in Exposed to check the existence of a row?
SELECT EXISTS(SELECT 1 FROM table WHERE...)

There is exists function in Exposed:
FooTable.select {
exists(BarTable.select { BarTable.id eq FooTable.id })
}

You can achieve it by calling QueryBuilder, but I wish I could use more DSL descriptions.
val result: Int = transaction {
addLogger(StdOutSqlLogger)
val qb = QueryBuilder(false).append("SELECT ").append(
exists(FooTable.select { FooTable.id eq 111 })
)
TransactionManager.current().exec(qb.toString()) {
it.next()
it.getInt(1)
} ?: 0
}

Related

Drilldown method always return null with Cube JS

I have a problem with cubejs library. when i call the method drilldown from a measure.
i was puting the drill members field on my schema but always return null.
this is my measure from the schema
compromisoFinalMuyAlta: {
sql: `
CASE
WHEN (
(sum(
CASE
WHEN (${CUBE}.id_tipo_cobertura = 4 AND ${CUBE}.id_bi_rango_sla = 5) THEN ${CUBE}.total_cuentas_rango
END
) - ${inactivasTotalMuyAlta}) > 0
) THEN ROUND(SUM(
CASE
WHEN (${CUBE}.id_tipo_cobertura = 4 AND ${CUBE}.id_bi_rango_sla = 5) THEN ${CUBE}.total_cuentas_rango
END
)) - ${inactivasTotalMuyAlta}
ELSE 0
END
`,
type: `number`,
title: 'Resultado final',
drillMembers:[`${CUBE}.id_tipo_cobertura`,`${CUBE}.total_cuentas_rango`]
}
this the method what i have the resultset and try to call the drilldown method
public BuscarDetalle(columnValue) {
console.log(this.resultQuery);
const queryDetail = this.resultQuery.drillDown({
xValues: ["0 - 3 días"],
yValues: ["compromiso_real.id_cliente", "SlaCompromisos.compromisoFinalMuyAlta"]
});
console.log(queryDetail);
}
and this is the table from cube.
enter image description here
First of all, Thanks.

Why I am getting "Too many DML Statements" error when performing Update operation in LWC salesforce?

Why I am getting 'TOO MANY DML STATEMENTS ERROR IN THIS LINE?'
Note: Class is auraenabled(cacheable =true)
List<ABC> obj1 = new List<ABC>();
for (ABC A1 : [SELECT ID,field1, field2, field3, (SELECT Id, Name,ParentId,CreatedDate FROM Attachments WHERE Name LIKE 'something' ORDER BY CreatedDate DESC Limit 1) FROM ABC WHERE field5ID = 'field5ID' ORDER BY field1]) {
if(!A1.Attachments.isEmpty()) {
for (Attachment Att : A1.Attachments) {
MAP1.put(A1.field1,A1.Id);
if(A1.field2 = true){
A1.field2 = false;
Obj1.add(A1) ;
}
}
}
}
if(Obj1.size() > 0){
update Obj1; //GETTING ERROR ON THIS LINE
}

How can I apply NOW() along with Exposed DAO

I have such a table.
object AsdTable : IntIdTable("asd") {
val created_date = timestamp("created_date")
val code = varchar("code", 64)
}
And I'd like to build sql insert statement like INSERT INTO asd(created_date, code) VALUES(NOW(), ??)
Kotlin code is
transaction {
AsdEntity.new {
createdDate = ?
code = "oook"
}
}
How this can be achieved?
I managed to make it work via defaultExpression:
object AsdTable : IntIdTable("asd") {
val created_date = timestamp("created_date").defaultExpression(CurrentTimestamp())
val code = varchar("code", 64)
}
transaction {
AsdEntity.new {
code = "OK"
}
}
NOW() or CURRENT_TIMESTAMP() is generated within INSERT statement.

How to find select distinct values from mongodb

How can I use find select distinct in angularJS then counting it, I'm new to mean-stack and I don't know how to use distinct method.
Here is my table structure with sample data:
donation_no:"3124"
donors_name:"Jeremy Adrian De Vera"
date:"12/26/2018"
time:"8:30"
blood_group:"B"
volume:"2"
branch:"Pasig"
I already tried it using php like this but how can I do this in angular:
$query = "SELECT DISTINCT fruits, COUNT(*) as counter FROM my_table WHERE date_tested BETWEEN '$date_from' AND '$date_to' GROUP BY fruits";
Here is my code for finding the values from my db
api.js
router.get('/blooddonationmanagement', function(req, res) {
Blooddonation.find({},function(err, blooddonations) {
res.json({ success: true, blooddonations: blooddonations });
});
});
blooddonationCtrl.js
angular.module('blooddonationControllers', [])
.controller('blooddonationCtrl', function(Blooddonation,$scope) {
var app = this;
function getBlooddonations() {
Blooddonation.getUsers().then(function(data) {
app.blooddonations = data.data.blooddonations;
});
}
chapters.html
<tr ng-repeat="person in blooddonationmanagement.blooddonations">
<td>{{ person.branch}}</td>
</tr>
How can I count the distinct values then counting it from my db, Im expecting like this:
branch count
Pasig 29
Manila 16
Pasay 19
You should try distinct()
Blooddonation.distinct('branch',... );
Just read the docs here: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/
You can use:
db.collection.distinct()

LINQ orderby int array index value

Using LINQ I would like to sort by the passed in int arrays index.
So in the code below attribueIds is my int array. I'm using the integers in that array for the where clause but I would like the results in the order that they were in while in the array.
public List BuildTable(int[] attributeIds)
{
using (var dc = new MyDC())
{
var ordering = attributeIds.ToList();
var query = from att in dc.DC.Ecs_TblAttributes
where attributeIds.Contains(att.ID)
orderby(ordering.IndexOf(att.ID))
select new Common.Models.Attribute
{
AttributeId = att.ID,
DisplayName = att.DisplayName,
AttributeName = att.Name
};
return query.ToList();
}
}
I would recommend selecting from the attributeIDs array instead. This will ensure that your items will be correctly ordered without requiring a sort.
The code should go something like this:
var query =
from id in attributeIds
let att = dc.DC.Ecs_TblAttributes.FirstOrDefault(a => a.ID == id)
where att != null
select new Common.Models.Attribute
{
AttributeId = att.ID,
DisplayName = att.DisplayName,
AttributeName = att.Name
};
Why don't you join:
public List BuildTable(int[] attributeIds)
{
using (var dc = new MyDC())
{
var query = from attID in attributeIds
join att in dc.DC.Ecs_TblAttributes
on attID equals att.ID
select new Common.Models.Attribute
{
AttributeId = attID,
DisplayName = att.DisplayName,
AttributeName = att.Name
};
return query.ToList();
}
}

Resources