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.
anthonyphamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 & Architect, EE Solution GuideCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
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 & Architect, EE Solution GuideCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.