Showing posts with label char. Show all posts
Showing posts with label char. Show all posts

Friday, March 9, 2012

GROUP BY problems...

Take a look at the following SELECT command:

Code Snippet

SELECT aagfakt.kndnr1,
aagfaktpos.artnr1,
CONVERT (char, aagfaktpos.faktdatum, 104) AS [Datum],
SUM (CASE
WHEN aagfaktpos.storno = 1
THEN aagfaktpos.bestellmenge * -1
ELSE aagfaktpos.bestellmenge
END) AS absatz
FROM aagfaktpos
INNER JOIN aagfakt
ON aagfaktpos.lfdfaktnr = aagfakt.lfdfaktnr
GROUP BY aagfaktpos.artnr1,
kndnr1,
aagfaktpos.faktdatum
ORDER BY kndnr1 ASC;

The data doesn't group, I don't know why... any help would be appreciated!

My suggestion:

Code Snippet

SELECT aagfakt.kndnr1,
aagfaktpos.artnr1,
CONVERT (char, aagfaktpos.faktdatum, 104) AS [Datum],
SUM (CASE
WHEN aagfaktpos.storno = 1
THEN aagfaktpos.bestellmenge * -1
ELSE aagfaktpos.bestellmenge
END) AS absatz
FROM aagfaktpos
INNER JOIN aagfakt
ON aagfaktpos.lfdfaktnr = aagfakt.lfdfaktnr
GROUP BY aagfaktpos.artnr1,
aagfakt.kndnr1,
CONVERT (char, aagfaktpos.faktdatum, 104)
ORDER BY aagfakt.kndnr1 ASC;

Wednesday, March 7, 2012

GROUP BY DateTime

Dear All,
Did a research, seems like the only solution in SQL Server is:
SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
CONVERT(CHAR(8), RecTime , 112);
The RecTime is a DateTime field, and I wish to sort by the Date
Component.
Any simpler solution?<ckkwan@.my-deja.com> wrote in message
news:58c5c9b0-1fd1-43f5-aac3-360c83d7b2a7@.w1g2000prd.googlegroups.com...
> Dear All,
> Did a research, seems like the only solution in SQL Server is:
> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
> CONVERT(CHAR(8), RecTime , 112);
> The RecTime is a DateTime field, and I wish to sort by the Date
> Component.
> Any simpler solution?
In SQL2000/2005, not really. But really, it's not that complex.
There's variations on this, but they're all about the same idea.
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||ckkwan@.my-deja.com wrote:
> Dear All,
> Did a research, seems like the only solution in SQL Server is:
> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
> CONVERT(CHAR(8), RecTime , 112);
> The RecTime is a DateTime field, and I wish to sort by the Date
> Component.
> Any simpler solution?
Definitely not the only solution!
Are there any simpler solutions? No. But there is a faster solution.
AFAIK this is the best performing solution:
SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0)
FROM RecTbl
GROUP BY DATEDIFF(day, 0, us_Hitdate)
--
Gert-Jan
SQL Server MVP|||Gert
You meant
GROUP BY DATEADD(day, DATEDIFF(day,0,EstimateDate) ,0)
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:480657AB.F6B90AE9@.toomuchspamalready.nl...
> ckkwan@.my-deja.com wrote:
>> Dear All,
>> Did a research, seems like the only solution in SQL Server is:
>> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
>> CONVERT(CHAR(8), RecTime , 112);
>> The RecTime is a DateTime field, and I wish to sort by the Date
>> Component.
>> Any simpler solution?
> Definitely not the only solution!
> Are there any simpler solutions? No. But there is a faster solution.
> AFAIK this is the best performing solution:
> SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0)
> FROM RecTbl
> GROUP BY DATEDIFF(day, 0, us_Hitdate)
> --
> Gert-Jan
> SQL Server MVP|||Gert is correct, no need to have the DATEADD in the GROUP BY as it does not
change the grouping. Try this:
SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0), COUNT(*)
FROM (SELECT CURRENT_TIMESTAMP
UNION ALL
SELECT DATEADD(hh, 1, CURRENT_TIMESTAMP)) AS T(us_HitDate)
GROUP BY DATEDIFF(day, 0, us_Hitdate);
Plamen Ratchev
http://www.SQLStudio.com|||Nope :-)
--
Gert-Jan
Uri Dimant wrote:
> Gert
> You meant
> GROUP BY DATEADD(day, DATEDIFF(day,0,EstimateDate) ,0)
> "Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
> news:480657AB.F6B90AE9@.toomuchspamalready.nl...
> > ckkwan@.my-deja.com wrote:
> >>
> >> Dear All,
> >>
> >> Did a research, seems like the only solution in SQL Server is:
> >>
> >> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
> >> CONVERT(CHAR(8), RecTime , 112);
> >>
> >> The RecTime is a DateTime field, and I wish to sort by the Date
> >> Component.
> >>
> >> Any simpler solution?
> >
> > Definitely not the only solution!
> >
> > Are there any simpler solutions? No. But there is a faster solution.
> > AFAIK this is the best performing solution:
> >
> > SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0)
> > FROM RecTbl
> > GROUP BY DATEDIFF(day, 0, us_Hitdate)
> >
> > --
> > Gert-Jan
> > SQL Server MVP|||Plamen
Have you run your script on SS2000?
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:9814BC3D-0B4E-401C-B18D-868581495690@.microsoft.com...
> Gert is correct, no need to have the DATEADD in the GROUP BY as it does
> not change the grouping. Try this:
> SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0), COUNT(*)
> FROM (SELECT CURRENT_TIMESTAMP
> UNION ALL
> SELECT DATEADD(hh, 1, CURRENT_TIMESTAMP)) AS T(us_HitDate)
> GROUP BY DATEDIFF(day, 0, us_Hitdate);
> Plamen Ratchev
> http://www.SQLStudio.com|||Yes, that was a limitation in SQL 2000... But I would assume by today's
standards SQL 2005 is the base to measure. :)
Plamen Ratchev
http://www.SQLStudio.com|||I'd prefer ask people what version they are using. There are still lots of
businesses use SQL Server 2000/
"Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
news:6D6390C6-57B3-470F-80FC-8E4DA9F4A3C5@.microsoft.com...
> Yes, that was a limitation in SQL 2000... But I would assume by today's
> standards SQL 2005 is the base to measure. :)
> Plamen Ratchev
> http://www.SQLStudio.com|||Yes, SQL Server 2000 or earlier will not accept the solution I posted.
In that case, it is easier to keep the Selection List items in sync with
the GROUP BY items.
However, if you really wanted to, you could achieve the same thing on
these version by writing:
SELECT DATEADD(day,MAX( DATEDIFF(day,0,my_date_column) ),0)
FROM ...
GROUP BY DATEDIFF(day, 0, my_date_column)
--
Gert-Jan
Uri Dimant wrote:
> I'd prefer ask people what version they are using. There are still lots of
> businesses use SQL Server 2000/
> "Plamen Ratchev" <Plamen@.SQLStudio.com> wrote in message
> news:6D6390C6-57B3-470F-80FC-8E4DA9F4A3C5@.microsoft.com...
> > Yes, that was a limitation in SQL 2000... But I would assume by today's
> > standards SQL 2005 is the base to measure. :)
> >
> > Plamen Ratchev
> > http://www.SQLStudio.com

GROUP BY DateTime

Dear All,
Did a research, seems like the only solution in SQL Server is:
SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
CONVERT(CHAR(8), RecTime , 112);
The RecTime is a DateTime field, and I wish to sort by the Date
Component.
Any simpler solution?
<ckkwan@.my-deja.com> wrote in message
news:58c5c9b0-1fd1-43f5-aac3-360c83d7b2a7@.w1g2000prd.googlegroups.com...
> Dear All,
> Did a research, seems like the only solution in SQL Server is:
> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
> CONVERT(CHAR(8), RecTime , 112);
> The RecTime is a DateTime field, and I wish to sort by the Date
> Component.
> Any simpler solution?
In SQL2000/2005, not really. But really, it's not that complex.
There's variations on this, but they're all about the same idea.
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
|||ckkwan@.my-deja.com wrote:
> Dear All,
> Did a research, seems like the only solution in SQL Server is:
> SELECT CONVERT(CHAR(8), RecTime , 112) FROM RecTbl GROUP BY
> CONVERT(CHAR(8), RecTime , 112);
> The RecTime is a DateTime field, and I wish to sort by the Date
> Component.
> Any simpler solution?
Definitely not the only solution!
Are there any simpler solutions? No. But there is a faster solution.
AFAIK this is the best performing solution:
SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0)
FROM RecTbl
GROUP BY DATEDIFF(day, 0, us_Hitdate)
Gert-Jan
SQL Server MVP
|||Gert
You meant
GROUP BY DATEADD(day, DATEDIFF(day,0,EstimateDate) ,0)
"Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
news:480657AB.F6B90AE9@.toomuchspamalready.nl...
> ckkwan@.my-deja.com wrote:
> Definitely not the only solution!
> Are there any simpler solutions? No. But there is a faster solution.
> AFAIK this is the best performing solution:
> SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0)
> FROM RecTbl
> GROUP BY DATEDIFF(day, 0, us_Hitdate)
> --
> Gert-Jan
> SQL Server MVP
|||Gert is correct, no need to have the DATEADD in the GROUP BY as it does not
change the grouping. Try this:
SELECT DATEADD(day, DATEDIFF(day,0,us_Hitdate) ,0), COUNT(*)
FROM (SELECT CURRENT_TIMESTAMP
UNION ALL
SELECT DATEADD(hh, 1, CURRENT_TIMESTAMP)) AS T(us_HitDate)
GROUP BY DATEDIFF(day, 0, us_Hitdate);
Plamen Ratchev
http://www.SQLStudio.com
|||Nope :-)
Gert-Jan
Uri Dimant wrote:[vbcol=seagreen]
> Gert
> You meant
> GROUP BY DATEADD(day, DATEDIFF(day,0,EstimateDate) ,0)
> "Gert-Jan Strik" <sorry@.toomuchspamalready.nl> wrote in message
> news:480657AB.F6B90AE9@.toomuchspamalready.nl...

Friday, February 24, 2012

Group by

create table my_table (
col1 char10,
col2 char10,
col3 char 10)
select col1, count(*)
from my_table
group by col1
--Now, from this group by results, I want to eliminate all the records where
col2 is Null. Can I accomplished this in the same group by query above?
Remember, I want to eliminate the Nulls after the group by operation been
done, NOT before.
Thank you.
SandraCan you show some sample data and sample output you're looking for? Your
description makes no sense, given the query. The query counts the number of
rows for each distinct value of Col1 -- you're not doing anything with Col2
there, so how can you eliminate rows based on it, after getting those
counts?
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Sandra" <Sandra@.discussions.microsoft.com> wrote in message
news:41A9E56B-40D9-4505-9AF0-9852B0E9718B@.microsoft.com...
> create table my_table (
> col1 char10,
> col2 char10,
> col3 char 10)
> select col1, count(*)
> from my_table
> group by col1
> --Now, from this group by results, I want to eliminate all the records
> where
> col2 is Null. Can I accomplished this in the same group by query above?
> Remember, I want to eliminate the Nulls after the group by operation been
> done, NOT before.
> Thank you.
> Sandra|||You can compare count(*) to count(col2).
You must modify below query.
select col1, count(col2)
from my_table
group by col1
HAVING Count(Col2) <> 0
"Sandra"?? ??? ??:

> create table my_table (
> col1 char10,
> col2 char10,
> col3 char 10)
> select col1, count(*)
> from my_table
> group by col1
> --Now, from this group by results, I want to eliminate all the records whe
re
> col2 is Null. Can I accomplished this in the same group by query above?
> Remember, I want to eliminate the Nulls after the group by operation been
> done, NOT before.
> Thank you.
> Sandra|||There is no col2 in the results'Anyways I think this was what you wanted.
Let me know.
select col1 ,count(*)
from my_table
group by col1
having count(*) = sum(case when col2 is null then 0 else 1 end)|||If I understand correctly, you want to get a count grouped by col1 where at
least one row has a value in Col2?
If none of the rows for a given Col1 have a value in Col2, then you don't
want them returned?
select col1, count(*), count(Col2)
from my_table
group by col1
having count(Col2) > 0
"Sandra" <Sandra@.discussions.microsoft.com> wrote in message
news:41A9E56B-40D9-4505-9AF0-9852B0E9718B@.microsoft.com...
> create table my_table (
> col1 char10,
> col2 char10,
> col3 char 10)
> select col1, count(*)
> from my_table
> group by col1
> --Now, from this group by results, I want to eliminate all the records
where
> col2 is Null. Can I accomplished this in the same group by query above?
> Remember, I want to eliminate the Nulls after the group by operation been
> done, NOT before.
> Thank you.
> Sandra

Group By

create table my_table (
col1 char10,
col2 char10,
col3 char 10)
select col1, count(*)
from my_table
group by col1
--Now, from this group by results, I want to eliminate all the records where
col2 is Null. Can I accomplished this in the same group by query above?
Remember, I want to eliminate the Nulls after the group by operation been
done, NOT before.
Thank you.
SandraSandra,
the request makes no sence to me. Pls provide test data and expected
results|||Please post your question only once.