Forum Discussion
SezMe
Jun 23, 2024Copper Contributor
How to write SQL getting data from multiple rows
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[People]') AND type in (N'U'))
DROP TABLE [People]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [People](
...
- Jun 28, 2024I Think You could use this Query
SELECT STRING_AGG(Name,' And ') Names, Married Date FROM People GROUP BY Married
Regards
Arshad
olafhelper
Jun 24, 2024Bronze Contributor
That's really to less on information.
Please post table design as DDL, some sample data as DML statement and the expected result.
Please post table design as DDL, some sample data as DML statement and the expected result.
SezMe
Jun 27, 2024Copper Contributor
olafhelperI have rewritten the OP.
- olafhelperJun 28, 2024Bronze Contributor
SezMe , so now you want to "guess" just by the marriage date the couples? Do you know, how many people marry per day? And which one first and second in the result?
DECLARE @People AS TABLE ( PersonID int IDENTITY(1,1) NOT NULL, Name varchar(16) NOT NULL, Married date NULL); INSERT INTO @People (Name, Married) VALUES ('John Doe', '2000-02-20'), ('Sam Smith', '2000-03-03'), ('Jack Beanstalk', NULL), ('Mary Doe', '2000-02-20'), ('Harry Potter', NULL), ('Patty Smith', '2000-03-03'); SELECT * FROM @People AS P1 INNER JOIN @People AS P2 ON P1.Married = P2.Married WHERE P1.Name <> P2.Name
- SezMeJun 30, 2024Copper Contributor
olafhelper No guessing required. A more precise question is: Do I know how many people marry per day who will ever be in my database? You will be happy to know I do know how to answer that question: 2.
If the order had mattered, I would have said so.