Search code examples
selectsumdistinct

Want to use distinct in one select case


So I used select distinct to eliminate duplicate address, each address has a unique phone number. How do I select applies to only address but no phone numbers?

SELECT distinct address--,moneyinfo
FROM database (nolock)
where buildingtime between '2020-08-23' and '2020-08-24'

Is there way I can apply distinct only to address, and shows the sums of money held in each adresss for that distinct address?

If don't apply distinct to adress, it shows up like this

Address Money
12341   10
12348   623
12338   19
12343   3
12349   3634
12342   6333

I'm just giving random examples, anyway to apply distinct to address, and do a sum of all the money in total for each house?


Solution

  • You need to GROUP BY the address; then you can SUM the money values for each address:

    SELECT address, SUM(moneyinfo) AS money
    FROM database
    WHERE buildingtime BETWEEN '2020-08-23' AND '2020-08-24'
    GROUP BY address