Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:13:43+00:00 2026-06-01T11:13:43+00:00

I have a particular scenario where I wrote my code using LINQ-SQL but I

  • 0

I have a particular scenario where I wrote my code using LINQ-SQL but I would do it … but the problem is that I can not convert it! Can anyone give me a hand? thanks

Here is the query

INSERT

SET NOCOUNT ON;
USE tempdb;
GO
CREATE TABLE dbo.Employe (
  Id int NOT NULL PRIMARY KEY,
  Name varchar(10) NOT NULL
  );
CREATE TABLE dbo.Customer (
  Id int NOT NULL PRIMARY KEY,
  Name varchar(10) NOT NULL
  );
CREATE TABLE dbo.Movement (
  Id int NOT NULL IDENTITY PRIMARY KEY,
  EmployeeId int NOT NULL,
  CustomerId int NOT NULL,
  Data date NOT NULL,
  Time int NOT NULL,
  SpecialCost decimal(18,4) NULL
  );
CREATE TABLE dbo.EmployeeCost (
  EmployeeId int NOT NULL,
  Data date NOT NULL,
  Value decimal(18,4) NOT NULL
  );
GO
INSERT INTO dbo.Customer
  VALUES ( 1, 'Cli1' ), ( 2, 'Cli2' ), ( 3, 'Cli3' );
INSERT INTO dbo.Employe
  VALUES ( 1, 'Mirko' ), ( 2, 'Andrea' );
INSERT INTO dbo.EmployeeCost
  VALUES ( 1, '20110101', 1 ), ( 1, '20110701', 1.5 ), ( 1, '20120101', 2 );

INSERT INTO dbo.EmployeeCost
  VALUES ( 2, '20110101', 1 ), ( 2, '20120101', 1.3 );

INSERT INTO dbo.Movement
  VALUES ( 1, 1, '20110301', 2, NULL )  
    , ( 2, 1, '20110501', 1, NULL )  
    , ( 1, 1, '20110801', 1, NULL ) 
    , ( 2, 1, '20111001', 1, NULL ) 
    , ( 2, 1, '20120201', 1, 0.5 ); 


GO

AND NOW QUERY

USE tempdb;
GO
WITH cte AS (
  SELECT c.EmployeeId, c.Data, c.Value
    , ROW_NUMBER() OVER (PARTITION BY EmployeeId ORDER BY Data) AS [r]
    FROM dbo.EmployeeCost c
  ),
cteCost AS (
  SELECT ec.EmployeeId, ec.Data, ec.Value
    , ISNULL(DATEADD(DAY, -1, c.Data), '20991231') AS NextDate
    FROM cte ec
      LEFT JOIN cte c ON ec.EmployeeId = c.EmployeeId
        AND c.r = ec.r +1
  )
SELECT c.Id, c.Name
  , m.Data, d.Name, m.Time AS [Time], ec.Value AS [Cost per Hour], m.SpecialCost
  , m.Time * ISNULL(m.SpecialCost, ec.Value) AS [Employe Cost]
  FROM dbo.Customer c
    JOIN dbo.Movement m ON m.CustomerId = c.Id
    JOIN dbo.Employe d ON m.EmployeeId = d.Id
    JOIN cteCost ec 
      ON ec.EmployeeId = m.EmployeeId
        AND m.Data BETWEEN ec.Data AND ec.NextDate;
GO

Can anyone help me? Thanks

EDIT: RESULT QUERY

--<----------------
Id           Customer   Data       Employe   Time     Cost per hour  Total Cost

1           Cli1       2011-03-01   Mirko      2            1.0000              2.0000000000
1           Cli1       2011-05-01   Andrea     1            1.0000                  1.0000000000
1           Cli1       2011-08-01   Mirko      1            1.5000                  1.5000000000
1           Cli1       2011-10-01   Andrea     1            1.0000                 1.0000000000
1           Cli1       2012-01-15   Mirko      1            2.0000          
1           Cli1       2012-02-01   Andrea     1            1.3000        0.5000000000
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:13:45+00:00Added an answer on June 1, 2026 at 11:13 am

    I don’t see any parameter which needs to be passed to the CTE statement, so you can create a view and map it in the model.

    Then you can access the view like any other table.

    edit

    create view dbo.vwEmployeeCosts as 
    
    ;WITH cte AS (
        ...
    )
    select *
       ...
    from
    

    Then map this view in your model, like you would do it with a table

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this particular scenario, which is simple really, but I don't see (so
I have a particular PHP page that, for various reasons, needs to save ~200
I have a particular application that needs to calculate something very specific, and while
I have a particular web application that like most others, has to account for
I have a particular method that is occasionally crashing with an ArgumentException: Destination array
I have one particular FLA that is crashing every time I try to compile
Just curious about a particular scenario of NAT. Let's suppose we have 4 computers
I have written a program in .NET that listens to a particular Serial Port
I have a table that must reference another record, but of the same table.
[Apologies if my question title does not accurately describe my problem- if you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.