1

I have table where date is stored as 3 columns in the table, i.e as below

`periodYear`
`periodMonth` 
`billDay`

Which is causing me problems, when i want to generate reports based on the DATE which is a combination of above three. for example

SELECT SUM(amount) as Total,  
FROM invoice 
WHERE `periodYear` = 2014, 
GROUP BY `billDay`,`periodMonth`,`periodYear`

Can somebody help me to explain how to solve this problem ?

For example I want to list all the totals last year on daily base,

If it's a date column, I could have just group by date, but in this case I don't know how to do that, because if you group by billday,..,., then it going group based on the day not DATE.. you see what I mean ?

4
  • 1
    what problems are you encountering? I believe those extra commas are typos, right? Commented Sep 24, 2014 at 4:26
  • What's the type of those columns? Numeric or character-based? Commented Sep 24, 2014 at 4:27
  • for example i want to list all the totals for the bill day last year on daily base, if its a date coloum i could have just group by date, but in this case i dont know how to do that , becos if you group by billday,..,., then it going group based on the day not DATE.. you see what i mean ? Commented Sep 24, 2014 at 4:28
  • smallint(4) all are integer Commented Sep 24, 2014 at 4:29

3 Answers 3

3

Try this,

 SELECT SUM(amount) as Total  FROM invoice WHERE `periodYear`=2014 GROUP BY CONCAT(periodYear, '-', periodMonth, '-', billDay);
Sign up to request clarification or add additional context in comments.

Comments

2

You could just concatenate the values together and then group on that:

SELECT SUM(amount) as Total FROM invoice
WHERE periodYear=2014
GROUP BY CONCAT(billDay, '-', periodMonth, '-', periodYear)

Or if you would want to convert to and actual date format for easier sorting afterwards:

SELECT SUM(amount) as Total FROM invoice
WHERE periodYear=2014
GROUP BY CONCAT(periodYear,
                '-',
                LPAD(periodMonth, 2, '00'),
                '-',
                LPAD(billDay, 2, '00')
)

Comments

0

I think what you want is just the opposite of what you've tried.

SELECT SUM(amount) as 'Total'
FROM `invoice`
WHERE `periodYear` = 2014
GROUP BY `periodYear`, `periodMonth`, `billDay`

This will group first by year, then by month, then by day. Biggest to smallest.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.