Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:06:34+00:00 2026-06-15T19:06:34+00:00

I have a database with two tables. Advertisements and Records as it’s foreign key.

  • 0

I have a database with two tables. Advertisements and Records as it’s foreign key. I need to count how often a record of an advertisement occurs and order by ascending.

I am assuming that I have to use a combination of a join and a group by clause to accomplish this. It just isn’t working the way I would like. Below is the code I have so far.

    Public Shared Function SortAdsByHitCount() As DataTable
    Dim dt As New DataTable

    Try
        Using context As New QREntities()
            Dim ads = (From advertisement In context.QRAdvertisements
                      Join record In context.QRRecords On advertisement.adId Equals record.adId
                      Select advertisement.adId, advertisement.adRedirectUrl, advertisement.adType, advertisement.adData, count = record
                      Order By AdData Ascending).ToList()

            'Dim ads = (From advertisement In context.QRAdvertisements
            '          Group advertisement.adId By advertisement.adId Into Group _
            '          Order By Group.Count() Ascending _
            '          Select Advertisement.AdRedirectUrl, Advertisement.AdType, Advertisement.AdData, Count = Group.Count()).ToList()

            'Set the column headers and their accepted data types
            dt.Columns.Add("adId", GetType(Integer))
            dt.Columns.Add("adRedirectUrl", GetType(String))
            dt.Columns.Add("adType", GetType(String))
            dt.Columns.Add("adData", GetType(String))
            dt.Columns.Add("hitCount", GetType(Integer))

            For Each a In ads
                dt.Rows.Add(a.adId, a.adRedirectUrl, a.adType, a.adData, a.Count)
            Next
        End Using
    Catch ex As Exception
        Return Nothing
    End Try

    Return dt
End Function

I basically need 4 rows from one table and I just include the count of the occurrence from the other. The advertisement datarow must know how many times it has been represented in the records table. So essentially, the gridview/listview would show the user a list of ads sorted by the frequency that that ad has been used.

Ex:
Advertisements

Ad id   Ad Type         Ad Date          Ad Occurrence

1       Automotive      13/12/2012       10
2       Personal        10/12/2012       5
3       Retail          02/11/2012       3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:06:35+00:00Added an answer on June 15, 2026 at 7:06 pm

    Here you need to be using a grouping function so you have access to the .Count() extension of the group. To do this in static syntax would look like this:

    Dim ads = (From advertisement In context.QRAdvertisements
                          Group Join record In context.QRRecords 
                          On advertisement.adId Equals record.adId 
                          Into records = Group
                          Select advertisement.adId, advertisement.adRedirectUrl, advertisement.adType, advertisement.adData, count = records.Count()
                          Order By AdData Ascending).ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database two tables and a linking table that I need a
I have two database tables with the following structure: actions: action_id int(11) primary key
I have a database that has two tables, one of which contains a foreign
I have two database tables Articles Archive Each article can have multiple records in
I have a database with two tables, ProductLine and Supplier. ProductLine contains the foreign
I have two database tables, users and logs. I need SQL code to do
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I have a database with two tables - let's call them Foo and Bar.
I have a database with two tables, tblPlayers and tblMatches. tblPlayers contains the following
I have a database with two tables: data and file . file_id is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.