Can someone help me with sql queries 1 How many times has ea
Can someone help me with sql queries
1. How many times has each dog been treated? Show only ID for each dog.
Solution
1. SELECT DOG_ID, COUNT(*) AS `Count` FROM TREATMENT GROUP BY DOG_ID;
Above query will group the result by DOG_ID. As a result we will have a single row for eac dog ID and cound of time the dog has been treated will be shown.
2. SELECT DOG_ID, sum(SUBQUERY.DISCOUNT_AMOUNT) FROM (Select DOG_ID, FEE*DISCOUNT_RATE as DISCOUNT_AMOUNT from TREATMENT) AS SUBQUERY GROUP BY DOG_ID;
In Above query first we calculating discount amount for each row and name it SUBQUERY. They we are taking DOG ID nad the sum of Discount amount and grupping by DOG ID
