Select FN,LN
Group by FN from A
Gives me
Bob
Bob
Tom
Tom
Tom
Bill
Bill
Need to achive output of names not after one another if they are same
name
Bob
Tom
Bill
Bob
Tom
Bob
Bill
Order is not important as long as they dont repaeat same name for next
rowOn 21 Dec 2005 13:50:36 -0800, Matt wrote:
>Select FN,LN
>Group by FN from A
>Gives me
>Bob
>Bob
>Tom
>Tom
>Tom
>Bill
>Bill
Hi Matt,
You must have made a mistake in this post. That query can never result
in anything but an error message.
>Need to achive output of names not after one another if they are same
>name
>Bob
>Tom
>Bill
>Bob
>Tom
>Bob
>Bill
>Order is not important as long as they dont repaeat same name for next
>row
Not sure I understand the requirements, but it sounds like a task for
the front end.
If you really want to do this server-side, then please proviude better
specs. Check out www.aspfaq.com/5006.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Since you specify no ORDER BY, SQL Server may return data in any sequence.
To return data in a pseudo random order, you can include ORDER BY NEWID().
> Need to achive output of names not after one another if they are same
> name
> Order is not important as long as they dont repaeat same name for next
> row
Is random order acceptable? In order words, is it ok if 2 or more same
names are consecutive, if only by coincidence?
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Matt" <metehanIT@.Hotmail.com> wrote in message
news:1135201836.056730.13200@.o13g2000cwo.googlegroups.com...
> Select FN,LN
> Group by FN from A
> Gives me
> Bob
> Bob
> Tom
> Tom
> Tom
> Bill
> Bill
> Need to achive output of names not after one another if they are same
> name
> Bob
> Tom
> Bill
> Bob
> Tom
> Bob
> Bill
> Order is not important as long as they dont repaeat same name for next
> row
>|||Pushing those recordsets to another system, system does not allow me to
push smilar account one another.
So i cant push right after another. I need to push different name.
Since there are no other Indicator to differenciate the records
Above query was an example
any idea?|||As Hugo requested, we really need DDL, a working query and sample to data to
help you out. Presuming that FN is the differentiator, what should be done
when it is impossible to order results to prevent consecutive duplicates?
Consider the following:
Tom
Bob
Tom
Tom
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Matt" <metehanIT@.Hotmail.com> wrote in message
news:1135260352.114345.114900@.g47g2000cwa.googlegroups.com...
> Pushing those recordsets to another system, system does not allow me to
> push smilar account one another.
> So i cant push right after another. I need to push different name.
> Since there are no other Indicator to differenciate the records
> Above query was an example
> any idea?
>|||SQL is almost 300 line thats why could not posted there
This is one something like
SELECT ACCOUNT
FROM TEMP
WHERE CLS_DATE > '12/30/03'
GROUP BY (ACCOUNT)
UNION
SELECT ACCOUNT
FROM TEMP
WHERE CLS_DATE > '12/30/02'
GROUP BY (ACCOUNT)
Gives me result set of
7321234
7321234
7324567
7324567
I need result set to be
7321234
7324567
so on ,, as long as they dont repeat one after another.
Cheers|||> SQL is almost 300 line thats why could not posted there
300 lines is reasonable. Please post.
I still don't understand your problem. The query you posted will eliminate
duplicate account numbers due to the UNION as illustrated by the example
below.
CREATE TABLE TEMP
(
ACCOUNT int NOT NULL,
CLS_DATE smalldatetime NOT NULL
CONSTRAINT PK_TEMP PRIMARY KEY
(
ACCOUNT,
CLS_DATE
)
)
INSERT INTO TEMP VALUES (7321234, '20021229')
INSERT INTO TEMP VALUES (7321234, '20021230')
INSERT INTO TEMP VALUES (7321234, '20021231')
INSERT INTO TEMP VALUES (7321234, '20031229')
INSERT INTO TEMP VALUES (7321234, '20031230')
INSERT INTO TEMP VALUES (7321234, '20031231')
INSERT INTO TEMP VALUES (7324567, '20021229')
INSERT INTO TEMP VALUES (7324567, '20021230')
INSERT INTO TEMP VALUES (7324567, '20021231')
INSERT INTO TEMP VALUES (7324567, '20031229')
INSERT INTO TEMP VALUES (7324567, '20031230')
INSERT INTO TEMP VALUES (7324567, '20031231')
SELECT ACCOUNT
FROM TEMP
WHERE CLS_DATE > '20031230'
GROUP BY (ACCOUNT)
UNION
SELECT ACCOUNT
FROM TEMP
WHERE CLS_DATE > '20021230'
GROUP BY (ACCOUNT)
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Matt" <metehanIT@.Hotmail.com> wrote in message
news:1135265718.603518.119100@.g47g2000cwa.googlegroups.com...
> SQL is almost 300 line thats why could not posted there
> This is one something like
> SELECT ACCOUNT
> FROM TEMP
> WHERE CLS_DATE > '12/30/03'
> GROUP BY (ACCOUNT)
> UNION
> SELECT ACCOUNT
> FROM TEMP
> WHERE CLS_DATE > '12/30/02'
> GROUP BY (ACCOUNT)
> Gives me result set of
> 7321234
> 7321234
> 7324567
> 7324567
> I need result set to be
> 7321234
> 7324567
> so on ,, as long as they dont repeat one after another.
> Cheers
>|||Thanks Don.
Showing posts with label bill. Show all posts
Showing posts with label bill. Show all posts
Wednesday, March 21, 2012
Monday, March 12, 2012
GROUP BY, GROUP BY, and DISTINCT
I have an SQL-Server table that contains "date, name, and account number"
records like:
22-Jan-2005 Bill BA39833J
22-Jan-2005 Bill RJ3399K
22-Jan-2005 Bill KL9833LL
22-Jan-2005 Bill BA39833J
23-Jan-2005 Bill HP54599K
23-Jan-2005 Bill AA9833LL
23-Jan-2005 Bill BA90330Q
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill XX93939
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill BA8993PPQ
I need to "group by" the date, and the user... and then "count()" the total
number
of account numbers that appear for each record.
... but here's the odd part...
I need to count each account # as "1"... except when there are similar
account numbers... then those all count as "1".
(Not counting each as "each".)
So the result will look something like:
22-Jan-2005 Bill 3 (Not 4, because the 2 similar values count as
1)
23-Jan-2005 Bill 3
24-Jan-2005 Bill 2 (Not 5, because the 4 similar values count as
1)
I thought all I would have to do is to "group by" all 3 fields... "date,
user, and account
number"... but that, of course, gives me something that I do NOT want.
ThanksTry,
select
colA,
colB,
count(distinct colC)
from
t
group by
colA,
colB
order by
colA,
colB
AMB
""A_Michigan_User"" wrote:
> I have an SQL-Server table that contains "date, name, and account number"
> records like:
> 22-Jan-2005 Bill BA39833J
> 22-Jan-2005 Bill RJ3399K
> 22-Jan-2005 Bill KL9833LL
> 22-Jan-2005 Bill BA39833J
> 23-Jan-2005 Bill HP54599K
> 23-Jan-2005 Bill AA9833LL
> 23-Jan-2005 Bill BA90330Q
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill XX93939
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill BA8993PPQ
> I need to "group by" the date, and the user... and then "count()" the tota
l
> number
> of account numbers that appear for each record.
> ... but here's the odd part...
> I need to count each account # as "1"... except when there are similar
> account numbers... then those all count as "1".
> (Not counting each as "each".)
> So the result will look something like:
> 22-Jan-2005 Bill 3 (Not 4, because the 2 similar values count
as
> 1)
> 23-Jan-2005 Bill 3
> 24-Jan-2005 Bill 2 (Not 5, because the 4 similar values count
as
> 1)
> I thought all I would have to do is to "group by" all 3 fields... "date,
> user, and account
> number"... but that, of course, gives me something that I do NOT want.
> Thanks
>
>
records like:
22-Jan-2005 Bill BA39833J
22-Jan-2005 Bill RJ3399K
22-Jan-2005 Bill KL9833LL
22-Jan-2005 Bill BA39833J
23-Jan-2005 Bill HP54599K
23-Jan-2005 Bill AA9833LL
23-Jan-2005 Bill BA90330Q
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill XX93939
24-Jan-2005 Bill BA8993PPQ
24-Jan-2005 Bill BA8993PPQ
I need to "group by" the date, and the user... and then "count()" the total
number
of account numbers that appear for each record.
... but here's the odd part...
I need to count each account # as "1"... except when there are similar
account numbers... then those all count as "1".
(Not counting each as "each".)
So the result will look something like:
22-Jan-2005 Bill 3 (Not 4, because the 2 similar values count as
1)
23-Jan-2005 Bill 3
24-Jan-2005 Bill 2 (Not 5, because the 4 similar values count as
1)
I thought all I would have to do is to "group by" all 3 fields... "date,
user, and account
number"... but that, of course, gives me something that I do NOT want.
ThanksTry,
select
colA,
colB,
count(distinct colC)
from
t
group by
colA,
colB
order by
colA,
colB
AMB
""A_Michigan_User"" wrote:
> I have an SQL-Server table that contains "date, name, and account number"
> records like:
> 22-Jan-2005 Bill BA39833J
> 22-Jan-2005 Bill RJ3399K
> 22-Jan-2005 Bill KL9833LL
> 22-Jan-2005 Bill BA39833J
> 23-Jan-2005 Bill HP54599K
> 23-Jan-2005 Bill AA9833LL
> 23-Jan-2005 Bill BA90330Q
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill XX93939
> 24-Jan-2005 Bill BA8993PPQ
> 24-Jan-2005 Bill BA8993PPQ
> I need to "group by" the date, and the user... and then "count()" the tota
l
> number
> of account numbers that appear for each record.
> ... but here's the odd part...
> I need to count each account # as "1"... except when there are similar
> account numbers... then those all count as "1".
> (Not counting each as "each".)
> So the result will look something like:
> 22-Jan-2005 Bill 3 (Not 4, because the 2 similar values count
as
> 1)
> 23-Jan-2005 Bill 3
> 24-Jan-2005 Bill 2 (Not 5, because the 4 similar values count
as
> 1)
> I thought all I would have to do is to "group by" all 3 fields... "date,
> user, and account
> number"... but that, of course, gives me something that I do NOT want.
> Thanks
>
>
Labels:
ba39833j22-jan-2005,
bill,
contains,
database,
date,
distinct,
group,
like22-jan-2005,
microsoft,
mysql,
number,
oracle,
records,
rj3399k22-jan-2005,
server,
sql,
sql-server,
table
Subscribe to:
Posts (Atom)