my data are look like this:
> Columns A B C
>
> Rows Total Audit Complete Audit Not Complete Audit Total
> 0 1 2 3
> 2 3 4 9
> 4 4 4 12
so, i need to change the rows title Audit Total with Audit Totals. the audit total rows is fix name. how to write the condition in mdx query. my mdx query is :
> With Member [Measures].[NCs of New
> Audit] as [Measures].[N Cs Of New
> Audit]
>
> Member [Measures].[PNCs Of New Audit]
> as [Measures].[PN Cs Of New Audit]
>
> Member [Measures].[OFIs Of New Audit]
> as [Measures].[OF Is Of New Audit]
>
> Member [Measures].[Average NCs Of New
> Audit] as [Measures].[Average N Cs Of
> New Audit]
>
> Member [Measures].[Average OFIs Of New
> Audit] as [Measures].[Average OF Is Of
> New Audit]
>
>
>
> SELECT { [Measures].[Audit Closed],
> [Measures].[Audit Open],
> [Measures].[CS Of New Audit],
> [Measures].[NCs of New Audit],
> [Measures].[PNCs Of New Audit],
> [Measures].[OFIs Of New Audit],
> [Measures].[Average NCs Of New Audit],
> [Measures].[Average OFIs Of New Audit]
> } ON COLUMNS,
>
> {[Results By Functional
> Discipline].[Discipline].[Discipline].AllMembers
> } ON ROWS
>
> FROM [QualityMattersView] where
> (<<SD>>:<<ED>>,<<Title>>)
>
> CELL PROPERTIES VALUE,
> FORMATTED_VALUE, CELL_ORDINAL,
> FONT_FLAGS, FORE_COLOR, BACK_COLOR
i want to write some iif condtion in rows, so if i get rows="Audit Total" then it replace by "Audit Totals".
One not very elegant solution is something like :
StrToSet(
Replace(
SetToStr(...[Discipline].AllMembers )
,'[Audit Total]'
,'[Audit Totals]' )
,CONSTRAINED)
You can also remove one element from a list {} - your member, and add the new one.
Related
I want to count the amount of records by year, for each year. These records contain a datecreated field. But the count should include the previous years as well. So counting the years of 2013 should, include those lower years as well, but not yet of 2014 and higher.
Explanation preferably in linq.
(sql is totally fine though)
I tried doing this by grouping by year, and then count for each year. Now only the previous years should be added, for each year.
I know this can be done with a lot where statements and selecting the results, but there should be a better way.
In SQL you need SUM OVER but it is not supported by Linq. You can download yearly data and calculate the cumulative sums in memory.
var fromYear = 2010;
var toYear = 2019;
var yearlyData = Receipts.Where(x => x.DateCreated.Year >= fromYear & x.DateCreated.Year <= toYear)
.GroupBy(x => x.DateCreated.Year)
.Select(x => new { Year = x.Key, Count = x.Count() })
.ToList();
var result = Enumerable.Range(fromYear, toYear - fromYear)
.Select(year => new
{
Year = year,
CumulativeCount = yearlyData.Where(y => y.Year <= year).Sum(y => y.Count)
});
Also you can use an outer variable:
var fromYear = 2010;
var toYear = 2019;
var yearlyData = Receipts.Where(x => x.DateCreated.Year >= fromYear & x.DateCreated.Year <= toYear)
.GroupBy(x => x.DateCreated.Year)
.Select(x => new { Year = x.Key, Count = x.Count() })
.OrderBy(x => x.Year)
.ToList()
;
var sum = 0;
var result = yearlyData.Select(x => new {x.Year, CumulativeSum = sum += x.Count});
select sum(case when year(datecreated) <= 2013 then 1 else 0 end) as until_2013,
sum(case when year(datecreated) <= 2014 then 1 else 0 end) as until_2014,
sum(case when year(datecreated) <= 2015 then 1 else 0 end) as until_2015
from your_table
Here's your query.
select count(1), year(created_date) from tableA
where year(created_date) < 2014
group by year(created_date)
I have three table below:
TABLE_PRODUCT (IdProduct, ProductName, ProductUnit)
TABLE_STORE_HOUSE (IdContain, IdProduct, ProductNumber, TimeInput)
TABLE_SELL (IdSell, IdContain, ProductNumberSell, TimeSell)
Current, How to using LinQ query get TABLE_STORE_HOUSE.IdProduct witch condition TABLE_STORE_HOUSE.ProductNumber - Sum(TABLE_SELL.ProductNumberSell) > 0 and TABLE_STORE_HOUSE.TimeInput is smallest
Help me convert Sql to Linq..............
select top 1 IdContain
from
TABLE_STORE_HOUSE
where IdProduct = '6'
and
ProductNumber - (select sum(ProductNumber)
from TABLE_SELL
Where TABLE_SELL.IdContain = IdContain)> 0
order by TimeInput desc;
Can you try this?
from t in TABLE_STORE_HOUSEs
let TSell = (
from s in TABLE_SELLs
where s.IdContain == t.IdContain
orderby s.ProductNumber
select new {
s.ProductNumber
}
)
where t.IdProduct == 6 && (t.ProductNumber - TSell.Sum(si => si.ProductNumber)) > 0
select new { t.IdContain }
for top 1 you can use Take() function.
I have a single column in a table to count specific rows. The sql query is as below:
SELECT
CASE
WHEN trail LIKE 'ClassA%' THEN 'ClassA'
WHEN trail LIKE 'ClassB%' THEN 'ClassB'
WHEN trail LIKE 'SemA%' THEN 'SemesterA'
WHEN trail LIKE 'SemB%' THEN 'SemesterB'
END AS Logs
, COUNT(*) AS Count
FROM Logs where s_date >= 'from date from UI' and e_date <= 'to date from ui'
GROUP BY
CASE
WHEN trail LIKE 'ClassA%' THEN 'ClassA'
WHEN trail LIKE 'ClassB%' THEN 'ClassB'
WHEN trail LIKE 'SemA%' THEN 'SemesterA'
WHEN trail LIKE 'SemB%' THEN 'SemesterB'
END
The above query result in sql fine as
ClassA 20
ClassB 5
SemesterA 2
SemesterB 50
Now, I need to change this sql to Linq with a date filter (from date, to date).
Please suggest change in query to simplyfy it.
Thanks
Tried:-
var data = _db.Logs.Where(p => p.trail.StartsWith("ClassA") && (p.SDate.Date >= CDate.Date && p.SDate.Date <= FDate.Date)).GroupBy(p => p.trail.StartsWith("ClassA")).Select(s =>
new
{
source = "Class - A total",
percentage = s.Count()
}).Union(_db.Logs.Where(p => p.trail.StartsWith("ClassB") && (p.SDate.Date >= CDate.Date && p.SDate.Date <= FDate.Date)).GroupBy(p => p.trail.StartsWith("ClassB")).Select(s =>
new
{
source = "Class - B total",
percentage = s.Count()
}).Union(_db.Logs.Where(p => p.trail.StartsWith("SemesterA") && (p.SDate.Date >= CDate.Date && p.SDate.Date <= FDate.Date)).GroupBy(p => p.trail.StartsWith("SemesterA")).Select(s =>
new
{
source = "Semester - A total",
percentage = s.Count()
}).Union(_db.Logs.Where(p => p.trail.StartsWith("SemesterB") && (p.SDate.Date >= CDate.Date && p.SDate.Date <= FDate.Date)).GroupBy(p => p.trail.StartsWith("SemesterB")).Select(s =>
new
{
source = "Semester - B total",
percentage = s.Count()
})))).ToList();
Try storing all the interesting starting keys in an enumerable of some sort and then using the built in group by method overload which outputs a result mapped from the key,group pairs (c.f. https://msdn.microsoft.com/en-us/library/vstudio/bb549393(v=vs.100).aspx)
string[] startingKeys = new string[] {"ClassA","ClassB","SemsterA","SemesterB"};
var data =_db.Logs.Where(p=>(p.SDate.Date >= CDate.Date && p.SDate.Date <= FDate.Date)&&startingKeys.Any(k=>p.Logs.StartsWith(k))).GroupBy(p=>startingKeys.Where(k=>p.Logs.StartsWith(k)).First(),(key,items)=>new {source=key,count = items.Count()})
One advantage of this method is you can change the starting keys at runtime if you feel like it.
I have the following queries:
var majorClients = maj in dbContext.MajorClients
where (maj.startdate > startDate)
where (maj.status == "Active")
Select new Client{EntityPK = maj.mjPrimaryKey,Name = maj.name, Type = "Maj"};
var minorClients = min in dbContext.MinorClients
where (min.startdate > startDate)
where (min.status == "Active" || min.status== "Inactive")
Select new Client{EntityPK = min.mnPrimaryKey,Name = min.name, Type = "Min"};
There are clients that could appear in both major and minor tables. I would like to return a list of all occurrences of clients in both tables, however if there are matching clients by name, then I would only want to return the matching record from the majorClients table.
I have written a sql query to return the results:
SELECT mjPrimaryKey AS EntityPK,name,'Maj' AS TYPE
FROM majorClients
WHERE status = 'Active' AND startDate > #startDate
UNION ALL
SELECT mnPrimaryKey,name,'Min' FROM minorClients
WHERE status IN ('Active','Inactive') AND startDate > #startDate
WHERE name NOT IN (SELECT name FROM majorClients WHERE status = 'Active' AND startDate > #startDate)
How would I represent this query in linq?
Try this linq. To exclude duplicates from minorClients, I've used Contains method. To union all objects - Union method:
var majorClients = from maj in dbContext.MajorClients
where maj.startdate > startDate
&& maj.status == "Active"
select new Client
{
EntityPK = maj.mjPrimaryKey,
Name = maj.name,
Type = "Maj"
};
var minorClients = from min in dbContext.MinorClients
where min.startdate > startDate
&& min.status == "Active" || min.status== "Inactive"
&& !(from maj in dbContext.MajorClients
where maj.startdate > startDate
&& maj.status == "Active"
select maj.name).Contains(min.Name)
select new Client
{
EntityPK = min.mnPrimaryKey,
Name = min.name,
Type = "Min"
};
var allClients = majorClients.Union(minorClients);
I'm trying to write a SQL Statement that should function like the below Linq query. Mainly, I want to sum two columns (Cash and Check) out of one column (Value * Quantity) based on a certain condition (Type == "Check"). So, if the Type is "Check", then put the value in the Check column, otherwise put it in the Cash column.
Linq:
from ac in m_DataContext.acquisitions
join ai in m_DataContext.acquireditems on ac.Identifier equals ai.AcquisitionIdentifier
group ai by ac.SessionIdentifier into g
select new
{
SessionIdentifier = g.Key,
Cash = g.Where(ai => ai.Type != "Check").Sum(ai => (double?)ai.Value * (int?)ai.Quantity) ?? 0,
Check = g.Where(ai => ai.Type == "Check").Sum(ai => (double?)ai.Value * (int?)ai.Quantity) ?? 0,
CheckQuantity = g.Where(ai => ai.Type == "Check").Sum(ai => (int?)ai.Quantity) ?? 0
};
SQL:
SELECT a.SessionIdentifier, a.Checks as CheckQuantity, ???? as Cash, ???? as Check
FROM Acquisitions ac
INNER JOIN AcquiredItems ai ON ai.AcquisitionIdentifier = ac.Identifier
GROUP BY a.SessionIdentifier
Is this possible?
You can do it like this:
select sum(case when type = 'cash' then Value * Quantity else 0 end) as Cash,
sum(case when type = 'check' then Value * Quantity else 0 end) as Check
from ...