LINQ vb.net GROUP BY - SELECT JOINED column - linq

I have a very basic SQL query I am trying to reproduce:
SELECT t1.id, t1.name, count(*), min(t2.inserted) as inserted_first, max(t2.inserted) as inserted_last
FROM tbl1 t1
LEFT JOIN tbl2 t2 ON t1.id=t2.tbl1_id
WHERE t2.search=15
GROUP BY t1.id, t1.name
This works perfect for me. It allows me to group by the unique items of t1.id and t1.name, but also get the amount of times this pair appears, and also the min and max value of t2.inserted for the linked table. Problem is, now when I turn this into LINQ I get:
Dim query =
From t1 In ent.TBL1
Join t2 In ent.TBL2 On t1.id Equals t2.tbl1_id
Where (t2.search=15)
Group t1 By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = ???,
.max_Value = ???
}
I am lost as to what i can do to select the min and max. Group.Min would work if it was of the same table as the grouping, however since it is in t2, I am not able to reference it. Also I can not add it to my group by, since its different.
Note that tbl2 links to tbl1 on tbl2.tbl1_id -> tbl1.id. Also this is a dumbed down example of my problem, not the real live schema.
I appreciate any help on this matter

I'm not a VB person, but I think you want:
Group t1, t2 By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = Group.Min(Function(x) x.t2.Inserted),
.max_Value = Group.Max(Function(x) x.t2.Inserted)
}
Note how here each group element contains both t1 and t2, which is how you're able to get to the Inserted property. It's not actually clear whether you even need t1 or any other parts of t2, so you may be able to use:
Group t2.Inserted By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = Group.Min(),
.max_Value = Group.Max()
}
Unfortunately my VB LINQ skills aren't up to the challenge of knowing whether that's right for you. If you'd be happy with it in C# (which you could then convert) I could help you more easily...

Related

Error when running a sub query in Oracle SQL

I am trying to join three tables using a sub query.
The result of the first left outer join is to be used with another table to get a composite view with all attributes.
I am getting an error where the compile says, Unknown Command for the table in the second join clause.
When I create two independent views and then join then it works fine.
(select
l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
from tgt_workday.learning l
left outer join ods_hrmaster.employee e
on l.EMPLOYEE_ID = e.employee#) t1
left outer join ( select
per_ids_id,
per_id,
id_pureid from
ods_pure.person_ids
) t2 on t1.wk_worker_id = t2.value where t2.type = 'Employee ID';
You can write it in a simple way. There is no need to make sub-queries as:
SELECT L.ENROLLED_CONTENT,
L.LEARNING_ENROLLMENT_LEARNER,
L.EMPLOYEE_ID,
L.JOB_FAMILY_GROUP,
L.EMPLOYEE_TYPE,
L.JOB_FAMILY,
L.LEARNING_ENROLLMENT,
L.COMPLETION_STATUS,
L.COMPLETION_DATE,
L.EXPIRATION_DATE,
L.CF_LRV_LEARNING_CONTENT_NUMBER,
L.LEARNING_CONTENT_DETAIL,
L.LEARNING_CONTENT_TYPE,
L.LESSON_TYPE,
E.ID# "WK_WORKER_ID"
FROM TGT_WORKDAY.LEARNING L
LEFT OUTER JOIN ODS_HRMASTER.EMPLOYEE E
ON L.EMPLOYEE_ID = E.EMPLOYEE#
LEFT OUTER JOIN ODS_PURE.PERSON_IDS T2
ON E.ID# = T2.VALUE
AND T2.TYPE = 'Employee ID';
Once you use the outer joined table's column in WHERE clause, It will result in the same result as inner join(there is another ways to use it in WHERE clause though). So it is better to avoid using outer joined table's column in the WHERE clause.
Try as
SELECT *
FROM ( (SELECT l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
FROM tgt_workday.learning l
LEFT OUTER JOIN ods_hrmaster.employee e
ON l.EMPLOYEE_ID = e.employee) t1
LEFT OUTER JOIN
(SELECT per_ids_id, per_id, id_pureid FROM ods_pure.person_ids) t2
ON t1.wk_worker_id = t2.VAL AND t2.TYPE = 'Employee ID')

How to group and uniqe each row with ORACLE?

Anyone please help me, how to uniqe each row
SELECT T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN AS SORT, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') AS DATE_FINISHED
FROM TB_WORK T1
JOIN TB_INSPECTION T2 ON T1.ID_INSPECTION=T2.ID_INSPECTION
JOIN TB_JOBSHEET T3 ON T2.ID_JS = T3.ID_JS
JOIN TB_METHODE_REPAIR T4 ON T1.ID_METHODE_REPAIR=T4.ID_METHODE_REPAIR
JOIN TB_DIVISI T5 ON T1.ID_DIVISI=T5.ID_DIVISI
where t3.id_js=142414
GROUP BY T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN,TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY')
ORDER BY T3.ID_JS, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') desc
[Result] (http://prntscr.com/sfpuuq)
Those rows are unique, so I'll guess: there are two rows with sort = 3 and you'd want to have only one. If that's so, regarding the fact that date value is the only thing that makes difference, a simple way is to use aggregate function (such as min or max). I used max, but you can change it if you want:
SELECT t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan AS sort,
MAX (TO_DATE (t1.time_finished_work, 'DD/MM/YYYY')) AS date_finished
FROM tb_work t1
JOIN tb_inspection t2 ON t1.id_inspection = t2.id_inspection
JOIN tb_jobsheet t3 ON t2.id_js = t3.id_js
JOIN tb_methode_repair t4
ON t1.id_methode_repair = t4.id_methode_repair
JOIN tb_divisi t5 ON t1.id_divisi = t5.id_divisi
WHERE t3.id_js = 142414
GROUP BY t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan
ORDER BY t3.id_js, date_finished DESC
You'd remove date column from GROUP BY; besides, if you wanted to select distinct rows, why didn't you apply select distinct instead of using group by? Result is the same, but - group by is generally used with aggregates, not to return distinct result.

Rewriting query with table join containing GROUP BY clause

Is it possible to rewrite the following query
SELECT CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM CURR_TABLE CT
JOIN (SELECT GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
FROM HIST_TABLE
WHERE STAT = 'A'
GROUP BY GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID
WHERE CT.GROUP = :1
AND CT.EMP_ID = :2
in a way that is similar to CROSS JOIN style?
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.
You may try writing your query as a single level join with an aggregation:
SELECT
CT.GROUP,
CT.EMP_ID,
MAX(HT.EFF_DT) AS EFF_DT
FROM CURR_TABLE CT
LEFT JOIN HIST_TABLE HT
ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID AND
HT.STAT = 'A'
WHERE
CT.GROUP = :1 AND
CT.EMP_ID = :2
GROUP BY
CT.GROUP,
CT.EMP_ID;
Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.

How to update and set values based on multiple joins in select statment?

I want to update a column prtnum and revlvl in table invdtl based on value from select statment, here is the code
update invdtl set invdtl.prtnum = usr_prtmst_xref.prtnum,invdtl.revlvl =
usr_prtmst_xref.colnam ([select
invdtl.prtnum,usr_prtmst_xref.prtnum AS
crossref,invdtl.revlvl,aremst.arecod,aremst.fwiflg from invdtl
join usr_prtmst_xref
on usr_prtmst_xref.prtnum = usr_prtmst_xref.prtnum
join invsub
join invlod
join locmst
join aremst
on aremst.arecod = locmst.arecod
and aremst.wh_id = locmst.wh_id
on locmst.stoloc = invlod.stoloc
and locmst.wh_id = invlod.wh_id
on invlod.lodnum = invsub.lodnum
on invsub.subnum = invdtl.subnum where aremst.arecod = 'EXPR' or
aremst.fwiflg = '1' and rownum <2])
I want to copy two values prtnum and revlvl that are returned by select statement but there is some syntax issue.
There are a bunch of errors here:
The syntax for a multi-column update is basically
update blah
set ( col1, col2 ) = ( select x, y
from
...
)
The syntax for multiple joins is basically
from table1 t1
join table2 t2
on t1.col = t2.col
join table3 t2 on
t2.col = ...
Get ride of "[" and "]"
The predicate rownum<2 is probably to get around the message you received, something like "single row sub-query returns more than 1
row" Which this predicate "fixes" that problem, you are just getting
the first random row; probably not what you want. You probably need to
correlate the sub-query with the update
I would fix these basic syntax errors and try again.

How do I return a table in LINQ that relies on a join/subquery?

I need the fields in 1 table contingent on 1 property matching rows in another table.
I can write this query in SQL with a subquery as such:
SELECT *
FROM Table1
WHERE Property1 IN
(
SELECT Property1
FROM Table2
WHERE Property0 = 1
)
But I read here that it's less complicated and just as easy to write with a join, which I did. However, so far I'm unable to return just Table1 as I'd like since I'm using a join, which if I'm not mistaken, requires me to create this anonymous type, as below. What I did here works (I created another object with the same properties of Table1 I need), but I can't help thinking there's a better way to do this.
Table1.Join(Table2, t1 => t1.Property1, t2 => t2.Property1, (t1, t2) => new
{
t1.Property1,
t1.Property2,
t1.Property3
})
.Select(ob => new UnnecessaryObject
{
Property1 = ob.Property1,
Property2 = ob.Property2,
Property3 = ob.Property3
}
I also tried just creating a Table1 in the .Select part, but I got an error about explicit construction not being allowed.
Just to clarify, I'd like to be able to return the IQueryable of type Table1, which it seems like I ought to be able to do without having to create UnnecessaryObject...but I'm still pretty new to LINQ, so I'd appreciate any help you can offer. Thanks in advance.
You could just do:
from t1 in table1
join t2 in table2 on t1.property1 equals t2.property1
select t1;
That would return a collection of table1 objects. This assumes from your example table1 is a collection of table1 objects and table2 is a collection of table2 objects.
The best translation of your original query I can come up with is:
from item in context.Table1
where context.Table2
.Where(x => x.Property0 == 0)
.Any(x => x.Property1 == item.Property1)
select item
This selects all items from Table1, where there's an item with matching Property1 and Property0 == 0 from Table2
It can also be solved with a join indeed. To get an efficient join, you need to have a relation between the two tables. Then you can do something like assuming the relation is called RelatedItems:
from item in context.Table1
join relatedItem in item.RelatedItems
on item.Property1 equals relatedItem.Property
where relatedItem.Property0 == 0
select item
This is equivalent to the SQL:
SELECT *
FROM Table1
JOIN Table2 ON Table1.Property1 = Table2.Property1
WHERE Table2.Property0 = 0

Resources