简体   繁体   中英

SELECT depending on previous columns

I want to do something, what with pseudo-code would be like

for each uniqeValue in column1
   for each uniqeValue in column2
       SELECT uniqueValueColumn1, uniqueValueColumn2, numberOfRepetitions

Is there a mechanism that would let me do this?

Edit:

As requested I provide more specific input and wanted output.

Name |BirthDate
-----------------
Chris|1999-01-01
Chris|1999-01-01
Chris|1999-12-12
Tom  |1999-12-12

Output:

Name |BirthDate |Repetitions
----------------------------
Chris|1999-01-01|          2
Chris|1999-12-12|          1
Tom  |1999-12-12|          1

I thought about GROUP BY, but didn't succeed in applying it.
My try for now is sth. like

SELECT DISTINCT Name,
    (SELECT DISTINCT BirthDate),
    (SELECT COUNT SELECT DISTINCT Name,
        (SELECT DISTINCT BirthDate))
    FROM Table

Of course it doesn't work because it doesn't have right to. My intention is to get know how to correctly implement this COUNT or how to replace it.

Sounds like you are looking for a query to COUNT the number of records where [Name] and [Birthdate] are the same. This should work for that purpose:

SELECT [Name], [BirthDate], COUNT(*) AS Repetitions
FROM Table
GROUP BY [Name], [BirthDate]

I think you want this:

select name, birthdate, count(*)
from person
group by name, birthdate

Check and test at: http://sqlfiddle.com/#!9/96898/1

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