Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# and SQL Server :: Find Most Common Via Junction Table

C# and SQL Server :: Find Most Common Via Junction Table

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt U
    wrote on last edited by
    #1

    Hi, CP. I have a database hosted on an instance of SQL Server 2008 Express. I am using C# (.NET 4.0) and VS2010. I created a junction table in order to use a many-to-many relationship between two tables. I retrieve and manipulate data primarily using the main table but a part of the application requires me to find the most common value for a particular field, which is stored in a junction table. For example, I call a SELECT statement which retrieves the fields from the table [Repair Data]. The junction table, [Failure Codes], contains the fields [Serial Number] and Failure. In order to find the most common [Failure Codes].[Failure] value based on a [Repair Data] record ([Repair Data] includes a field [Serial Number], which is how the junction table knows which [Repair Data] record it belongs to) would I do something like this:

    reader = SELECT * FROM [Repair Data]

    foreach (Record r in reader)
    {
    // Do something with [Repair Data] fields

    /\* Find most common \[Failure Codes\].\[Failure\] in a separate query \*/
    

    }

    or is there a simpler method for achieving these results? I think I explained it correctly. I am currently not at work and I try to forget work when I'm at home ;P. So I do not recall exactly what code I left off with. But I am quite positive I followed the "code" above. Thanks CP.

    djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

    L 1 Reply Last reply
    0
    • M Matt U

      Hi, CP. I have a database hosted on an instance of SQL Server 2008 Express. I am using C# (.NET 4.0) and VS2010. I created a junction table in order to use a many-to-many relationship between two tables. I retrieve and manipulate data primarily using the main table but a part of the application requires me to find the most common value for a particular field, which is stored in a junction table. For example, I call a SELECT statement which retrieves the fields from the table [Repair Data]. The junction table, [Failure Codes], contains the fields [Serial Number] and Failure. In order to find the most common [Failure Codes].[Failure] value based on a [Repair Data] record ([Repair Data] includes a field [Serial Number], which is how the junction table knows which [Repair Data] record it belongs to) would I do something like this:

      reader = SELECT * FROM [Repair Data]

      foreach (Record r in reader)
      {
      // Do something with [Repair Data] fields

      /\* Find most common \[Failure Codes\].\[Failure\] in a separate query \*/
      

      }

      or is there a simpler method for achieving these results? I think I explained it correctly. I am currently not at work and I try to forget work when I'm at home ;P. So I do not recall exactly what code I left off with. But I am quite positive I followed the "code" above. Thanks CP.

      djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

      L Offline
      L Offline
      Lukasz Nowakowski
      wrote on last edited by
      #2

      I don't know if I understand correctly your issue. I understand the issue is, that you have tables: 1. [Repaired item] ([Serial Number], [Some data about item]) - stores information about items, that are repaired (car, or whatever). 2. [Failure Codes] ([Failure Code], [Information about failure]) - stores information about failures, that can be repaired. 3. [Repair Data] ([Serial Number], [Failure Code]) - stores connection between [Repaired item] and [Failure Codes]. With the structure above you want (for the given [Serial Number]) find the failure that occurs most often. If the above is correct, then I think you should do this in SQL using the following query:

      SELECT
      [Serial Number],
      [Failure Code],
      COUNT(*) AS [Counter]
      FROM
      [Failure Codes]
      WHERE
      [Serial Number] = @SerialNumber
      GROUP BY
      [Serial Number],
      [Failure Code]
      ORDER BY
      [Counter] ASC

      And you can retrieve it easily from C# without any loops.

      Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups