1. 程式人生 > >SQL 中 Count 和Distinct的使用

SQL 中 Count 和Distinct的使用

先來複習一下左連線:```
select * from tb_driverReportDetails
left join tb_driverReportInput on tb_driverReportInput.id=tb_driverReportDetails.reportid
left join tb_stationMileage on tb_stationMileage.id=mileageStationId

它的行數和select * from  tb_driverReportDetails  是一樣的列數更多。
上述表中,tb_driverReportInput的一條對應tb_driverReportDetails的多條。
客戶要求 統計出某個時間段內 乘務員報單數,就是tb_driverReportInput.id的個數。

首先要gorup by driverGh.
如果直接Count (tb_driverReportInput.id) 查出來的比較多,這是不對的, 改為count( distinct tb_driverReportInput.id)就可以了  。

完整的語句如下:


 ` select 
   (select gh from tb_driver where gh=driverGh) as  gh, 
   (select drivername from tb_driver where gh=driverGh) as  name, 
   (select department from tb_driver where gh=driverGh) as  department, 
   (select post from tb_driver where gh=driverGh) as  post, 
   '司機' as  workpost, 
    CAST(sum(actualOil) as decimal(18,2) ) as actualOil,
    CAST(sum(tb_driverReportDetails.comprehensiveOil) as decimal(18,2) ) as comprehensiveOil,
    CAST(sum(actualOil)-sum(tb_driverReportDetails.comprehensiveOil) as decimal(18,2) ) as saveOil,
	 0.5* sum(dzCount+dxCount+dcCount)+sum(score) as driverValue,
	  count( distinct tb_driverReportInput.id)  
    from  tb_driverReportDetails left join tb_driverReportInput on tb_driverReportInput.id=tb_driverReportDetails.reportid
	left join tb_stationMileage on tb_stationMileage.id=mileageStationId 
   where department='田集' and fromDateTime>@startdate and fromDateTime<@enddate  group by driverGh`


另外有個不重複的約束
首先,建立一張表table_a
CREATE TABLE [dbo].[table_a](
 [aID] [int] NULL,
 [aNum] [int] NULL
) ON [PRIMARY];

這個是沒有unique約束的,若我們想給aID欄位增加unique約束,則可有下列語句:
ALTER TABLE table_a ADD unique(aID);


-----------------修改完善,另一種寫法-----------------
上面的太複雜,效率也不高,另外一種寫法,是包一層,邏輯更清晰。

select
(select gh from tb_driver where gh=assistantDriverGh) as gh, (select drivername from tb_driver where gh=assistantDriverGh) as name,
(select department from tb_driver where gh=assistantDriverGh) as department,
(select post from tb_driver where gh=assistantDriverGh) as post,
‘副司機’ as workpost,
(select sum(driverOil) from tb_driverReportInput nb where nb.assistantDriverGh=b.assistantDriverGh)as SumDriverOil,

(select sum(driverOil)- SumComprehensiveOil from tb_driverReportInput nb where nb.assistantDriverGh=b.assistantDriverGh)as saveOil,

 * from 
(
 select assistantDriverGh, 0.5* sum(dzCount+dxCount+dcCount)+sum(score) as driverValue,
  count( distinct tb_driverReportInput.id) as  driverReportCount,
  sum(tb_driverReportDetails.comprehensiveOil)as SumComprehensiveOil  
from  tb_driverReportDetails left join tb_driverReportInput on tb_driverReportInput.id=tb_driverReportDetails.reportid
left join tb_stationMileage on tb_stationMileage.id=mileageStationId  

where (select department from tb_driver where tb_driver.gh=assistantDriverGh and post=‘副司機’)=‘謝橋’ group by assistantDriverGh
) b


分頁查詢語句:查詢31-40條的資料

    select   top 10  *  from [Table] where Id not in (
    select   top 30  id from [Table]
    )