2015-09-14 51 views
-2

我創建了一個我的兩個SQL表的示例。第一個需要簡單的乘法來產生最終的「收入」,而第二個表格將該值直接存儲爲「獎金」。我希望能夠做到的是創建一個PHP頁面,它允許我選擇任何員工,並讓它生成一個HTML表格(就像我的片段中的第三個表格)。將兩個SQL表合併到一個排序的HTML輸出中

我不確定我是否可以在SQL查詢中以某種方式「加入」這兩個表。我可以在每個表上運行兩個單獨的查詢,兩個產生兩個單獨的數組,但我不知道如何按日期排序表。

MONTHLY DATA (sql table) 
 
<table> 
 
    <tr> 
 
    <th>Month</th> 
 
    <th>Employee</th> 
 
    <th>Net Sales</th> 
 
    <th>Com%</th> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>Tim</td> 
 
    <td>275,500</td> 
 
    <td>.05</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>Sara</td> 
 
    <td>219,200</td> 
 
    <td>.06</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Tim</td> 
 
    <td>391,500</td> 
 
    <td>.055</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Sara</td> 
 
    <td>359,800</td> 
 
    <td>.06</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Sara</td> 
 
    <td>116,100</td> 
 
    <td>.10</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Alex</td> 
 
    <td>217,100</td> 
 
    <td>.04</td> 
 
    </tr> 
 
</table> 
 
<br /> 
 
<br />BONUS DATA (sql table): 
 
<table> 
 
    <tr> 
 
    <th>Month</th> 
 
    <th>Employee</th> 
 
    <th>Bonus</th> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>Tim</td> 
 
    <td>500</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>Sara</td> 
 
    <td>800</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Tim</td> 
 
    <td>600</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Sara</td> 
 
    <td>950</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>Alex</td> 
 
    <td>450</td> 
 
    </tr> 
 
</table> 
 
<br /> 
 
<br />View Sara's Monthly Revenue (html output): 
 
<table> 
 
    <tr> 
 
    <th>Month</th> 
 
    <th>Toal Rev</th> 
 
    <th>Source</th> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>13,152</td> 
 
    <td>"from sales"</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Oct</td> 
 
    <td>800</td> 
 
    <td>"bonus"</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>33,198</td> 
 
    <td>"from sales"</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Nov</td> 
 
    <td>950</td> 
 
    <td>"bonus"</td> 
 
    </tr> 
 
    <tr> 
 
    <td>.</td> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td>48,100</td> 
 
    <td>TOTAL</td> 
 
    </tr> 
 
</table>

回答

0

月度數據(SQL表)

Month Employee Net Sales Com% 
Oct  Tim   275,500  .05 
Oct  Sara  219,200  .06 
Nov  Tim   391,500  .055 
Nov  Sara  359,800  .06 
Nov  Sara  116,100  .10 
Nov  Alex  217,100  .04 

贈品數據(SQL表):

Month Employee Bonus 
Oct  Tim   500 
Oct  Sara  800 
Nov  Tim   600 
Nov  Sara  950 
Nov  Alex  450 

你需要拉出來的數據兩個表分開,然後將它聯合在一起。像下面的SQL會將數據拉回給您,之後計算最終的總數應該是緊密的。

SELECT 
    Month, 
    [Total Rev], 
    Source 
FROM 
    (
    SELECT 
     Employee, 
     Month, 
     SUM([Net Sales] * [Com%]) AS [Total Rev], 
     'from sales' AS Source 
    FROM 
     [MONTHLY DATA] 
    GROUP BY 
     Employee, 
     Month 
    UNION 
    SELECT 
     Employee, 
     Month, 
     SUM(Bonus) AS [Total Rev], 
     'bonus' 
    FROM 
     [BONUS DATA] 
    GROUP BY 
     Employee, 
     Month 
    ) AS tbl1 
WHERE 
    Employee='Sara' 
ORDER BY 
    Month, 
    Source DESC 
相關問題