MySQL报告,联接和优化查询

时间:2018-06-27 17:44:19

标签: mysql report

在我的第一个MySQL报表查询中进行实验,结果还可以,但是查询似乎是错误的+我想在一个单独的表“ sale.ConfirmedSale”上列出一个案例,以便我可以按月修改它们并不要每次都触摸查询。

PS:我更喜欢“何时遇见”。

我拥有的桌子:

<style type="text/css">
	table.tableizer-table {
		font-size: 12px;
		border: 1px solid #CCC; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	.tableizer-table td {
		padding: 4px;
		margin: 3px;
		border: 1px solid #CCC;
	}
	.tableizer-table th {
		background-color: #104E8B; 
		color: #FFF;
		font-weight: bold;
	}
</style>
<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>User</th><th>date</th><th>Group</th><th>Team</th><th>SaleAmmount</th><th>ConfirmedSale</th><th>ConfirmationDate</th><th>Week</th></tr></thead><tbody>
 <tr><td>agent1</td><td>06/26/2018 13:59</td><td>groupA</td><td>Team1</td><td>200</td><td>0</td><td>&nbsp;</td><td>5</td></tr>
 <tr><td>agent1</td><td>06/20/2018 00:00</td><td>groupA</td><td>Team1</td><td>400</td><td>1</td><td>06/26/2018 00:00</td><td>4</td></tr>
 <tr><td>agent1</td><td>06/18/2018 11:34</td><td>groupA</td><td>Team1</td><td>195</td><td>1</td><td>06/18/2018 00:00</td><td>4</td></tr>
 <tr><td>agent1</td><td>06/15/2018 11:27</td><td>groupA</td><td>Team1</td><td>200</td><td>1</td><td>06/18/2018 00:00</td><td>3</td></tr>
 <tr><td>agent1</td><td>06/13/2018 15:49</td><td>groupA</td><td>Team1</td><td>200</td><td>0</td><td>&nbsp;</td><td>3</td></tr>
 <tr><td>agent1</td><td>06/12/2018 00:00</td><td>groupA</td><td>Team1</td><td>200</td><td>0</td><td>&nbsp;</td><td>3</td></tr>
 <tr><td>agent1</td><td>06/12/2018 00:00</td><td>groupA</td><td>Team1</td><td>200</td><td>1</td><td>06/13/2018 00:00</td><td>3</td></tr>
 <tr><td>agent1</td><td>05/29/2018 00:00</td><td>groupA</td><td>Team1</td><td>1700</td><td>0</td><td>&nbsp;</td><td>5</td></tr>
 <tr><td>agent1</td><td>05/23/2018 00:00</td><td>groupA</td><td>Team1</td><td>200</td><td>0</td><td>&nbsp;</td><td>4</td></tr>
 <tr><td>agent1</td><td>04/18/2018 00:00</td><td>groupA</td><td>Team1</td><td>200</td><td>0</td><td>&nbsp;</td><td>3</td></tr>
 <tr><td>agent2</td><td>04/03/2018 00:00</td><td>groupA</td><td>Team2</td><td>200</td><td>0</td><td>&nbsp;</td><td>1</td></tr>
 <tr><td>agent2</td><td>03/29/2018 00:00</td><td>groupA</td><td>Team2</td><td>400</td><td>0</td><td>&nbsp;</td><td>5</td></tr>
</tbody></table>

我使用的查询:

SELECT 
    sale.user,sale.Group,
    SUM(case when sale.week <= 5 and MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE())then 1 else 0 end) as SalesTOT,
    SUM(case when MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE()) and sale.SaleAmmount <400 then 1 else 0 end) as Sale,
    SUM(case when MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE()) and sale.SaleAmmount >399 then 1 else 0 end) as BigSale,
    sale.date,
    sale.Group,
    sale.Team,
    SUM(case when MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE())then SaleAmmount else 0 end) as SaleAmmount,
    SUM(case when MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE()) and sale.SaleAmmount >399 then ((sale.SaleAmmount * 0.1)*140) else 0 end) as Bonus1,
   SUM(case when sale.ConfirmedSale = 1 and MONTH(sale.ConfirmationDate) = MONTH(CURRENT_DATE()) AND YEAR(sale.ConfirmationDate) = YEAR(CURRENT_DATE()) then 1 else 0 end) as ConfirmedSale,
   case 
      when SUM(sale.ConfirmedSale) = 1 then 620
      when SUM(sale.ConfirmedSale) = 2 then 640
      when SUM(sale.ConfirmedSale) = 3 then 680
      when SUM(sale.ConfirmedSale) = 4 then 740
      when SUM(sale.ConfirmedSale) = 5 then 820
      when SUM(sale.ConfirmedSale) = 6 then 920
      when SUM(sale.ConfirmedSale) = 7 then 1020 else 0 
    end as AmountEaned,
    case 
       when SUM(sale.ConfirmedSale) = 0 then 620
       when SUM(sale.ConfirmedSale) = 1 then 640
       when SUM(sale.ConfirmedSale) = 2 then 680
       when SUM(sale.ConfirmedSale) = 3 then 740
       when SUM(sale.ConfirmedSale) = 4 then 820
       when SUM(sale.ConfirmedSale) = 5 then 920
       when SUM(sale.ConfirmedSale) = 6 then 1020 else 0 
    end as '+1Confirm'
FROM
    sale
WHERE 
    sale.Group = 'GroupA'
GROUP BY 
    sale.user
HAVING
    SUM(case when sale.week <= 5 and MONTH(sale.date) = MONTH(CURRENT_DATE()) AND YEAR(sale.date) = YEAR(CURRENT_DATE())then 1 else 0 end) > 0 
    OR SUM(case when sale.ConfirmedSale = 1 and MONTH(sale.ConfirmationDate) = MONTH(CURRENT_DATE()) AND YEAR(sale.ConfirmationDate) = YEAR(CURRENT_DATE()) then 1 else 0 end) > 0

0 个答案:

没有答案