How to compare two columns not having same value using sequelize orm - database

I have two fields in my table dispatchCount & qty.
I want to findOne tuple where dispatchCount is not equal to qty
I want to do something similar to this (Mysql Select Rows Where two columns do not have the same value) but using sequelize ORM.
I don't want to write the raw query myself bcz there are a lot of aliases & things like that. So how can I do the following using sequelize
SELECT *
FROM my_table
WHERE column_a != column_b

The following is the solution without writing raw query to do so :-
let en = Entity.findOne({
where: {
dispatchCount : {
[Op.ne]: sequelize.col("qty");
}
}
})

Related

SOQL Query for Left Join for custom objects

I have a requirement to fetching data from Sales force. I need to get the data from two custom objects. I
have written query in sql can anyone help me to convert it into SOQL
SELECT ID, Name, Crop_Year__c, Targeted_Enrollment_Segments__c, Description__c, Start_Date__c,
End_Date__c from Enrollment_Program__c EP
Left Join Account_Enrollment__c AE on EP.Crop_Year__c = AE.Crop_Year__c and EP.ID =
AE.Enrollment_Program__c
where AE.Account__c = 'xyz'
As you probably know, Salesforce SOQL doesn't have explicit JOIN clauses. It does that for you implicitly based on related object fields. That means you'll have to query Account_Enrollment__c and traverse the fields to get the related Enrollment_Program__c Lookup relationship.
Another problem is Salesforce only performs joins based on primary and foreign keys, so the EP.Crop_Year__c = AE.Crop_Year__c in your query won't work.
So, with that said, you can try this:
SELECT Enrollment_Program__c, Enrollment_Program__e.Name,
Enrollment_Program__r.Crop_Year__c, Enrollment_Program__r.Targeted_Enrollment_Segments__c,
Enrollment_Program__r.Description__c, Enrollment_Program__r.Start_Date__c,
Enrollment_Program__r.End_Date__c
FROM Account_Entrollment_Program__c WHERE Account__c = 'zyz'
If you know beforehand what the Crop_Year__c value is, you can just add this to your query:
AND Crop_Year__c=:year AND Enrollment_Program__c.Crop_Year__c=:year
Some details on the queries:
The __r suffix is how you get the lookup object addressed in the query. If you are interested only in the id, you can use __c.
The :year is how you pass the parameter year to the query. If you want to append it as text you can just use ... Crop_Year='+ year + '.

Postgresql 9.5 JSONB nested arrays LIKE statement

I have a jsonb column, called "product", that contains a similar jsonb object as the one below. I'm trying to figure out how to do a LIKE statement against the same data in a postgresql 9.5.
{
"name":"Some Product",
"variants":[
{
"color":"blue",
"skus":[
{
"uom":"each",
"code":"ZZWG002NCHZ-65"
},
{
"uom":"case",
"code":"ZZWG002NCHZ-65-CASE"
},
]
}
]}
The following query works for exact match.
SELECT * FROM products WHERE product#> '{variants}' #> '[{"skus":[{"code":"ZZWG002NCHZ-65"}]}]';
But I need to support LIKE statements like "begins with", "ends width" and "contains". How would this be done?
Example: Lets say I want all products returned that have a sku code that begins with "ZZWG00".
You should unnest variants and skus (using jsonb_array_elements()), so you could examine sku->>'code':
SELECT DISTINCT p.*
FROM
products p,
jsonb_array_elements(product->'variants') as variants(variant),
jsonb_array_elements(variant->'skus') as skus(sku)
WHERE
sku->>'code' like 'ZZW%';
Use DISTINCT as you'll have multiple rows as a result of multiple matches in one product.

How do I query SQL Server for a record with multiple key/value pair constraints?

I'm using Entity Framework, and have an entity that has a collection of key/value pairs. I want to be able to search for an entity based on those pairs.
Here are my tables:
StoredDocuments:
StoredDocumentKeywordValues:
So if I searched for entities with key k1 and value v1, I'd want StoredDocumentIds 1 and 2.
If I search for k1/v1, k2/v2, I'd want only StoredDocumentId 1
If I search for k1/v1, k4/v4, I'd get nothing
A linq statement would be ideal, but I couldn't figure it out. Closest I got using SQL was ORing together all the individual key/value statements, but the case where a key/value didn't exist threw me. What I really need is all the key/value pairs ANDed together:
If a StoredDocument has K1,V1 AND K2,V2 AND K3,V3 ...
Sorry I don't know EF
The challenge is where condition is for single row and the single row does not have multiple values
In TSQL
select storedDocumentID
from table
where (keywork = 'k1' and value = 'v1')
or (keywork = 'k2' and value = 'v2')
group by storedDocumentID
having count(*) = 2
You can also do it with Intersection and Join but I think this is easier
If LINQ won't do it directly you could output the out in hashsets and then take the intersection of the hashsets.
var query = db.StoredDocumentKeywords;
foreach (var kv in kvlist)
{
query = query.where (p => p.keyword == kv.Key && p.Value == kv.Value)
}
return query.Select(p => p.StoredDocumentId).Distinct();

SQL Server 2008 R2 STcontains spatial join using two tables

I've been stabbing at this for a while and am getting nowhere, so I'm hoping that someone with greater skills than I might have the answer.
I have two tables and in one is a set of latitude and longitude coordinates as separate columns. In the second able I have polygon shapes set in to a spatial geometry column.
The goal is to select all of the latitude and longitude pairs from table 1, which might be called separately as:
SQLSTRING = "SELECT LAT,LONG FROM dbo.Table1;"
The second table can be called using a scripting language loop to parse through each result one by one by using the following query:
SQLSTRING = "SELECT * FROM dbo.Table2 a WHERE a.POLY.STContains(geometry::STPointFromText('POINT(" & -Text Longitude Value from Table 1- & " " & -Text Latitude Value from Table 1- & ")',0))=1;"
So, my dilemma is that it surely would be possible to select all items from Table 1 and run them through a query that will only return those results where the latitude and longitude from table 1 are contained within any specified polygon stored in table 2. The scripting language loop is so obviously inefficient, so a single SQL query that could replace this and just return any matches would be a major time and resource saver.
Any help or pointers would be most gratefully appreciated. Thank you, in advance, for your advice.
Since you're working with spatial data, you can do a cross join (join all the rows from both tables together), then filter out what matches.
SELECT *
FROM dbo.Table2 AS a
, dbo.Table1 AS b
WHERE a.POLY.STContains(geometry::STPointFromText('POINT('+CAST(b.LONG AS VARCHAR)+' '+CAST(b.LAT AS VARCHAR)+')',0))=1;
One problem with performance here is that it will need to generate the geometry object repeatedly. It would be better if you could create a column to hold the geometry for table1. Make sure you have an spatial index on POLY in Table2 also.

How to get the table name from a field in a join query with MSSQL? (mysql_field_table equivalent)

I'm doing a query manager in Delphi using ADO, I need to know all fields that will be returned by a query, does no matter how complex and how much joins they will have. I want to call a function, that return to me, all fields that will be returned in a specific query, and fields information, like table what is this field from.
In mysql with php, I have the command mysql_field_table, in this command I pass the result object and the field index and this command return the table name for me.
Well, that is my dream, get the table name from a field index in a query like:
SELECT * FROM TableOne Left Join Table2 ON Table2.MasterField = Table1.KeyField
You could use a TADODataSet to fetch the Recordset, iterate the Fields collection, and get the tables/fields names like this:
for I := 0 to ADODataSet1.Recordset.Fields.Count - 1 do
begin
TableName := ADODataSet1.Recordset.Fields[i].Properties['BASETABLENAME'].Value;
FieldName := ADODataSet1.Recordset.Fields[i].Properties['BASECOLUMNNAME'].Value;
end;

Resources