Getting the sum of quantity by product id - c#

I'm struggling to get some results from my database. I have a table like this:
ID ID_INVOICE PRODUCT QUANTITY PRODUCT_ID
------------------------------------------
1 1 aaa 3 2
2 1 bbb 2 3
3 1 ccc 1 4
4 2 bbb 3 3
5 2 aaa 3 2
So after the query I would like to get something like
aaa 6
bbb 5
ccc 1
The query is based on the ID_INVOICE so far I've tried this:
SELECT product, sum(quantity)
FROM product
WHERE invoice_id = #p1

Add a GROUP BY product clause, like so:
SELECT product, sum(quantity)
FROM product
WHERE invoice_id = #p1
GROUP BY product;
SQL Fiddle Demo

Related

EF select many doesn't retrieve record which has no relation

I have two tables, for example, Student and Grade.
Those two tables have a relationship with many-to-many, so the table is StudentGrade.
By using the .SelectMany query, I can retrieve all records which have a relation.
For example,
var myResult = myDb.Student.SelectMany(x => x.Grade).ToList();
But let say I add a new record just to the Student table, which has no relation with Grade, this new record cannot be retrieved by using the query above.
How can I retrieve all data including this new one?
Student
Id Name Age
1 AAA 4
2 BBB 5
3 CCC 6
4 DDD 7
Grade
Id Name
1 G1
2 G2
3 G3
4 G4
StudentGrade
Student Grade
2 1
2 2
2 3
3 3
Require result:
Id Name Age Grade
1 AAA 4
2 BBB 5 G1
2 BBB 5 G2
2 BBB 5 G3
3 CCC 6 G3
4 DDD 7
I found full outer join would help but will it work in a many-to-many relationship?
How can I overcome this issue ?
var result = (from s in myDb.Student
from g in myDb.Grades
where !g.Select(x => x.Student).Contains(s) || g.Student == null || g.Student == s).ToList();
As long as I understand your question this should give you
all Students that have grades,
all Grades with no students,
and all Students with no grades.
I have not seen your models but this should help
var myResult = myDb.Student.SelectMany(x => x.Grade).ToList();
This query "means" retrieve all the Grades that have a Student. If you want the Students, select Student and Include the Grades if you want.
var myResult = myDb.Students.Include(x => x.Grades).ToList();

Problems in Right join in SQl

I have the following table structure also i have mention my expected output
please help me with query as i dont know much about sql query
Table Structure
Table 1 : Emp Details
FName Id
Pratik 1
Praveen 3
Nilesh 2
Table 1 : JoinigDocument
id DocumentName
1 Leaving
2 Exp letter
3 birth cert
Table 2 : EmployeeJoiningDocument
EmpId JoiningDocumentId
1 1
1 2
3 1
3 2
3 3
2 1
2 3
Expected Output :
FName Id JoiningDocumentId DocumentName
Pratik 1 1 Leaving
Pratik 1 2 Exp letter
Pratik 1 null birth cert
Praveen 3 1 Leaving
Praveen 3 2 Exp letter
Praveen 3 3 birth cert
Nilesh 2 1 Leaving
Nilesh 2 null Exp letter
Nilesh 2 3 birth cert
You can write a query as:
select
A.FName,
A.Id,
B.JoiningDocumentId,
c.DocumentName
from #JoinigDocument C
cross join #EmployeeDetail A
Left join #EmployeeJoiningDocument B on B.EmployeeId = A.id and
B.JoiningDocumentId = C.id
order by A.Id
First cross join JoinigDocument and EmployeeDetail table so that you get all possible combinations of Employee and Documents irrespective of the fact that employee has that Joining Document or not. Then you need to do a left join to retain all these matches and find data corresponding to valid entries in EmployeeJoiningDocument.
Demo

LINQ: Groupby and Last

I am new to LINQ.
I have the following table:
ID Field1 Field2 Field3
1 aaaa 20/01/2014 10
2 aaaa 21/01/2014 3
3 aaaa 25/01/2014 10
4 bbbb 01/01/2014 90
5 bbbb 03/01/2014 1
6 bbbb 31/01/2014 5
I want to group by Field1 and grab the last line of each group.
The SQL Query equivalent to this is:
SELECT Field1, Last(Field2) AS LastOfField2, Last(Field3) AS LastOfField3
FROM Table1
GROUP BY Field1
How this can be achieved on Linq?
var result = from p in Table1
group p by p.Field1 into grp
select grp.OrderByDescending(g=>g.Field2).First();

How to do recursive query using a temporary table in Mysql

OrganisationID OrganisationName parentID
1 Org1 Null
2 Org2 1
3 Org3 1
4 Org4 2
5 Org5 2
6 Org5 4
Table Name is tbl_Organisation
I am having a table similar to this. All I am trying is to retreive the Sub Organisation and display it. Suppose the Org ID passed is 3, then the Org3 doesnt have any child so it displays only Org3. Suppose if OrgID =2 then the Org2 has a child Org4 and Org4 has a child Org5. So for OrgID=2 I have to display Org2, Org4 and Or5. SO how can I do that. I have tried few things but it didn't work as I intended.
SELECT distinct b.OrganisationID,b.OrganisationName
FROM tbl_organisation as a LEFT OUTER JOIN tbl_organisation as b
on a.OrganisationID=b.ParentID where a.OrganisationID=b.parentID
Tell me where I am wrong
I am using this in asp.net website, I am using c# and mysql
This is related to Hierarchail Query:
SELECT (LPAD(' ', level * 3, ' ')||OrganisationID) as Org_id,
OrganisationName,
parentID,
LEVEL
FROM tbl_Organisation
START WITH OrganisationID = ---
Comment: Pass the Organisation ID here
CONNECT BY PRIOR OrganisationID = parentID
If you pass 1 as the OrganizationID then the Output will be
Org_id OrganisationName parentID LEVEL
1 Org1 1
2 Org2 1 2
4 Org4 2 3
6 Org6 4 4
5 Org4 2 3
3 Org2 1 2

How to add a Restriction to an inner join?

I have the following NHibernate DetatchedCriteria,
return DetachedCriteria.For<MMFund>()
.CreateCriteria<MMFund>(x => x.DataUniverse)
.Add<DataUniverse>(x => x.SiteId == 100)
.SetProjection(LambdaProjection.Property<MMFund>(x => x.FundId));
which is producing the following SQL:
and
this_.ShareClassReturn_ShareClassId in
(
SELECT f.[Fund_ID] as y0_
FROM
dbo.Fund f inner join CAP.DataUniverse du
on f.[Fund_TypeID] = du.[DataUniverse_TypeId]
and f.[Fund_CountryID] = du.[DataUniverse_CountryID]
WHERE fu.[DataUniverse_SiteId] = 100
)
There are many funds in a DataUniverse.
I need to filter this so that I can select only the funds with a country ID of 'ET', so that my query looks as follows:
and
scr.ShareClassReturn_ShareClassId in
(
/* Get funds in universe */
SELECT f.[Fund_ID] as y0_
FROM dbo.Fund f inner join CAP.DataUniverse du
on f.[Fund_TypeID] = du.[DataUniverse_TypeId]
and f.[Fund_CountryID] = 'ET' // these are the guys I need
WHERE du.[DataUniverse_SiteId] = 100
)
However, I'm not sure what I need to do to the DetachedCriteria in order to make this happen. The problem I'm having is that no matter what I do, it's putting the clause in the wrong place, such as
WHERE du.[DataUniverse_SiteId] = 100 and f.Fund_CountryId = 'ET'
when I add the line .Add(Restrictions.Eq("CountryId", "ET")) as follows
return DetachedCriteria.For<MMFund>()
.Add(Restrictions.Eq("CountryId", "ET"))
.CreateCriteria<MMFund>(x => x.DataUniverse)
.Add<DataUniverse>(x => x.SiteId == 100)
.SetProjection(LambdaProjection.Property<MMFund>(x => x.FundId));
or it attempts to filter on the wrong table entirely when I specify that the Restriction should be part of the second .CreateCriteria, such as
return DetachedCriteria.For<MMFund>()
.CreateCriteria<MMFund>(x => x.DataUniverse)
.Add(Restrictions.Eq("CountryId", "ET"))
.Add<DataUniverse>(x => x.SiteId == 100)
.SetProjection(LambdaProjection.Property<MMFund>(x => x.FundId));
which produces this;
WHERE du.[DataUniverse_SiteId] = 100 and du.[DataUniverse_CountryID] = 'ET'
** note - as I'm using the Criteria API, this is actually the Restriction that I'm using:
.Add<MMFund>(f => f.CountryId == "ET")
I used the Restriction terminology because it's more explicit to what I'm trying to achieve. The Criteria API & the other way both produce the exact same results.
Why do you think Where is the wrong place for the filter? That's where filtering happens.
The generated SQL looks sound. You have two tables joined on their common fields. The Where clause is providing the appropriate filtering information. If your preferred SQL statement was in place, you'd have data joined on TypeID alone, not the CountryID.
For example, let's say your Fund table looks like this
TypeID CountryID
1 1
1 2
2 1
2 2
3 1
4 1
And your DataUniverse table is the following
TypeID CountryID
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
4 1
4 2
If you wrote SQL like you desire, you would produce a join based on TypeID and you would filter Fund.CountryID = 1, for example. What would your product look like?
F.TypeID F.CountryID D.TypeID D.CountryID
1 1 1 1
1 1 1 2
1 1 1 3
2 1 2 1
2 1 2 2
2 1 2 3
3 1 3 1
3 1 3 2
4 1 4 1
4 1 4 2
Is that your desired output? Yes, you've filtered Fund.CountryID, but your join was just on TypeID, so you've got all records from DataUniverse with that matching type for each Fund.
With the join on the two fields and the Where filtering the CountryID, the result will be the following
F.TypeID F.CountryID D.TypeID D.CountryID
1 1 1 1
2 1 2 1
3 1 3 1
4 1 4 1
The question is which set of data is the one you expect?

Categories

Resources