Why the implicit CASTing is happening during SQL Join? - sql

I have two tables T1 and T2.
T1 has 3 columns t1c1, t1c2, t1c3 - all are of String types.
T2 has 4 columns t2c1, t2c2, t2c3, t2c4 - all are of String types.
I'm trying to perform a join as:
SELECT T1.t1c1, T1.t1c2, T1.t1c3, T2.t2c1, T2.t2c2, T2.t2c3, T2.t2c4
FROM T1, T2
WHERE T1.t1c1 = T2.t2c1 & T1.t1c2 = T2.t2c2;
But this is throwing me an error like this:
AnalysisException: cannot resolve '(CAST(T2.t2c1 AS DOUBLE) & CAST(T1.t1c2 AS DOUBLE))' due to data type mismatch: '(CAST(T2.t2c1 AS DOUBLE) & CAST(T1.t1c2 AS DOUBLE))' requires integral type, not double;
Then I tried this one:
SELECT T1.t1c1, T1.t1c2, T1.t1c3, T2.t2c1, T2.t2c2, T2.t2c3, T2.t2c4
FROM T1, T2
WHERE CAST(T1.t1c1 AS STRING) = CAST(T2.t2c1 AS STRING) & CAST(T1.t1c2 AS STRING) = CAST(T2.t2c2 AS STRING);
Then this error comes:
AnalysisException: cannot resolve '(CAST(CAST(T2.t2c1 AS STRING) AS DOUBLE) & CAST(CAST(T1.t1c2 AS STRING) AS DOUBLE))' due to data type mismatch: '(CAST(CAST(T2.t2c1 AS STRING) AS DOUBLE) & CAST(CAST(T1.t1c2 AS STRING) AS DOUBLE))' requires integral type, not double;
I want to work with StringType. How can I resolve this problem? And why this implicit casting is happening?

Related

Unable to cast a object of type 'WhereSelectEnumerableIterator`2 [VB $ AnonymousType_8`9

I have written a query by using the linq to Datatable, but I don't know what is the type to use to declare my variable queryArchi? I have tried the type System.Data.EnumerableRowCollection(Of Object) but I get a message error :
Unable to cast a object of type 'WhereSelectEnumerableIterator2 [VB $ AnonymousType_89
Code:
Dim queryArchi As System.Data.EnumerableRowCollection(Of Object)
Dim queryStructure
If tf = 0 Then
queryArchi = (From b In DataBien.AsEnumerable()
Group b By b!code_projet, b!code_operation, b!code_type, b!Designation_part, b!code_part Into Group
Select code_projet, code_operation, code_type, NBP = Group.Count())
Else
queryArchi = (From b In DataBien.AsEnumerable()
Group b By b!code_projet, b!code_operation, b!code_type, b!Designation_part, b!code_part, b!Titré Into Group
Select code_projet, code_operation, code_type, Titré, NBP = Group.Count())
End If
If vil <> "" Then queryArchi = queryArchi.Where(Function(d) d.code_projet = vil)
queryStructure = queryArchi.ToList

how to i typecast string to integer - Postgres?

This query, return this error.
operator does not exist: text = integer LINE 2: from mas_book ) as
outp where outp.authors = 2 ^ HINT: No operator matches the given name
and argument type(s). You might need to add explicit type casts
public function authors($ids)
{
$query = "select count(authors) from (select distinct regexp_split_to_table(author, E',') as authors
from mas_book ) as outp where outp.authors = ".$ids;
$result = $this->db->query($query);
return $result->result();
}
If outp.authors value can be converted into Integer then You can use :
"select count(authors) from (select distinct regexp_split_to_table(author, E',') as authors
from mas_book ) as outp where to_number(outp.authors,'9999999') = ".$ids;
If It is not work then See this Link
SELECT COUNT(authors) FROM (SELECT DISTINCT regexp_split_to_table(author, E',') as authors
FROM mas_book ) as outp WHERE outp.authors = ".'cast(.'$ids'. as character varying);

Entity framework : Using summary functions inside a Projection

I'm trying to run the following query :
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Sum(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Sum(Function(t3) t3.value) _
}).ToList
( in this query .quantity and .value have Decimal type.)
but I'm getting this error on runtime :
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
Additional information: The cast to value type 'System.Decimal' failed because the materialized value is null.
Either the result type's genericparameter or the query must use a nullable type.
It's sure that the collection child1 has items that have .tp=2.
What's wrong ?
Thank you !
Updated :
these are the tables on database :
MyObj1:
Id name
2 name1
7 name7
8 name8
Child1:
ID ParentID TP Quantity Value
1 2 2 7 9
2 7 2 20 10
3 7 2 8 11
( ParentID is the forign key for child1 related to ID field on MyObj )
Also , I try the query like this :
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Count(Function(t3) t3.quantity), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Count(Function(t3) t3.value) _
}).ToList
and has no problem. so I think maybe the problem is the SUM function.
Update :
This is working without errors :
Dim lst = (From t In context.MyObj1 where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.AsQueryable.Where(Function(t2) t2.tp=2).Sum(Function(t3) Ctype(t3.quantity,System.Nullable(of Decimal)), _
.vlh = t.child1.AsQueryable.Where(Function(t3) t3.tp=2).Sum(Function(t3) Ctype(t3.value,System.Nullable(of Decimal)) _
}).ToList
But I have problems because this method doesn't return any value on the Sums for those parent's items that doesn't have any child in Child1 collection , for example For the Item on Myobj1 with id=8 there's no child1's item , but in this case I want to return a 0 as a sum.
What can I do ?
Thank you !
Try this:
Dim lst = (From t In context.MyObj1
Where t.id > 6
Where Not (t.child1 Is Nothing)
Select New With {}).ToList
Hard to tell with just the code you've posted, but it appears something before you get into the LINQ statements is already null (i.e., Nothing).
EDIT
Sorry, just couldn't hack it in VB anymore ... switching to C# - hoping this is what you're looking for (because it's EF, I don't have an actual DB, and don't have time to set up an in-memory data store, it's not tested with your actual data):
(from t in context.MyObj1s
where t.Id > 6
from c in context.Child1s
where c.ParentId == t.Id
where c.Tp == 2
group new { Quantity = c.Quantity, Value = c.Value } by t into g
select new
{
Parent = g.Key,
Sash = g.Sum(x => x.Quantity),
Vlh = g.Sum(x => x.Value),
}).ToList();
This avoids passing the child1 navigation property on MyObj1 into a context where it's trying to convert IQueryables into SQL, which child1 is not (directly).
The cast to nullable decimals is necessary because of the null values.
If you want zeros in stead of null values you have to add DefaultIfEmpty:
Dim lst = (From t In context.MyObj1 _
where t1.id>6 Select New With { _
.Parent = t, _
.sash = t.child1.Where(Function(t2) t2.tp=2) _
.Select(Function(t3) t3.quantity), _
.DefaultIfEmpty().Sum(), _
.vlh = t.child1.Where(Function(t3) t3.tp=2) _
.Select(Function(t3) t3.value) _
.DefaultIfEmpty().Sum() _
}).ToList
This return an IEnumerable with a 0 value when there are no results in the subqueries.

Entity framework vb.net error in translating query

I am trying to convert this query:
select sales.TTL_Sales_Net, sales.TTL_Sales_Target, units.TTL_Units_Net
from epos_Anal_Branch_Sales_Day sales
join epos_PULL_PM_SKU_STATUS till on sales.BRANCH_ID = till.BRANCH_ID
join epos_Anal_Branch_Units_Day units on sales.BRANCH_ID = units.BRANCH_ID
where till.BRANCH_ID = 45 and sales.DATEKEY = 20140725 and units.DATEKEY = 20140725
from SQL into an Entity Framework query in VB.net. I am sending in the BRANCH_ID in my function and producing the datekey like so:
Dim MyDate As String = Date.Now.ToString("yyyyMMdd")
This is what I have but it just returns no results and I am at a complete loss as to why! This is my query thus far, if anyone can spot what I have done wrong then I would be grateful for a push in the right direction
Public Class clsStoreSales
Public Property DailyAmount As String
Public Property DailyUnits As String
Public Property SalesTarget As String
Public Property ErrorMessages As String
Public Shared Function GetStoreSales(ByVal BranchID As String) As List(Of clsStoreSales)
Dim live As New RMISEntitiesLive
Dim MyDate As String = Date.Now.ToString("yyyyMMdd")
Dim l As New List(Of clsStoreSales)
Try
Dim r = From till In live.epos_PULL_PM_SKU_STATUS
Join sales In live.epos_Anal_Branch_Sales_Day On till.BRANCH_ID Equals sales.BRANCH_ID
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.DATEKEY
Where till.BRANCH_ID = CDbl(BranchID) AndAlso sales.DATEKEY = CDbl(MyDate) AndAlso units.DATEKEY = CDbl(MyDate)
Select New With {.SalesAmount = sales.TTL_Sales_Net, .SalesTarget = sales.TTL_Sales_Target, .SaleUnits = units.TTL_Units_Net}
For Each t In r
Dim m As New clsStoreSales
m.DailyAmount = CStr(t.SalesAmount)
m.DailyUnits = CStr(t.SaleUnits)
m.SalesTarget = CStr(t.SalesTarget)
l.Add(m)
Next
Return l
Catch ex As Exception
Dim e As New clsStoreSales
e.ErrorMessages = ex.ToString
l.Add(e)
Return l
End Try
End Function
End Class
This is what sqlServer Profiler is showing as the query being run:
exec sp_executesql N'SELECT
[Extent1].[BRANCH_ID] AS [BRANCH_ID],
[Extent2].[TTL_Sales_Net] AS [TTL_Sales_Net],
[Extent2].[TTL_Sales_Target] AS [TTL_Sales_Target],
[Extent3].[TTL_Units_Net] AS [TTL_Units_Net]
FROM [dbo].[epos_PULL_PM_SKU_STATUS] AS [Extent1]
INNER JOIN [dbo].[epos_Anal_Branch_Sales_Day] AS [Extent2] ON [Extent1].[BRANCH_ID] = [Extent2].[BRANCH_ID]
INNER JOIN [dbo].[epos_Anal_Branch_Units_Day] AS [Extent3] ON [Extent1].[BRANCH_ID] = [Extent3].[DATEKEY]
WHERE ( CAST( [Extent1].[BRANCH_ID] AS float) = #p__linq__0) AND ( CAST( [Extent2].[DATEKEY] AS float) = #p__linq__1) AND ( CAST( [Extent3].[DATEKEY] AS float) = #p__linq__2)',N'#p__linq__0 float,#p__linq__1 float,#p__linq__2 float',#p__linq__0=45,#p__linq__1=20140725,#p__linq__2=20140725
This
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.DATEKEY
Should be this!
Join units In live.epos_Anal_Branch_Units_Day On till.BRANCH_ID Equals units.BRANCH_ID

Linq To Sql left join using multiple join clauses

For some reason I am getting a syntax error on the below code. What I am trying to achieve is
A LEFT JOIN with MULTIPLE JOIN CLAUSES. The syntax error occurs at the Keyword INTO foobar. VS 2012 says unexpected Token. Any help would be great, thank you !
Dim results = From f In foo _
Join b In bar On new with {f.Type,f.ID} Equals New With {"Test",b.ID} into fooBar _
from x in foobar.DefaultEmpty() _
Where foo.id = 1
You want a Group Join:
Dim results = From f In foo _
Group Join b In bar On
New With {f.Type,f.ID} Equals New With {"Test",b.ID} _
Into fooBar = Group _
from x in foobar.DefaultEmpty() _
Where foo.id = 1
Try to cast the f.ID or b.ID
Dim results = From f In foo _
Join b In bar On new with {f.Type, CInt(f.ID)} Equals New With {"Test", CInt(b.ID)} into fooBar _
from x in foobar.DefaultEmpty() _
Where foo.id = 1