Have not come across an exact use-case for what I'm trying to achieve on Stackoverflow so will explain it in the hopes someone can assist.
I have two tables, one contains a master activity list and the other contains who completed those activities.
Table A is the Activity Table. This is a distinct list of things that can be done.
ID | Activity
---------------------------
1 | Change Oil
2 | Change Airfilter
3 | Change Brake Fluid
Table B is the Activity Log table. This tracks where people have done one of the above Activities. ActivityID links to ID on Table A.
ID | ActivityID | CompletedBy
---------------------------------------
1 | 1 | john#auto.com
2 | 1 | sally#auto.com
3 | 3 | john#auto.com
What I am trying to produce is a list of all activities, but then only for a distinct person. I have tried multiple ways for this, but can only get the query to show where values exist in both tables.
My preferred output would be the following, where in the query i have asked to show me the full Activity list and also to show where John has completed anything. If there is no record in Table B for this activity, to show a blank value.
ID | Activity | CompletedBy
-------------------------------------------
1 | Change Oil | john#auto.com
2 | Change Airfilter |
3 | Change Brake Fluid | john#auto.com
Here is my current SQL Query in my attempt to work this one out, which right now the results only return ID 1 and 3 from that example above, where john actually has a record in Table B.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Any help or pointers in the right direction would be greatly appreciated.
If you move the CompletedBy restriction into the join conditions you would not need the group by.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID and b.CompletedBy = 'john#auto.com'
Change your where clause to Where CompletedBy = 'john#auto.com' or CompletedBy is null so it matches on a text match or the case where the name does not exist at all. You can also use isnull( to convert the null value to a empty string if that is the behavior you are wanting.
Select a.ID, a.Activity, isnull(b.CompletedBy, '') as CompletedBy
FROM ActivityList a
LEFT OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com' or b.CompletedBy is null
GROUP BY a.ID, a.Activity, b.CompletedBy
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable table_A = new DataTable();
table_A.Columns.Add("ID", typeof(int));
table_A.Columns.Add("Activity", typeof(string));
table_A.Rows.Add(new object[] { 1, "Change Oil"});
table_A.Rows.Add(new object[] { 2, "Change Airfilter"});
table_A.Rows.Add(new object[] { 3, "Change Brake Fluid"});
DataTable table_B = new DataTable();
table_B.Columns.Add("ID", typeof(int));
table_B.Columns.Add("ActivityID", typeof(string));
table_B.Columns.Add("CompletedBy", typeof(string));
table_B.Rows.Add(new object[] { 1, 1, "john#auto.com"});
table_B.Rows.Add(new object[] { 2, 1, "sally#auto.com"});
table_B.Rows.Add(new object[] { 3, 3, "john#auto.com"});
var groups = (from a in table_A.AsEnumerable()
join b in table_B.AsEnumerable() on a.Field<int>("ID") equals b.Field<int>("ID")
select new { a = a, b = b})
.GroupBy(x => x.b.Field<string>("CompletedBy"))
.ToList();
}
}
}
----------
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
Right OUTER JOIN ActivityLog b
ON a.ID = b.ActivityID
Where CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Your query is correct just change the potion of condition.
Select a.ID, a.Activity, b.CompletedBy
FROM ActivityList a
LEFT JOIN ActivityLog b
ON a.ID = b.ActivityID AND b.CompletedBy = 'john#auto.com'
GROUP BY a.ID, a.Activity, b.CompletedBy
Related
I want to get the Catalog of a selected supplier along with the last purchase price which is the column Costs in the CommandDetails table as shown bellow :
Product (idproduct, productName)
Command (idCommand, CommandCode, CommandDate, idSupplier)
CommandDetails(idCommandDetails, idProduct, Qty, idCommand, Costs)
Supplier (idSupplier, SupplierName, SupplierAddress)
SupplierCatalog (idSupplier, idProduct)
I tried the row_number() Over (Partition by ...) and other methods but i'm missing something, my brain said enough.
Desired Result :
--------------------------------------------------
| SupplierName| ProductName | CommandDate | Costs|
--------------------------------------------------
| SUP1 | P1 | 01/01/2018 | 3,06 |
| SUP1 | P6 | 01/01/2018 | 1,65 |
| SUP1 | P8 | 03/01/2018 | 5,20 |
| SUP1 | P9 | 05/01/2018 | 8,00 |
| SUP1 | P10 | 01/01/2018 | NULL |
--------------------------------------------------
Null in Costs for P10 when the product has never been ordered.
My last attempt :
SELECT
*
FROM
(SELECT
Sct.idsupplier,
SCt.idProduct,
SCD.PurchasePriceCmd Costs,
SCD.Qty,
P.ProCode,
P.ProName,
Row_number() OVER(PARTITION BY Sct.idProduct order by P.ProCode) rn
FROM SupplierCatalog SCt
LEFT JOIN CommandDetails SCD
ON SCD.idProduct = SCat.idProduct
LEFT JOIN Command a
ON a.idCommand = SCD.idCommand
LEFT OUTER JOIN StoreCommand b
ON a.idCommand = b.idCommand
INNER JOIN Product P
ON P.idProduct = SCt.idProduct) t
where Sct.idSupplier = 4 and rn = 1
You could also try this:
SELECT
s.supplierName,
p.productName,
latestCommandDetail.CommandDate,
latestCommandDetail.Costs
FROM Supplier s
INNER JOIN SupplierCatalog sc ON sc.idSupplier = s.idSupplier
INNER JOIN Product p ON p.idProduct = sc.idProduct
OUTER APPLY
(
SELECT TOP 1
c.CommandDate,
cd.Costs
FROM Command c
INNER JOIN CommandDetails cd ON cd.idCommand = c.idCommand
WHERE c.idSupplier = s.idSupplier AND cd.idProduct = p.idProduct
ORDER BY c.CommandDate DESC
) latestCommandDetail
WHERE s.idSupplier = 4
ORDER BY
s.supplierName,
p.productName
I don't have SQL Server going on this machine at the moment so you may need to tweak the syntax. The trick is just doing a link to a subquery that returns the top row sorted by the date descending.
I'm assuming the idSupplier for "SUP1" is 4 as per you example code above.
OUTER APPLY (the other optional join) will return nulls if no record is returned from the sub query. If you are only interested in products with prices use CROSS APPLY instead.
Also note that this query does not determine what to do if:
- there are two Commands for the same last date from the same supplier for the same product but with different Costs
- the same product is sold twice under the same Command but at different Costs
In both cases this can probably be handled by extending the sort order of the sub-query or by grouping/aggregating the sub-query.
Something like this ought to work:
;with cte as (
select a.idCommand, b.idProduct, row_number() over (partition by b.idProduct
order by a.CommandDate desc) as rn, a.CommandDate, b.Costs
from Command a
inner join CommandDetails b on a.idCommand = b.idCommand
)
select
c.SupplierName, e.productName, cte.CommandDate, cte.Costs
from Supplier c
left join SupplierCatalog d on c.idSupplier = d.idSupplier
left join Product e on d.idProduct = e.idproduct
left join cte on e.idProduct = cte.idproduct and cte.rn = 1
where c.idSupplier = #SupplierNumber;
You can probably replace the common table expression at the top with a subquery, or take some fields out of the CTE and join them in later.
I'm trying to execute this Linq Expression, but the LEFT JOIN is duplicating the rows.
When I write this exactly query in SQL it works ok, but when I write it in Linq Expression the LEFT JOIN duplicates the row.
I've tried to group by.. but I've got the same result.
var sql = (from project in db.Project
join suitT in db.SuitT
on project.Id equals suitT.IdProject
join inspec in db.Inspec
on suitT.Id equals inspec.IdSuitT
join listFinalDef in db.ListFinalDef
on inspec.Id equals listFinalDef.IdInspec
into myListFinalDef
from groupListFinalDef in myListFinalDef.DefaultIfEmpty()
join artefact1 in db.Artefact
on groupListFinalDef.Id equals artefact1.IdListFinalDef
into myArtefact1
from groupArtefact1 in myArtefact1.DefaultIfEmpty()
join artefact2 in db.Artefact
on inspec.Id equals artefact2.IdInspec
into myArtefact2
from groupArtefact2 in myArtefact2.DefaultIfEmpty()
join typeArtefact in db.TypeArtefact
on inspec.IdTypeArtefact equals typeArtefact.Id
where ...
select new ArtefactModels
{
IdArtefact1 = groupArtefact1.Id,
IdArtefact2 = groupArtefact2.Id,
...,
...,
...
}).ToList();
Whats happening: Duplicating the row
Example
Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2
What I'm trying to do:
Example
Id: 1 | ProjectName: Project 1 | InspecTitle: Title 1
Id: 2 | ProjectName: Project 2 | InspecTitle: Title 2
EDITED:
I'm trying to execute this SQL but with LINQ Expression:
SELECT art1.Id AS IdArtefact1, art2.Id AS IdArtefact2, ...
FROM
Project
INNER JOIN SuitT ON Project.Id = SuitT.IdProject
INNER JOIN Inspec ON SuitT.Id = Inspec.IdSuitT
LEFT JOIN ListFinalDef ON Inspec.Id = ListFinalDef.IdInspec
LEFT JOIN Artefact AS art1 ON ListFinalDef.Id = art1.IdListFinalDef
LEFT JOIN Artefact AS art2 ON Inspec.Id = art2.IdInspec
INNER JOIN TypeArtefact ON Inspec.IdTypeArtefact = TypeArtefact.Id
WHERE ...
The groupby goes like this:
var sql = (from project in db.Project
join suitT in db.SuitT
on project.Id equals suitT.IdProject
join inspec in db.Inspec
on suitT.Id equals inspec.IdSuitT
join listFinalDef in db.ListFinalDef
on inspec.Id equals listFinalDef.IdInspec
into myListFinalDef
from groupListFinalDef in myListFinalDef.DefaultIfEmpty()
join artefact1 in db.Artefact
on groupListFinalDef.Id equals artefact1.IdListFinalDef
into myArtefact1
from groupArtefact1 in myArtefact1.DefaultIfEmpty()
join artefact2 in db.Artefato
on inspec.Id equals artefact2.IdInspec
into myArtefact2
from groupArtefact2 in myArtefact2.DefaultIfEmpty()
join typeArtefact in db.TypeArtefact
on inspec.IdTypeArtefact equals typeArtefact.Id
where ...
select new ArtefactModels
{
IdArtefact1 = groupArtefact1.Id,
IdArtefact2 = groupArtefact2.Id,
...,
...,
...
})
//This will return IGroupable<key,value> with as key IdArtefact and as
//value an Enumerable of all the anonymous types with the same IdArtefact
.GroupBy(a => a.IdArtefact1)
//Select first value per IdArtefact if there's multiple
.Select(a => a.FirstOrDefault())
.ToList();
You can use my library PowerfulExtensions. It has a Distinct by properties method documented here.
Just call it at the end of the LINQ chain and pass the properties that make a row distinctive.
I have the following data:
Id | Value | OtherStuff
---------------------------
6 | 6 | 1
---------------------------
5 | 4 | 2
---------------------------
5 | 2 | 3
The desired result:
Id | Value | OtherStuff
---------------------------
6 | 6 | 1
---------------------------
5 | 4 | 2
That is I need the Max Value for each of the Id's.
I'm a bit stumped of how to do this without breaking it into multiple queries, can it be done, and if so how?
Update: I think I oversimplified the issue:
var query = from st in StockStakes
join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
from o in oGroup.DefaultIfEmpty()
where st.Stock.Status == "A"
select new
{
Id = st.Id,
Value = st.Value,
CustomerId = o.OrganisationId
};
The data sample from above still stands... now how do i structure the query to give me the Max Value alongside each Id?
var query = from x in data
group x by x.Id into x
select x.OrderByDescending(y => y.Value).FirstOrDefault()
Based on you updated query, similar approach to the first query, but since you have multiple tables you need to group all the tables into an anonymous object and then select only the columns you want
var query = from st in StockStakes
join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
from o in oGroup.DefaultIfEmpty()
where st.Stock.Status == "A"
group new { st, o } by st.Id into g
let largestValue = g.OrderByDescending(x => x.Value).FirstOrDefault()
select new
{
Id = g.Key,
Value = largestValue.st.Value,
CustomerId = largestValue.o.OrganisationId
};
I'm not really sure about what you mean, but maybe you can try with this query.
select Id, max(Value)
from your_table
group by Id;
This gives you the max "Value column" value for each "Id column" value.
-- EDIT --
LINQ version:
var q = from t in dc.YourTable
group t by t.Id
into g
select new
{
Id = g.Id,
Value = (from t2 in g select t2.Value).Max()
};
Code not tested. I'm on the bus now... :-) Give it a try!
I am trying to select everything from a table and then make a join with two other tables, from which I need just some of the columns. The final list will be populated into a DevExpress GridControl. The main table that I take the data from has these columns:
Id | ScheduleId | AccountId | Description
I can easily select everything from this table and return. Here is my list:
public IList<StatusDescription> getStatusDescription()
{
var listOfItems = from e in context.StatusDescription select e;
return listOfItems.ToList<StatusDescription>();
}
The other two tables are Schedules and Accounts, they look as follows:
Id | ScheduleName | DateFrom | DateTo
and
Id | AccountName | AccountCode
I would like to join the DateFrom and DateTo from the Schedule table and the AccountCode from the Account table. Is it possible to get all that into a single list. I can easily bind it later to the GridControl
var listOfItems = from e in context.StatusDescription
join s in context.Schedules on e.ScheduleId equals s.Id
join a in context.Accounts on e.AccountId equals a.Id
select new
{
Description = e.Description,
ScheduleStart = s.DateFrom,
ScheduleEnd = s.DateTo,
AccountCode = a.AccountCode
}
var Query = from p in context.StatusDescription
select new
{
p.Id,
p.Description,
p.Schedule.DateFrom,
p.Schedule.DateTo,
p.Account.AccountCode
};
hope it helps
I have this table (Prefrences_Table)
--------------------------
|student | Preferences |
--------------------------
Stud A | Stud B
Stud A | Stud C
Stud B | Stud E
Stud B | Stud A
Stud C | Stud F
Stud F | Stud B
--------------------------
If "Stud A" has added "Stud B" in his Preferences list, i would like to check if "stud B" has also added "stud A" in his preference, so i can add both of them in one group.
How can this be done using SQL or C#?
A self-join should work just fine here. The additional predicate returns only the first instance of the match to avoid duplicates.
select t.student, t1.student
from
Prefrences_Table t
inner join Prefrences_Table t1
on t.student = t1.preferences
and t.preferences = t1.student
and t.student < t1.student
this might give you anwer to your question, field mutual will be one if both students added the other in preferences, zero otherwise
SELECT T1.student, T2.Preferences,
(SELECT COUNT(*) FROM Prefrences_Table T2 WHERE T2.Preferences = T1.student AND T2.student = T1.Preferences) AS mutual
FROM Prefrences_Table T1
Another alternative would be the following:
SELECT * FROM
(
SELECT PM.student, PM.Preferences,
(SELECT COUNT(student) FROM Prefrences_Table AS PI WHERE PI.Preferences = PM.student
AND PI.student = PM.Preferences) AS CheckCross
FROM Prefrences_Table AS PM
) AS PD
WHERE PD.CheckCross > 0
You have some SQL answers, here is one in c#/linq.
var list = new List<Prefrences_Table>();
var results = (from t in list
join t1 in list on t.student equals t1.preferences
where
t.student == t1.preferences &&
t.preferences == t1.student &&
string.CompareOrdinal(t.student, t1.student) < 0
select new {t.student, t1.student}
);