Hello,
How do I go about selecting some things once and others more than once?
This is what I have returning right now:
Pet Name Food
Dog Buster Kibbles and Bits
Dog Buster IAMS
Dog Buddy Kibbles and Bits
How can I return this?
Pet Name Food
Dog Buster Kibbles and Bits
Dog IAMS
Dog Buddy Kibbles and Bits
I just want to the dog's name to appear once. If it appears more than once have all subsequent appearences be NULL.
How would I do this?
You can do this in a query but why are you generating a report using SQL? It is not meant for that. The query will look complex depending on the version of SQL Server and perform slower than a simple SELECT statement. You will get better performance if you use the client to do the reporting functionality. Any standard reporting tool will have options to suppress repeating values like you want. So what are you doing with this data? Is there an application that is issueing the query?|||I don't have a report returning...just data.
I just want to select the dog's name once if it is the same dog....
It is filling a datagrid.
|||The following query will work on SQL Server 2005,
But no guarantee on performance...
Code Snippet
Create Table #data (
[Pet] Varchar(100) ,
[Name] Varchar(100) ,
[Food] Varchar(100)
);
Insert Into #data Values('Dog','Buster','KibblesandBits');
Insert Into #data Values('Dog','Buster','IAMS');
Insert Into #data Values('Dog','Buddy','KibblesandBits');
;With Ordered
as
(
Select *, Row_number() Over(order By Pet) RowId from #data
)
, Grouped
as
(
Select * , Row_Number() Over(Partition By Pet,Name Order By RowID) as GroupId from Ordered
)
Select Pet,Case When GroupId=1 Then Name Else '' End,Food from Grouped Order By RowId
No comments:
Post a Comment