I have created the Datatable below which feeds a GridView on Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strQuery As String = "SELECT DISTINCT setsTbl.setName, trainingTbl.t_date, userAssessmentTbl.o_id FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl ON userAssessmentTbl.o_id = outcomesTbl.o_id WHERE UserId = #UserId ORDER BY setName, t_date DESC "
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.AddWithValue("#UserId", userString)
Dim dt As DataTable = GetData(cmd)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
At present it outputs values:
SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name2 15.07.12 2
name3 16.07.12 4
name3 17.07.12 3
I would like to get my DataTable below to output distinct rows like this:
SetName t_date o_id
name1 12.12.12 1
name2 13.07.12 1
name3 16.07.12 4
can anybody tell me hw this can be achieved?
It looks like you want the earliest date for each name. If so, you could use a Group By and the min function:
setsTbl.setName, Min(trainingTbl.t_date), Min(outcomesTbl.o_id)
FROM userAssessmentTbl LEFT JOIN trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id
LEFT JOIN setsTbl ON trainingTbl.s_id = setsTbl.s_id LEFT JOIN outcomesTbl
ON userAssessmentTbl.o_id = outcomesTbl.o_id
WHERE UserId = #UserId Group By setsTbl.setName ORDER BY setsTbl.setName
Related
I would like to join 4 table, main table in this example is "MAGAZ101" (records on screen).
Table "TOWARY" keep name of items, unit of measurement(kg, quantity), itc.
Table "MAGAZ01" keep info about item in stock (1 in, 0 sold).
Table "PRZYTOW" keep info about how many same items had been take to stock also real weight of item.
Tables "MAGAZ101", "TOWARY", "MAGAZ01" are join by fields "TOWAR".
My problem is join "MAGAZ01" and "PRZYTOW" by column "DOK_PRZYJ", group and sum weight in "PRZYTOW" and also sum stock in "MAGAZ01", sum if item in table "MAGAZ01" is "1".
Here is my Code:
'''''''''
Dim MAGAZYNGLOWNY = (From tab1 In MAGAZ101.AsEnumerable()
Join tab2 In TOWARY.AsEnumerable() On tab1.Field(Of String)("TOWAR") Equals tab2.Field(Of String)("TOWAR")
Join tab3 In MAGAZ01.AsEnumerable() On tab1.Field(Of String)("TOWAR") Equals tab3.Field(Of String)("TOWAR")
Join tab4 In PRZYTOW.AsEnumerable() On tab3.Field(Of String)("DOK_PRZYJ") Equals tab4.Field(Of String)("DOK_PRZYJ")
Select New With {
.Symbol = tab1.Field(Of String)("TOWAR"),
.Nazwa = tab2.Field(Of String)("NAZWA"),
.Jm = tab2.Field(Of String)("JM"),
.Stan = If(IsDBNull(tab3.Item("STANMAG")), 0, tab3.Field(Of Double)("STANMAG")),
.WAGA = If(IsDBNull(tab4.Item("WAGA")), 0, tab4.Field(Of Double)("WAGA"))
}).ToList
Group and sum:
Dim MAGAZYNGLOWNYSUMA = (From A1 In MAGAZYNGLOWNY
Group A1 By Key = New With {.SYMBOL = A1.Symbol, .NAZWA = A1.Nazwa, .JM = A1.Jm, .STAN = A1.Stan, .Waga = A1.WAGA} Into Group
Select New With {
.Symbol = Key.SYMBOL,
.Nazwa = Key.NAZWA,
.Jm = Key.JM,
.Stan = Group.Sum(Function(A) A.Stan),
.WAGA = Group.Sum(Function(A) A.WAGA)
})
.Symbol - ID of item,
.Nazwa - name of item,
.Jm - unit,
.STAN - how many in stock,
.WAGA - weight,
Thanks in advace Adam
If you need, ask for detail.
Here is correct working code.
1st "MAG_GLOW" joined tables 1 and 2.
2nd "dane" joined tables 3 and 4, by "TOWAR" and also "DOK_PRZYJ" in the result the table is sorted and easy to sum.
3rd "Stock" where stock is greater than 0 (sum "STAN") and where stock is greater than 0 (sum weight "WAGA")
4th "MAGAZYNGLOWNY" joined 1st step "MAG_GLOW" and 3rd step "Stock".
Dim MAG_GLOW = (From tab1 In MAGAZ101.AsEnumerable()
Join tab2 In TOWARY.AsEnumerable() On tab1.Field(Of String)("TOWAR") Equals tab2.Field(Of String)("TOWAR")
Select New With {
.TOWAR = tab1.Field(Of String)("TOWAR"),
.Nazwa = tab2.Field(Of String)("NAZWA"),
.Jm = tab2.Field(Of String)("JM")
}).ToList
Dim dane = (From TAB3 In MAGAZ01.AsEnumerable()
Join TAB4 In PRZYTOW.AsEnumerable() On TAB3.Field(Of String)("towar") Equals TAB4.Field(Of String)("towar") And TAB3.Field(Of String)("Dok_przyj") Equals TAB4.Field(Of String)("Dok_przyj")
Select New With
{
.TOWAR = TAB3.Field(Of String)("TOWAR"),
.STAN = If(IsDBNull(TAB3.Item("STANMAG")), 0, TAB3.Field(Of Double)("STANMAG")),
.WAGA = If(IsDBNull(TAB4.Item("WAGA")), 0, TAB4.Field(Of Double)("WAGA"))
})
Dim stock = (From MAG In dane
Group MAG By TOWAR = MAG.TOWAR Into G = Group
Select New With {
.TOWAR = TOWAR,
.Stan = G.Where(Function(x) x.STAN > 0).Sum(Function(x) x.STAN),
.Waga = G.Where(Function(x) x.STAN > 0).Sum(Function(x) x.WAGA)
}
)
Dim MAGAZYNGLOWNY = (From tab1 In MAG_GLOW
Group Join tab2 In stock On tab2.TOWAR Equals tab1.TOWAR Into co = Group
From tab2 In co.DefaultIfEmpty()
Order By tab1.TOWAR
Select New With
{.TOWAR = tab1.TOWAR,
.NAZWA = tab1.Nazwa,
.Jm = tab1.Jm,
.STAN = If(IsNothing(tab2), Nothing, tab2.Stan),
.WAGA = If(IsNothing(tab2), Nothing, tab2.Waga)
})
I'm trying to create a report that will display my table that has joins from different tables that looks likes this,
select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid
My table consist of 8 columns and 4 of it is column id's for joins, I can succesfully use the query when im trying to display to datagridview but when I try this in reportviewer it doesn't work and gives me error: "Object reference not sent to an instance of an object". This is what I tried,
Public Class frmInventoryReport
Public Property strReport2 As String
Private Sub frmInventoryReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.tblproductTableAdapter.Fill(Me.groceryDataSet.tblproduct)
Me.ReportViewer1.RefreshReport()
Dim rptDataSource As New ReportDataSource
Try
With Me.ReportViewer1.LocalReport
.ReportPath = "D:\VisualBasic\EELPOS - Copy\EELPOS\Report3.rdlc"
.DataSources.Clear()
End With
Select Case strReport2
Case "Report3"
Dim i As Integer = 0
Dim sqlQuery As String = "select * from tblproduct As p inner join tblbrand As b On p.bid = b.brandid inner Join tblclassification As c On p.cid = c.classid inner Join tblformulation As f On p.fid = f.formid inner Join tblgeneric As g On p.gid = g.genericid inner Join tbltype As t On p.tid = t.typeid"
Dim da As New MySqlDataAdapter(sqlQuery, cn)
Dim dt As DataTable = New DataTable()
While dr.Read
dt.Rows.Add(i, dr.Item("barcode").ToString, dr.Item("generic").ToString, dr.Item("classification").ToString, dr.Item("type").ToString, dr.Item("formulation").ToString, dr.Item("price").ToString, dr.Item("qty").ToString)
End While
da.Fill(dt)
rptDataSource = New ReportDataSource("inventory", dt)
End Select
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Me.ReportViewer1.RefreshReport()
End Sub
End Class
What's the correct way to do it?
Dim ID_Section as Int32 = 10
Dim Query = From Book1 In db.Book1
Group Join Section In db.Section On CInt(Book1.ID_Section) Equals Section.ID_section _
And Section.ID_section Equals (ID_Section) Into Section_join = Group
From Section In Section_join.DefaultIfEmpty()
Select
Book1.ID_Book,
Book1.Name_Book,
ID_section = Section.ID_section,
Name_Section = Section.Name_Section
The error appears in the variable id_Section, since the Linq does not accept values from the outside, as it seems to me of course.
Here Error :
And Section.ID_section Equals (ID_Section)
In SQL Query Use At :
Declare #ID_Section int
SELECT Book.ID_Book, Book.Name_Book, Section.ID_section, Section.Name_Section
FROM Book LEFT OUTER JOIN
Section ON Book.ID_Section = Section.ID_section and Section.ID_section = #ID_Section
where Book.ID_Book =1
Using Where on db.Section with lambda syntax:
Dim Query = From Book1 In db.Book1
Group Join Section In db.Section.Where(Function(s) s.ID_section = ID_Section)
On CInt(Book1.ID_Section) Equals Section.ID_section _
Into Section_join = Group
From Section In Section_join.DefaultIfEmpty()
Select
Book1.ID_Book,
Book1.Name_Book,
ID_section = Section.ID_section,
Name_Section = Section.Name_Section
Alternatively you can apply the Where to the Join results:
Dim Query = From Book1 In db.Book1
Group Join Section In db.Section
On CInt(Book1.ID_Section) Equals Section.ID_section _
Into Section_join = Group
From Section In Section_join.Where(Function(s) s.ID_section = ID_Section).DefaultIfEmpty()
Select
Book1.ID_Book,
Book1.Name_Book,
ID_section = Section.ID_section,
Name_Section = Section.Name_Section
I have a stored procedure and often that procedure will return multiple rows, for example:
ID Type Price
1234 A 2.260
1234 B 2.690
1234 C 2.990
1234 D 2.690
1234 D 2.790
1234 D 2.650
1234 D 2.680
And I want to output the latest value for each type. In my data reader I have:
While dr.Read
result.price.TypeA= dr("price")
result.price.TypeB= dr("price")
result.price.TypeC= dr("price")
result.price.TypeD= dr("price")
End While
and my query looks like:
select sm_id,
type,
price
from ** WITH (NOLOCK)
where id = #id
order by id desc
I'm not sure how to store all of my results into my object so I can access them in my front end.
This is may be not most performant query but it will do the job as what you described it
select t1.id, t1.t, t1.maxDt, t2.price
from
(select id, [TYPE] t, MAX(ud) maxDt
from dbo.a it
where id =1
group by id, [type]) t1
left join
dbo.a t2 on t1.id = t2.id and t1.t = t2.[type] and t1.maxDt = t2.ud
Now, storing results in object and displaying it
Public Class MyClass
Public Property Id As Integer
Public Property [Type] As String
Public Property [Date] As DateTime
Public Property Price As Decimal
End Class
. . . . .
Dim myList As New List(Of MyClass)()
While dr.Read()
Dim item As New MyClass()
item.Id = dr("id")
item.[Type] = dr("t")
item.[Date] = dr("maxDt")
item.Price = dr("price")
myList.Add(item)
End While
myDataGrid.DataSource = myList
This is what you, approximately, need
I have to convert it to Linq in vb.net. I am new to sql to linq. Guidance welcomed
select CONVERT(VARCHAR(10),a.StartDt,112) datenew,
COUNT(distinct(b.EmployerAccountOid)) companymoved,
COUNT(distinct(c.EmployerAccountOid)) companyfailed,
COUNT(distinct(d.ProductAccountOid)) planmoved,
COUNT(distinct(e.ProductAccountOid)) planfailed
from ebp.MgnCOREDCDataGroupMigrationRun a
left join ebp.MgnCOREDCMigrationRun b
on a.MigrationRunID = b.MigrationRunID
And TypeCd = 1 and a.MigrationStatusCd = 4
left join ebp.MgnCOREDCMigrationRun c
on a.MigrationRunID = c.MigrationRunID
and TypeCd = 1 and a.MigrationStatusCd = 5
left join ebp.MgnCOREDCMigrationRun d
on a.MigrationRunID = d.MigrationRunID
and TypeCd = 2 and a.MigrationStatusCd = 4
left join ebp.MgnCOREDCMigrationRun e
on a.MigrationRunID = e.MigrationRunID
and TypeCd = 2 and a.MigrationStatusCd = 5
group by CONVERT(VARCHAR(10),a.StartDt,112)
I tried to convert it to Linq with fail.
Dim query1= (From migrationgroup In UnitOfWork.DbContext.Set( Of MgnCOREDCDataGroupMigrationRun)()
Group Join migration In UnitOfWork.Set(of MgnCOREDCMigrationRun)() On migrationgroup.MigrationRunID Equals migration.MigrationRunID And migrationgroup.TypeCode = 1 And migrationgroup.MigrationStatusCode=4 _
Into migrationErrorGrp = Group
From mgeg In migrationErrorGrp.DefaultIfEmpty()
Group Join migration1 In UnitOfWork.Set(of MgnCOREDCMigrationRun)() On migration1.MigrationRunID Equals migrationgroup.MigrationRunID And migrationgroup.TypeCode = 1 And migrationgroup.MigrationStatusCode=4 _
Into migrationErrorGrp1 = Group
From mgeg1 In migrationErrorGrp1.DefaultIfEmpty()
Group Join migration2 In UnitOfWork.Set(of MgnCOREDCMigrationRun)() On migration2.MigrationRunID Equals migrationgroup.MigrationRunID And migrationgroup.TypeCode = 2 And migrationgroup.MigrationStatusCode=5 _
Into migrationErrorGrp2 = Group
From mgeg2 In migrationErrorGrp2.DefaultIfEmpty()
Group Join migration3 In UnitOfWork.Set(of MgnCOREDCMigrationRun)() On migration3.MigrationRunID Equals migrationgroup.MigrationRunID And migrationgroup.TypeCode = 2 And migrationgroup.MigrationStatusCode=5 _
Into migrationErrorGrp3 = Group
From mgeg3 In migrationErrorGrp3.DefaultIfEmpty()
Group By CONVERT(VARCHAR(10),migrationgroup.StartDt,112) into g
select New With{CONVERT(VARCHAR(10),migrationgroup.StartDt,112),
Count(distinct(migration.EmployerAccountOid)) ,
Count(distinct(migration1.EmployerAccountOid)),
Count(distinct(migration2.EmployerAccountOid)),
Count(distinct(migration3.EmployerAccountOid))}).ToList()
If IsNothing(query1) Then
Return Nothing
End If
coredcmigrationhistory =
From coredcmigrationrow In query1()
My query is non-queryable. Can anybody guide me where I m goin wrong