Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1196 connectés 

  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  [SGBD] AVG d'un COUNT euh...

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[SGBD] AVG d'un COUNT euh...

n°1307851
Arjuna
Aircraft Ident.: F-MBSD
Posté le 17-02-2006 à 11:56:06  profilanswer
 

Salut,
 
J'ai un table qui me sert à faire des stats de fréquentation.
 
Sans entrer dans la complexité du truc, voudrais savoir si vous voyez un moyen simple de faire la moyenne du nombre de hits pour une période donnée.
 
Mettons par exemple, actuellement, j'ai ça :
 
select <<jour_de_la_semaine>>, count(*) from wt_weblog where <<la semaine en cours>> group by <<jour_de_la_semaine>>
(je vous passe le détail de la syntaxe, surtout que la requête est bien plus compliquée que ça :D)
 
ensuite, j'ai la même requête pour récupérer les mêmes infos sur la période précédente.
 
et maintenant, afin de mieu voir l'évolution de la chose, je voudrais ajouter aussi la moyenne.
 
c'est à dire "la moyenne du nombre de hits par jours de la semaine sur l'ensemble des dates jusqu'à la semaine dernière" (histoire que la semaine en cours ne vienne pas polluer la moyenne)
 
et là, mise à part en faisant une sous-requête, je vois pas trop.
 
seulement ma requête est un peut beaucoup très complexe (100% dynamique, les critères de sélection, de regroupement et de filtres sont 100% dynamiques) et je sens que ça va me péter à la tronche...
 
en effet, je pourrais bien faire !
 
select <<jour_de_la_semaine>>, agv(*)
from
(
select <<date>>, count(*)
from wt_web_logs
where <<tout_sauf_la_semaine_en_cours>>
group by date
)
group by <<jour_de_la_semaine>>
 
mais là je la sens pas... (notamment parceque là je parle en jours, mais je peux aussi avoir des heures, des mois, etc.)
 
Bon, c pas clair (t'ain pis chuis pas réveillé ça m'énerve !)
 
Voilà le code, je pense que vous comprendrez mieu ma détresse :D
(mais nan c'est pas goret, ça marche :o)
 

Code :
  1. private void FillFilters()
  2.  {
  3.   if (canRefresh)
  4.   {
  5.    canRefresh = false;
  6.    int i;
  7.    ComboboxItem oldItem = new ComboboxItem();
  8.    siteCondition = "";
  9.    codsocCondition = "";
  10.    pageCondition = "";
  11.    typtieCondition = "";
  12.    sigtieCondition = "";
  13.    codproCondition = "";
  14.    periodeCondition1 = "";
  15.    periodeCondition2 = "";
  16.    if (site.SelectedItem != null && ((ComboboxItem)site.SelectedItem).val != 0)
  17.    {
  18.     siteCondition = string.Format(" and host = '{0}'", ((ComboboxItem)site.SelectedItem).text);
  19.    }
  20.    if (codsoc.SelectedItem != null && ((ComboboxItem)codsoc.SelectedItem).val != 0)
  21.    {
  22.     codsocCondition = string.Format(" and codsoc = {0}", ((ComboboxItem)codsoc.SelectedItem).val.ToString());
  23.    }
  24.    if (page.SelectedItem != null && ((ComboboxItem)page.SelectedItem).val != 0)
  25.    {
  26.     pageCondition = string.Format(" and page = '{0}'", ((ComboboxItem)page.SelectedItem).text);
  27.    }
  28.    if (typtie.SelectedItem != null && ((ComboboxItem)typtie.SelectedItem).val != 0)
  29.    {
  30.     typtieCondition = string.Format(" and typtie = '{0}'", ((ComboboxItem)typtie.SelectedItem).text);
  31.    }
  32.    if (sigtie.SelectedItem != null && ((ComboboxItem)sigtie.SelectedItem).val != 0)
  33.    {
  34.     sigtieCondition = string.Format(" and sigtie = '{0}'", ((ComboboxItem)sigtie.SelectedItem).text);
  35.    }
  36.    if (codpro.SelectedItem != null && ((ComboboxItem)codpro.SelectedItem).val != 0)
  37.    {
  38.     codproCondition = string.Format(" and codpro = '{0}'", ((ComboboxItem)codpro.SelectedItem).text);
  39.    }
  40.    if (periode.SelectedItem != null && ((ComboboxItem)periode.SelectedItem).val != 0)
  41.    {
  42.     switch (((ComboboxItem)periode.SelectedItem).val)
  43.     {
  44.      case 1:
  45.       periodeCondition1 = " and datlog = to_char(sysdate, 'YYYYMMDD')";
  46.       periodeCondition2 = " and datlog = to_char(sysdate - 1, 'YYYYMMDD')";
  47.       break;
  48.      case 2:
  49.       periodeCondition1 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYYIW') = to_char(sysdate, 'YYYYIW')";
  50.       periodeCondition2 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYYIW') = to_char(sysdate - 7, 'YYYYIW')";
  51.       break;
  52.      case 3:
  53.       periodeCondition1 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYYMM') = to_char(sysdate , 'YYYYMM')";
  54.       periodeCondition2 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYYMM') = to_char(add_months(sysdate, -1) , 'YYYYMM')";
  55.       break;
  56.      case 4:
  57.       periodeCondition1 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYY') = to_char(sysdate, 'YYYY')";
  58.       periodeCondition2 = " and to_char(to_date(datlog, 'YYYYMMDD'), 'YYYY') = to_char(add_months(sysdate, -12), 'YYYY')";
  59.       break;
  60.      default:
  61.       break;
  62.     }
  63.    }
  64.    // site
  65.    if (site.SelectedItem != null)
  66.    {
  67.     oldItem = (ComboboxItem)site.SelectedItem;
  68.    }
  69.    else
  70.    {
  71.     oldItem.Set(0, "" );
  72.    }
  73.    OleDbCommand siteCmd = new OleDbCommand(string.Format("select distinct host from wt_weblog where 1 = 1{0}{1}{2}{3}{4}{5}{6} order by host", "", codsocCondition, pageCondition, typtieCondition, sigtieCondition, codproCondition, "" ), cnx);
  74.    OleDbDataReader siteDr = siteCmd.ExecuteReader(CommandBehavior.Default);
  75.    i = 1;
  76.    site.Items.Clear();
  77.    ComboboxItem siteItem = new ComboboxItem();
  78.    siteItem.Set(0, "Site" );
  79.    site.Items.Add(siteItem);
  80.    while (siteDr.Read())
  81.    {
  82.     siteItem.Set(i++, siteDr.GetString(0));
  83.     site.Items.Add(siteItem);
  84.    }
  85.    site.SelectedIndex = 0;
  86.    foreach (ComboboxItem item in site.Items)
  87.    {
  88.     if (item.isEqual(oldItem))
  89.     {
  90.      site.SelectedItem = item;
  91.      break;
  92.     }
  93.    }
  94.    siteDr.Close();
  95.    siteDr = null;
  96.    siteCmd.Dispose();
  97.    siteCmd = null;
  98.    // codsoc
  99.    if (codsoc.SelectedItem != null)
  100.    {
  101.     oldItem = (ComboboxItem)codsoc.SelectedItem;
  102.    }
  103.    else
  104.    {
  105.     oldItem.Set(0, "" );
  106.    }
  107.    OleDbCommand codsocCmd = new OleDbCommand(string.Format("select distinct ut_soc.soc, ut_soc.libut_soc from ut_soc, wt_weblog where ut_soc.soc = wt_weblog.codsoc{0}{1}{2}{3}{4}{5}{6} order by libut_soc", siteCondition, "", pageCondition, typtieCondition, sigtieCondition, codproCondition, "" ), cnx);
  108.    OleDbDataReader codsocDr = codsocCmd.ExecuteReader(CommandBehavior.Default);
  109.    codsoc.Items.Clear();
  110.    ComboboxItem codsocItem = new ComboboxItem();
  111.    codsocItem.Set(0, "Société" );
  112.    codsoc.Items.Add(codsocItem);
  113.    while (codsocDr.Read())
  114.    {
  115.     codsocItem.Set(System.Convert.ToInt32(codsocDr.GetDecimal(0)), codsocDr.GetString(1));
  116.     codsoc.Items.Add(codsocItem);
  117.    }
  118.    codsoc.SelectedIndex = 0;
  119.    foreach (ComboboxItem item in codsoc.Items)
  120.    {
  121.     if (item.isEqual(oldItem))
  122.     {
  123.      codsoc.SelectedItem = item;
  124.      break;
  125.     }
  126.    }
  127.    codsocDr.Close();
  128.    codsocDr = null;
  129.    codsocCmd.Dispose();
  130.    codsocCmd = null;
  131.    // page
  132.    if (page.SelectedItem != null)
  133.    {
  134.     oldItem = (ComboboxItem)page.SelectedItem;
  135.    }
  136.    else
  137.    {
  138.     oldItem.Set(0, "" );
  139.    }
  140.    OleDbCommand pageCmd = new OleDbCommand(string.Format("select distinct page from wt_weblog where 1 = 1{0}{1}{2}{3}{4}{5}{6} order by page", siteCondition, codsocCondition, "", typtieCondition, sigtieCondition, codproCondition, "" ), cnx);
  141.    OleDbDataReader pageDr = pageCmd.ExecuteReader(CommandBehavior.Default);
  142.    i = 1;
  143.    page.Items.Clear();
  144.    ComboboxItem pageItem = new ComboboxItem();
  145.    pageItem.Set(0, "Page" );
  146.    page.Items.Add(pageItem);
  147.    while (pageDr.Read())
  148.    {
  149.     pageItem.Set(i++, pageDr.GetString(0));
  150.     page.Items.Add(pageItem);
  151.    }
  152.    page.SelectedIndex = 0;
  153.    foreach (ComboboxItem item in page.Items)
  154.    {
  155.     if (item.isEqual(oldItem))
  156.     {
  157.      page.SelectedItem = item;
  158.      break;
  159.     }
  160.    }
  161.    pageDr.Close();
  162.    pageDr = null;
  163.    pageCmd.Dispose();
  164.    pageCmd = null;
  165.    // typtie
  166.    if (typtie.SelectedItem != null)
  167.    {
  168.     oldItem = (ComboboxItem)typtie.SelectedItem;
  169.    }
  170.    else
  171.    {
  172.     oldItem.Set(0, "" );
  173.    }
  174.    OleDbCommand typtieCmd = new OleDbCommand(string.Format("select distinct typtie from wt_weblog where 1 = 1{0}{1}{2}{3}{4}{5}{6} order by typtie", siteCondition, codsocCondition, pageCondition, "", sigtieCondition, codproCondition, "" ), cnx);
  175.    OleDbDataReader typtieDr = typtieCmd.ExecuteReader(CommandBehavior.Default);
  176.    i = 1;
  177.    typtie.Items.Clear();
  178.    ComboboxItem typtieItem = new ComboboxItem();
  179.    typtieItem.Set(0, "typtie" );
  180.    typtie.Items.Add(typtieItem);
  181.    while (typtieDr.Read())
  182.    {
  183.     typtieItem.Set(i++, typtieDr.GetString(0));
  184.     typtie.Items.Add(typtieItem);
  185.    }
  186.    typtie.SelectedIndex = 0;
  187.    foreach (ComboboxItem item in typtie.Items)
  188.    {
  189.     if (item.isEqual(oldItem))
  190.     {
  191.      typtie.SelectedItem = item;
  192.      break;
  193.     }
  194.    }
  195.    typtieDr.Close();
  196.    typtieDr = null;
  197.    typtieCmd.Dispose();
  198.    typtieCmd = null;
  199.    // sigtie
  200.    if (sigtie.SelectedItem != null)
  201.    {
  202.     oldItem = (ComboboxItem)sigtie.SelectedItem;
  203.    }
  204.    else
  205.    {
  206.     oldItem.Set(0, "" );
  207.    }
  208.    OleDbCommand sigtieCmd = new OleDbCommand(string.Format("select distinct sigtie from wt_weblog where 1 = 1{0}{1}{2}{3}{4}{5}{6} order by sigtie", siteCondition, codsocCondition, pageCondition, typtieCondition, "", codproCondition, "" ), cnx);
  209.    OleDbDataReader sigtieDr = sigtieCmd.ExecuteReader(CommandBehavior.Default);
  210.    i = 1;
  211.    sigtie.Items.Clear();
  212.    ComboboxItem sigtieItem = new ComboboxItem();
  213.    sigtieItem.Set(0, "sigtie" );
  214.    sigtie.Items.Add(sigtieItem);
  215.    while (sigtieDr.Read())
  216.    {
  217.     sigtieItem.Set(i++, sigtieDr.GetString(0));
  218.     sigtie.Items.Add(sigtieItem);
  219.    }
  220.    sigtie.SelectedIndex = 0;
  221.    foreach (ComboboxItem item in sigtie.Items)
  222.    {
  223.     if (item.isEqual(oldItem))
  224.     {
  225.      sigtie.SelectedItem = item;
  226.      break;
  227.     }
  228.    }
  229.    sigtieDr.Close();
  230.    sigtieDr = null;
  231.    sigtieCmd.Dispose();
  232.    sigtieCmd = null;
  233.    // codpro
  234.    if (codpro.SelectedItem != null)
  235.    {
  236.     oldItem = (ComboboxItem)codpro.SelectedItem;
  237.    }
  238.    else
  239.    {
  240.     oldItem.Set(0, "" );
  241.    }
  242.    OleDbCommand codproCmd = new OleDbCommand(string.Format("select distinct codpro from wt_weblog where 1 = 1{0}{1}{2}{3}{4}{5}{6} order by codpro", siteCondition, codsocCondition, pageCondition, typtieCondition, sigtieCondition, "", "" ), cnx);
  243.    OleDbDataReader codproDr = codproCmd.ExecuteReader(CommandBehavior.Default);
  244.    i = 1;
  245.    codpro.Items.Clear();
  246.    ComboboxItem codproItem = new ComboboxItem();
  247.    codproItem.Set(0, "codpro" );
  248.    codpro.Items.Add(codproItem);
  249.    while (codproDr.Read())
  250.    {
  251.     codproItem.Set(i++, codproDr.GetString(0));
  252.     codpro.Items.Add(codproItem);
  253.    }
  254.    codpro.SelectedIndex = 0;
  255.    foreach (ComboboxItem item in codpro.Items)
  256.    {
  257.     if (item.isEqual(oldItem))
  258.     {
  259.      codpro.SelectedItem = item;
  260.      break;
  261.     }
  262.    }
  263.    codproDr.Close();
  264.    codproDr = null;
  265.    codproCmd.Dispose();
  266.    codproCmd = null;
  267.    // periode
  268.    if (periode.SelectedItem != null)
  269.    {
  270.     oldItem = (ComboboxItem)periode.SelectedItem;
  271.    }
  272.    else
  273.    {
  274.     oldItem.Set(0, "" );
  275.    }
  276.    periodeItems = new ComboboxItem[5];
  277.    periodeItems[0].Set(0, "Période" );
  278.    periodeItems[1].Set(1, "Jour" );
  279.    periodeItems[2].Set(2, "Semaine" );
  280.    periodeItems[3].Set(3, "Mois" );
  281.    periodeItems[4].Set(4, "Année" );
  282.    periode.Items.Clear();
  283.    foreach (ComboboxItem item in periodeItems)
  284.    {
  285.     periode.Items.Add(item);
  286.    }
  287.    periode.SelectedIndex = 0;
  288.    foreach (ComboboxItem item in periode.Items)
  289.    {
  290.     if (item.isEqual(oldItem))
  291.     {
  292.      periode.SelectedItem = item;
  293.      break;
  294.     }
  295.    }
  296.    LoadStats();
  297.    canRefresh = true;
  298.   }
  299.  }
  300.  private void LoadStats()
  301.  {
  302.   statusBar1.Text = "Chargement des données...";
  303.   statusBar1.Refresh();
  304.   groupBy = string.Empty;
  305.   orderBy = string.Empty;
  306.   if (radioButton1.Checked)
  307.   {
  308.    groupBy = "host";
  309.   }
  310.   else if (radioButton2.Checked)
  311.   {
  312.    groupBy = "to_char(codsoc)";
  313.   }
  314.   else if (radioButton3.Checked)
  315.   {
  316.    groupBy = "page";
  317.   }
  318.   else if (radioButton4.Checked)
  319.   {
  320.    groupBy = "typtie";
  321.   }
  322.   else if (radioButton5.Checked)
  323.   {
  324.    groupBy = "sigtie";
  325.   }
  326.   else if (radioButton6.Checked)
  327.   {
  328.    groupBy = "codpro";
  329.   }
  330.   else if (radioButton7.Checked)
  331.   {
  332.    switch (((ComboboxItem)periode.SelectedItem).val)
  333.    {
  334.     case 0:
  335.      groupBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'YYYY')";
  336.      break;
  337.     case 1:
  338.      groupBy = "to_char(to_date(wt_weblog.heulog, 'HH24:MI:SS'), 'HH24')";
  339.      break;
  340.     case 2:
  341.      groupBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'DAY')";
  342.      orderBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'D')";
  343.      break;
  344.     case 3:
  345.      groupBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'DD')";
  346.      break;
  347.     case 4:
  348.      groupBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'MONTH')";
  349.      orderBy = "to_char(to_date(wt_weblog.datlog, 'YYYYMMDD'), 'MM')";
  350.      break;
  351.     default:
  352.      break;
  353.    }
  354.   }
  355.   string sql1 = "select {0} a, count(*) b, 0 c, {9} d from wt_weblog where 1 = 1{1}{2}{3}{4}{5}{6}{7}{8}";
  356.   string sql2 = "select {0} a, 0 b, count(*) c, {9} d from wt_weblog where 1 = 1{1}{2}{3}{4}{5}{6}{7}{8}";
  357.   if (groupBy == "" )
  358.   {
  359.    sql1 = string.Format(sql1, "'tous'", siteCondition, codsocCondition, pageCondition, typtieCondition, sigtieCondition, codproCondition, periodeCondition1, "", "null" );
  360.   }
  361.   else
  362.   {
  363.    sql1 = string.Format(sql1, string.Format("{0}", groupBy), siteCondition, codsocCondition, pageCondition, typtieCondition, sigtieCondition, codproCondition, periodeCondition1, string.Format(" group by {0}", (orderBy==string.Empty)?groupBy:orderBy + ", " +groupBy), (orderBy==string.Empty)?"null":orderBy);
  364.   }
  365.   if (groupBy == "" )
  366.   {
  367.    sql2 = string.Format(sql2, "'tous'", siteCondition, codsocCondition, pageCondition, typtieCondition, sigtieCondition, codproCondition, periodeCondition2, "", "null" );
  368.   }
  369.   else
  370.   {
  371.    sql2 = string.Format(sql2, string.Format("{0}", groupBy), siteCondition, codsocCondition, pageCondition, typtieCondition, sigtieCondition, codproCondition, periodeCondition2, string.Format(" group by {0}", (orderBy==string.Empty)?groupBy:orderBy + ", " +groupBy), (orderBy==string.Empty)?"null":orderBy);
  372.   }
  373.   string sql = string.Format("select a \"Objet\", sum(b) \"Période d'étude\", sum(c) \"Période précédente\" from ({0} union {1}) tmp group by d, a", sql1, sql2);
  374.   OleDbCommand statsCmd = new OleDbCommand(sql, cnx);
  375.   OleDbDataAdapter da = new OleDbDataAdapter(statsCmd);
  376.   DataSet ds = new DataSet();
  377.   da.Fill(ds);
  378.   dataGrid1.DataSource = ds.Tables[0];
  379.   chart1.dataSource = ds;
  380.   chart1.DataBind();
  381.   ds.Dispose();
  382.   ds = null;
  383.   da.Dispose();
  384.   da = null;
  385.   statsCmd.Dispose();
  386.   statsCmd = null;
  387.   statusBar1.Text = "Données chargées";
  388.   statusBar1.Refresh();
  389.  }


 
Ca fait peur hein ? :D
 
-- Edit : Je me disais "tiens, elles sont passées où mes conditions ?" Forcément, j'avais oublié de poster la moitié du code :o --


Message édité par Arjuna le 17-02-2006 à 12:04:31
mood
Publicité
Posté le 17-02-2006 à 11:56:06  profilanswer
 

n°1307978
Arjuna
Aircraft Ident.: F-MBSD
Posté le 17-02-2006 à 14:04:21  profilanswer
 

Hmmm ouais, nan, je vais laisser tomber l'idée en fait.
 
En effet, si j'ai des jours avec aucune fréquentation, ils ne seront de toute façon pas pris en compte par la moyenne.
Du coup ça marche pas.
 
Reste la solution de compter le nombre de jours entre la plus petite dat de log et la plus grande, et diviser le count par ça, mais ça va être le bordel pour compter les mois, les semaines et autres.
 
Allez, zou ! Je laisse tomber :D


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  [SGBD] AVG d'un COUNT euh...

 

Sujets relatifs
Demande de conseils pour SGBD[SGBD] Algo d'une requête
Equivalent de plusieurs COUNT(x WHERE y = z) dans une requête ?[SGBD/SQL] Existe-t-il une API C qui gererait plusieurs SGBD ?
SGBD 4D[SGBD] Encore une requête à décorner les boeufs...
[SGBD] Oracle 8i, TextSearch : besoin d'infosrequete+champ+count
fonction select count???[resolu-merci!][SGBD][SQL]Création d'une base de données
Plus de sujets relatifs à : [SGBD] AVG d'un COUNT euh...


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR