• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Group By

I have one table that I have turned into 2 tables.  I have assigned aliases to the tables.  So I have t1 and t2.  There is one field in the tables that I have combined in a case statement to make a comparison.  The query executes successfully, but rows are dropped periodically.  How can I list both column names in the group by?   I believe that this is the reason why I am dropping rows.  The column name is time.  So it would be t1.time and t2.time.  
0
daintysally
Asked:
daintysally
  • 2
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
If those have timestamp values they may not be exactly equal if using that to join so that may be one problem; however, this is how you would put multiple columsn in group by:

SELECT ...
FROM t1 JOIN t2 ...
GROUP BY t1.time, t2.time
0
 
jamesguCommented:
select t1.time , t2.time, count(...)
from t1, t2
where t1.id = t2.id ...
group by  t1.time = t2.time

if it doesn't work, please attach your query here
0
 
daintysallyAuthor Commented:
They do have timestamp values.  Please see a part of my case statement and the grouping that follows it in the code snippet
HEN DATEPART(DW,AL2.AU_TIME)NOT IN (1,7)
AND DATEDIFF(ms,AL2.AU_TIME,AL1.AU_TIME)> 0
AND DATEDIFF(dd,AL2.AU_TIME,AL1.AU_TIME)> 0
AND DATEDIFF(hh,AL2.AU_TIME,AL1.AU_TIME)> 0
AND DATEDIFF(mi,AL2.AU_TIME,AL1.AU_TIME)> 0
AND DATEDIFF(ss,AL2.AU_TIME,AL1.AU_TIME)> 0 THEN DATEDIFF(ms,AL2.AU_TIME,AL1.AU_TIME)
END) 
AS "DAYS_IN_STATUS"
 
 
....
 
Group by BUG.BG_BUG_ID, BUG.BG_PLANNED_CLOSING_VER, BUG.BG_STATUS, BUG.BG_SEVERITY,BUG.BG_USER_14, BUG.BG_RESPONSIBLE,BUG.BG_USER_62, BUG.BG_DETECTION_DATE,
BUG.BG_USER_18,BUG.BG_USER_07,AL2.AU_TIME,AL2.AU_USER,AP2.AP_FIELD_NAME,AP2.AP_OLD_VALUE,AP2.AP_NEW_VALUE
Order By BUG.BG_BUG_ID,BUG.BG_USER_14

Open in new window

0
 
daintysallyAuthor Commented:
The column that I am referring to, is AL1.AU_TIME AND AL2.AU_TIME
0
 
Kevin CrossChief Technology OfficerCommented:
You could just add both in the GROUP BY statement as shown, but what happens most times in a case like this is you may end up needing the formula (DATEDIFF) in the group by since that is the value that needs to be grouped in common and not the individual time values.

i.e. DATEDIFF(ms,AL2.AU_TIME,AL1.AU_TIME)

if value 2000ms, then for any two dates that are 2000ms apart they will be grouped together.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now