I have a basic SQL Table ( pKey INT, TransDate smallDateTime, Amount Float)
I simply want to emulate this SQL in LINQ
SELECT SUM(Amount) AS result
FROM dbo.Basic
WHERE TransDate >= #startDate
AND TransDate <= #EndDate
I have created the LINQ dbml for this and I can get basic query results for a date range
However I can't find the right syntax to get the SUM over a dateRange, I've tried several variations on the following but either they dont compile, or the result they give cannot be converted to a double
BasicDataContext dContext = new BasicDataContext();
var lnq = from c in dContext.Basic
where c.TransDate >= startdate &&
c.TransDate <= enddate
select new { total = c.Sum(Amount) };
double result = (double)lnq.total;
This should work:
double result = (from c in dContext.Basic
where c.TransDate >= startdate &&
c.TransDate <= enddate
select c.Amount).Sum();
Related
I'm trying to figure out how to write a linq query that will return equivalent results to the sql query below. The problem I'm having has to do with the two select count queries included in the select list of the main query. I need to get counts of two different types of records from the PaymentHistory table for the last year. Can the equivalent of this be written using linq? Preferrably using lambda syntax.
select ieinum, serviceaddrkey,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 100) as ReturnedFees,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 101) as CorrectedReturnedFees
from Serviceaddr
Any help will be great.
Perhaps something like this:
from s in Serviceaddr
let ph = PaymentHistory.Where(p => s.serviceaddrkey == p.serviceaddrkey &&
p.PostDate >= DateTime.Now.AddYears(-1))
select new
{
s.ieinum,
s.serviceaddrkey,
ReturnedFees = ph.Count(p => p.SetID == 100),
CorrectedReturnedFees = ph.Count(p => p.SetID == 101)
}
SELECT
domain_usage.url,
LEFT(url,locate('?',url)) AS cleanURL
FROM
domain_usage
WHERE
MONTH(domain_usage.login_date) = (Now()) AND
YEAR(domain_usage.login_date) = (Now());
Returns no records, and no error ?
im trying to clean the query string from URL field.....
Any thoughts ?
Your date comparisons are wrong. you're basically trying
3 = '2013-01-18 10:36:00'
you need to compare apples to apples:
MONTH(domain_usage.login_date) = MONTH(now())
modify your WHERE condtion,
SELECT domain_usage.url, LEFT(url,locate('?',url)) AS cleanURL
FROM domain_usage
WHERE MONTH(domain_usage.login_date) = MONTH(CURDATE()) AND
YEAR(domain_usage.login_date) = YEAR(CURDATE())
I am using MySQL to make a report showing the number of hours billed for a particular date range and project. The complexity is that the date range is variable for each project (different start month and start day). This information is coming from a value in another database/table.
I have the following UDF in MySQL:
DELIMITER //
CREATE FUNCTION TimeLeft(startday INT, today INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s INT;
IF startday < today THEN SET s = 0;
ELSE SET s = 1;
END IF;
RETURN s;
END //
DELIMITER;
I use that function in the following query, which is supposed to take the value returned in the TimeLeft function to determine the values for the start month (month(curdate())-#xx) and start day (#yy) for each project to calculate the hours:
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
Here's how I am setting the values for #xx and #yy:
SET #xx = 0; #this is the value that we will use to manipulate the month for the date range
SET #yy = 0;
#yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT #xx:= TimeLeft(#yy,dayofmonth(curdate()));
I am getting some issues:
#yy is not getting the value - possibly my syntax is wrong?
The variables are set at the top of the code, so they are not getting changed for each project as they should be (there should be a different #xx and #yy for each project since each one has a different start and end date).
Here's the full query:
#below is where I assign the variables
SET #xx = 0; #this is the value that we will use to manipulate the month for the date range
SET #yy = 0;
#yy:= SELECT start_day_of_month FROM dashboard.client; #this doesn't seem to work
SELECT #xx:= TimeLeft(#yy,dayofmonth(curdate()));
# below is the MySQL query that is meant to use the variables assigned above
SELECT X.expr1 AS 'Project Name', #monthly_hours - SUM(X.expr2) AS 'Hours Billed
FROM
(SELECT
projects.name AS expr1
, sum(time_records.value) AS expr2
FROM project_objects
INNER JOIN projects
ON projects.id = project_objects.project_id
INNER JOIN time_records
ON time_records.parent_id = project_objects.id
WHERE time_records.parent_type = 'Task'
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name
UNION
SELECT
projects.name AS expr1
, sum(time_records.value) as expr2
FROM projects
INNER JOIN time_records
ON projects.id = time_records.parent_id
WHERE time_records.parent_type = 'Project'
AND time_records.record_date >= concat('2012/', month(curdate())-#xx , '/' , #yy)
AND time_records.record_date <= curdate()
GROUP BY projects.name) X
GROUP BY X.expr1
I think there is some issue of where I am assigning the variables #xx and #yy. These should be done for each individual Project, so putting them up on the top is probably not the best idea. I'm also not sure if I am assigning the #yy value correctly. It's supposed to query the value of the field of a table that is in another database but it keeps throwing a syntax error on the #yy assignment to that field.
Assign value to #yy inside select:
SELECT #yy:= start_day_of_month FROM dashboard.client;
I am stucking in sql statement .
Please my query below
select
master_brand.BRAND_NAME as AcCompanyID,
lewre_product.SKU_CODE as AcStockID,
lewre_product.SKU_CODE as BarCode,
lewre_product.SKU_CODE as StockBarCode,
lewre_product.SKU_CODE as StockDescription1,
NULL as StockDescription2,
lewre_project.PROJECT_NAME as AcStockBrandID,
lewre_artice.ARTICLE_NUMBER as AcStockGroupID,
icenter_acStockCategory.CATEGORY_CODE as AcStockCategoryID,
icenter_acStockColor.AcStockColorID as AcStockColorID,
lewre_product.SIZE as AcStockSizeID,
'NA' as ISBN,
master_product_type.UOM as AcStockUOMID,
lewre_article_price.FINAL_COST as StockCost,
lewre_article_price.PRICE as StockPrice1,
0 as StockPrice2,
0 as StockPrice3,
0 as StockPrice4,
0 as StockPrice5,
lewre_artice.CREATE_DATE
from [LEWRE.ARTICLE]as lewre_artice,
[iCENTER.ACSTOCKCATEGORY] as icenter_acStockCategory,
[iCENTER.ACSTOCKCOLOR] as icenter_acStockColor,
[LEWRE.PRODUCT] as lewre_product,
[MASTER.BRAND] as master_brand,
[LEWRE.PROJECT] as lewre_project,
[MASTER.PRODUCT_TYPE] as master_product_type,
[LEWRE.ARTICLE_PRICE] as lewre_article_price
where
(icenter_acStockCategory.CATEGORY_ID = lewre_artice.PRODUCT_CATEGORY_ID
AND icenter_acStockCategory.PRODUCT_TYPE_ID = lewre_artice.PRODUCT_TYPE_ID)
AND lewre_product.ARTICLE_ID = lewre_artice.ARTICLE_ID
and lewre_product.COLOR_ID = icenter_acStockColor.COLOR_ID
and master_brand.BRAND_ID = lewre_artice.BRAND_ID
and lewre_artice.PROJECT_ID = lewre_project.PROJECT_ID
and master_product_type.PRODUCT_TYPE_ID = lewre_artice.PRODUCT_TYPE_ID
and lewre_artice.ARTICLE_ID = lewre_article_price.ARTICLE_ID
and lewre_artice.CREATE_DATE
order by lewre_artice.CREATE_DATE
Here is the result that i get
I want to select all row which create_date is current date.for example (2012-04-30)' . How can i get the result? Please help me
Add this to your where clause.
lewre_artice.CREATE_DATE >= cast(getdate() as date) and
lewre_artice.CREATE_DATE < cast(dateadd(day, 1, getdate()) as date)
getdate() returns the current timestamp and cast(getdate() as date) removes the time part. dateadd(day, 1, ....) adds one day. The comparison returns the rows where CREATE_DATE is within the interval.
I need to find all rows in a MySQL database that fall between "YYYY-10-30" and "YYYY-12-11" I don't care about which year the the date is in just that falls between the two dates.
SELECT *
FROM yourTable
WHERE (MONTH(yourDate) = 10 AND DAYOFMONTH(yourDate) >= 30)
OR (MONTH(yourDate) = 11)
OR (MONTH(yourDate) = 12 AND DAYOFMONTH(yourDate) <= 11)
select *
from table
where
concat(year(now()),right(date_field,6))
between
concat(year(now()),'-10-30')
and
concat(year(now()),'-12-11')
Could you try this?:
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetByMonthAndDayRange`
(
IN monthFrom INT
, IN dayFrom INT
, IN monthTo INT
, IN dayTo INT
)
BEGIN
-- For good coding practice, name your columns instead of using *
SELECT *
FROM tablename
WHERE
(MONTH(datecolumnname) = monthFrom AND DAYOFMONTH(datecolumnname) >= dayFrom)
OR (MONTH(datecolumnname) > monthFrom AND MONTH(datecolumnname) < monthTo)
OR (MONTH(datecolumnname) = monthTo AND DAYOFMONTH(datecolumnname) <= dayTo);
END
If you use a stored procedure (and if the code works to your taste) you'll be able to code faster, just providing the parameters (it's dynamic this way)