比较逐月数据并发现SQL

时间:2019-03-21 18:23:32

标签: sql database data-analysis

如何动态计算同一张表的月差异。

我有下表,我正在尝试比较每个月的FT(全职雇员)和TE(总雇员)(每月比较01到12),并找到差异。如果该月的FT超过TE,则显示ID,Company,FT和TE,否则通过。谁能帮我这个查询。

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
th, td {
  padding: 5px;
  text-align: left;    
}
</style>
</head>
<body>

<p>FT = Full Time Employee <br>
TE = Total Employee <br>
01-12 = month
</p>

<table style="width:100%">
  <tr>
    <th>ID</th>
    <th>Company</th>
    <th>FT01</th>
    <th>FT02</th>
    <th>FT03</th>
    <th>FT04</th>
    <th>FT05</th>
    <th>FT06</th>
    <th>FT07</th>
    <th>FT08</th>
    <th>FT09</th>
    <th>FT10</th>
    <th>FT11</th>
    <th>FT12</th>
    <th>TE01</th>
    <th>TE02</th>
    <th>TE03</th>
    <th>TE04</th>
    <th>TE05</th>
   <th>TE06</th>
    <th>TE07</th>
   <th>TE08</th>
   <th>TE09</th>
   <th>TE10</th>
    <th>TE11</th>
    <th>TE12</th>
    
  </tr>
  <tr>
    <td>100</td>
    <td>A</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>3</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
    <td>4</td>
  </tr>
    <tr>
    <td>101</td>
    <td>B</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
  </tr>
      <tr>
    <td>102</td>
    <td>C</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>7</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
    <td>6</td>
  </tr>
  
</table>

</body>
</html>

enter image description here

1 个答案:

答案 0 :(得分:1)

将问题分解成一部分!

这个问题实际上是将几个“我该怎么办”分解成一个问题,这个问题几乎等于“为我编写此代码”请求。

因此,我将帮您解决第一个问题:数据结构。

您有一个非规范化的表,虽然重组数据库会有所帮助,但有时是不可能的。但是,如果您可以将数据归一化,则可以使查询变得更简单。

那么,让我们这样做吧!

如果表看起来像这样,问题会容易得多

ID
Company
MonthNum
TotalEmployees
FullTimeEmployees

...所以让我们编写一个查询,以这种格式获取数据:

select ID, Company, 1 as MonthNum, TE01 as TotalEmployees, FT01 as FullTimeEmployees from ...
UNION ALL
select ID, Company, 2 as MonthNum, TE02 as TotalEmployees, FT02 as FullTimeEmployees from ...
UNION ALL
select ID, Company, 3 as MonthNum, TE03 as TotalEmployees, FT03 as FullTimeEmployees from ...
-- etc, going up to 12.

现在,我们可以将其用作子查询来对该数据执行查询:

select WhateverColumns from
(
   -- that query from the previous section
) as normalizedData
where WhateverConditionsYouWant

有道理吗?将问题分解为具体步骤,然后开始解决步骤。第一个是以一种更易于使用的格式获取数据,而我展示了如何解决该问题。现在,您需要继续编写查询以获取所需的特定数据。祝你好运!