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

Group By BiMonthly

We're trying to group several reports with our bimonthly payroll. Is there a way to group by DAY 1-15 and 16- month end in T-SQL?  We're using MS SQL 2008.
0
anthonypham
Asked:
anthonypham
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
MohammedUCommented:
Easy way is write two queries where clause DAY 1-15 and 16- month end and combine them using UNION ALL to get the output...

SELECT ....
FROM ...
WHERE DAY(datecolumn) <= 15
UNION ALL
SELECT ....
FROM ...
WHERE DAY(datecolumn) > 15
group by ...
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
You can do it like this:

select * from ur_table
group by case when DAY(ur_datetime_col) < 16 then 1 else 2 end

Posting your existing query might help providing correct query
0
 
anthonyphamAuthor Commented:
This is my code.  I tried adding group by case..., but it doesn't work.
SELECT ae.last_name + ', ' + ae.first_name AE, 
	ae.exclude,
	g.rep,
	YEAR(f.funded) Year, 
	MONTH(f.funded) Month, 
	DAY(f.funded) Day,
	SUM(g.loan_amt) Loan_Amount
FROM g
JOIN f ON g.file_id = f.file_id
JOIN ae ON g.rep = ae.employ_id
JOIN a ON g.file_id = a.file_id
GROUP BY ae.last_name + ', ' + ae.first_name, 
	ae.exclude,
	g.lo_rep,
	YEAR(f.funded), 
	MONTH(f.funded),
	DAY(f.funded)

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
Raja Jegan RSQL Server DBA & ArchitectCommented:
You are already grouping it by several parameters..
Can you post the sample records for the query provided above and the expected result sets so that they can be fixed out accordingly...
0
 
anthonyphamAuthor Commented:
Sample Records
AE  exclude lo_rep Year Month Day Loan_Amount
CH, JOHN 0 035 2010 2 1 417000.00
CH, JOHN 0 035 2010 2 3 359000.00
CH, JOHN 0 035 2010 2 4 200000.00
CH, JOHN 0 035 2010 2 8 187500.00
CH, JOHN 0 035 2010 2 16 558000.00
CH, JOHN 0 035 2010 2 25 464000.00
Desired Records
AE  exclude lo_rep Year Month Day Loan_Amount
CH, JOHN 0 035 2010 2 1-15 1163500.00
CH, JOHN 0 035 2010 2 16+ 1022000.00
 
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
Try this:
SELECT AE, exclude, lo_rep, Year, Month, case when Day < 16 then '1 - 15' else '> 15' end as Day, Loan_Amount
FROM (
SELECT ae.last_name + ', ' + ae.first_name AE, 
        ae.exclude,
        g.rep,
        YEAR(f.funded) Year, 
        MONTH(f.funded) Month, 
        DAY(f.funded) Day,
        SUM(g.loan_amt) Loan_Amount
FROM g
JOIN f ON g.file_id = f.file_id
JOIN ae ON g.rep = ae.employ_id
JOIN a ON g.file_id = a.file_id
GROUP BY ae.last_name + ', ' + ae.first_name, 
        ae.exclude,
        g.lo_rep,
        YEAR(f.funded), 
        MONTH(f.funded),
        DAY(f.funded)) temp
GROUP BY AE, exclude, lo_rep, Year, Month, case when Day < 16 then '1 - 15' else '> 15' end , Loan_Amount

Open in new window

0
 
SharathData EngineerCommented:
try this
SELECT   ae.last_name + ', ' + ae.first_name AE, 
         ae.exclude, 
         g.rep, 
         YEAR(f.funded)                      YEAR, 
         MONTH(f.funded)                     MONTH, 
         CASE 
           WHEN DAY(f.funded) < 16 THEN 1 
           ELSE 2 
         END DAY, 
         SUM(g.loan_amt)                     Loan_Amount 
FROM     g 
         JOIN f 
           ON g.file_id = f.file_id 
         JOIN ae 
           ON g.rep = ae.employ_id 
         JOIN a 
           ON g.file_id = a.file_id 
GROUP BY ae.last_name + ', ' + ae.first_name, 
         ae.exclude, 
         g.lo_rep, 
         YEAR(f.funded), 
         MONTH(f.funded), 
         CASE 
           WHEN DAY(f.funded) < 16 THEN 1 
           ELSE 2 
         END

Open in new window

0
 
SharathData EngineerCommented:
to be precise,
SELECT   ae.last_name + ', ' + ae.first_name AE, 
         ae.exclude, 
         g.rep, 
         YEAR(f.funded)                      YEAR, 
         MONTH(f.funded)                     MONTH, 
         CASE 
           WHEN DAY(f.funded) < 16 THEN '1-15' 
           ELSE '16+' 
         END DAY, 
         SUM(g.loan_amt)                     Loan_Amount 
FROM     g 
         JOIN f 
           ON g.file_id = f.file_id 
         JOIN ae 
           ON g.rep = ae.employ_id 
         JOIN a 
           ON g.file_id = a.file_id 
GROUP BY ae.last_name + ', ' + ae.first_name, 
         ae.exclude, 
         g.lo_rep, 
         YEAR(f.funded), 
         MONTH(f.funded), 
         CASE 
           WHEN DAY(f.funded) < 16 THEN '1-15' 
           ELSE '16+' 
         END

Open in new window

0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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