简体   繁体   中英

How to aggregate counts across two SQL tables?

I have two tables like this

Table 1: MedicineType  
MedicineTypeID MedicineTypeName
TE001         | HIV
TE002         | AIDS

Table 2: MsMedicine
MedicineID MedicineTypeID MedicineName
ME001     | TE001        | HIVgood 
ME002     | TE001        | HIVsmart
ME003     | TE002        | AIDSawesome

How can I show MedicineTypeID, MedicineTypeName, and MedicineCount (derived from the number of MedicineID on each MedicineType) example:

MedicineTypeID MedicineTypeName MedicineCount
        TE001 | HIV            | 2
        TE002 | AIDS           | 1
Select mt.MedicineTypeID, mt.MedicineTypeName, Count(m.MedicineID) as MedicineCount
From MedicineType
Inner Join MSMedicine m On mt.MedicineTypeID = m.MedicineTypeID
Group By mt.MedicineTypeID, mt.MedicineTypeName

从MedicineType中选择MedicineTypeID,MedicineTypeName,count(*)作为MedicineCount在MedicineType.MedicineID = MsMedicine.MedicineID组上通过MedicineTypeID,MedicineTypeName加入MsMedicine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM