0

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
7
  • 1
    Have you profiled the server to see what query it is actually producing in SQL? Commented Jul 25, 2014 at 11:53
  • Are you sure CDbl(MyDate) is giving you the correct number? Commented Jul 25, 2014 at 11:56
  • Also, don't you have to use underscores at the end of the line to have multiline statements? Commented Jul 25, 2014 at 11:59
  • @Charleh, thanks for this tip, looking now! Commented Jul 25, 2014 at 12:06
  • @Charleh added the profiler info, still cant see whats wrong :-( Commented Jul 25, 2014 at 12:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.