anthonypham
asked on
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
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...
Can you post the sample records for the query provided above and the expected result sets so that they can be fixed out accordingly...
ASKER
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
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
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT ....
FROM ...
WHERE DAY(datecolumn) <= 15
UNION ALL
SELECT ....
FROM ...
WHERE DAY(datecolumn) > 15
group by ...