MySQL - is it possible to display empty rows? [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Displaying zero valued months with SQL
For a certain attribute, is it possible to display its rows even if its empty?
Say I have attribute date_of_car_sale. Also, let's say I want to do a monthly report.
From January to December of 2009, there's two months where the company has been away on holiday, so there's no date_of_car_sale for June and August.
Is it possible to make a report that displays every month of 2009, even if June and August has no data value?
I can make every month show and by grouping them to show individual months only. But can't seem to get June and August to display because they're empty.
PS: This only requires one column from the table Company.

In cases like this, I usually make a table with all values I want displayed and do a left join to my data set. So in your case, I would make a table called "months" with a single date column that stores the first day of the month or maybe two columns with the first and last day and do join like this:
SELECT m.month_first, COUNT(x.date_of_car_sale) as sales
FROM months m
LEFT JOIN (
SELECT date_of_car_sale
FROM sales
) x ON m.month_first <= x.date_of_car_sale
AND m.month_last >= x.date_of_car_sale
GROUP BY m.month_first
ORDER BY m.month_first

Related

how do i group customer entries using months across different years in ssrs reports in nav

I am trying to get Sum of each customer ledger entries and group the Ssrs report into months across different years. For instance, I should be able to filter with the year 011117..310318, this should give me entries of each customer grouped per month: November, December, Jan(18), Feb, Match.
You can do this by using a matrix in your report.
Add the matrix and set the row group to group by Customer (whatever makes the customer unique such as an ID).
Add column groups by month and then a parent column group by year. If you don't have separate columns for months and years in your dataset, you can use expression into the group expression such as..
=Month(Fields!myDateField.Value)
=Year(Fields!myDateField.Value)
In the data 'cell' just drag the value you want to aggregate there.
That should give you a basic working matrix.
If you need more help refer to the SSRS documentation on the matrix control.
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/create-a-matrix-report-builder-and-ssrs?view=sql-server-ver15

Display different values based on dates stored in a table in Access [duplicate]

This question already has answers here:
SQL/VBA: How to group by a fiscal year starting from a day other than 1st day of month
(2 answers)
Closed 3 years ago.
I have a set of data, orders from customers on every date.
I use seasons in my company to compare sales. The seasons are not defined by the year but from certain dates ie. Season 2019 - From: 01/05/2018 - To: 30/04/2019.
The data for the Seasons (SeasonName , From , To ) are stored for all seasons (2018, 2017 etc) in another table in Access.
I want to create a field in a query of Access that checks the date of the order and if it falls between the range defined in the Seasons table, to display the equivalent SeasonName.
In Excel I would do it using index and match but in Access I need your help about it!
Thanks a lot!
The easiest way is to use a Calendar table. You can link your dates to this table to retrieve the corresponding SeasonName. You can generate the data for this table in Excel then import it into Access. Here's an example of a Calendar table https://www.excelcampus.com/tables/calendar-table-explained/.
The ideas are the same for Access. Build your table with your required dates and then add the SeasonName info for each date.

Return SQL query in specific chunks? [duplicate]

This question already has answers here:
Interleave rows of MySQL table
(2 answers)
Closed 5 years ago.
I have a time stamped data for specific dates in my mysql database. Whenever I retrieve the data in different ways, this is what I usually get:
I'm trying to get this query to look like this
What I want is to return different/distinct rows for date1 while keeping time ordered. If you look at the desired result, you will see that date1 starts with Sep then Oct then goes back to Sep, Oct etc. I hope this makes it clear.
Note: This is just an example, the real data have four different dates in date1 column so I'm expecting every four rows to have a different entry of date1
If I understand your question correctly, you just need to specify an order by clause.
select
...
from
...
where
...
order by
date2
, time

Get sum of values based on the value of 2 other date related columns

Given the sample data in the screenshot below, would it be possible in mysql to return a sum of values from monthly_amount only where the values are before this month. I used a join to pull this data. The 5 left columns are from one table, and the rest are from another.
The issue I'm running into is, lets say its April of 2015, I can't just do a sum WHERE goal_year <= 2015 AND month_id_FK <= 4, or else I'll get only those 4 months from both years, when in that scenario, I really want all the months from 2014, plus the 4 months from 2015.
I could handle this in PHP, but I wanted to first see if there would be a way to do this in mysql?
try
WHERE Goal_Year*100+month_id_FK <= 201504
alternatively:
WHERE
GOAL_YEAR < 2015 OR
(GOAL_YEAR = 2015 and month_id_FK <= 4)
select sum(monthly_amount) from table where goaldate<(SELECT CURDATE())
this is not the actual query for your table..but if you do like this you will get the answer
you need the sum of monthly amount where the date is before current-date means today.
then you can just compare the currentdate with goal date

How to get month using date in MySQL [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how do I get month from date in mysql
I want to get month using date example 2011-04-02 so I want month april. How to get this in MySQL?
SELECT MONTHNAME(date) AS monthName for January, February...
SELECT MONTH(date) AS monthName for 1, 2...
SELECT MONTHNAME(`date`) AS month_name FROM table_name;
You can use MONTHNAME() to get the month name. If you want month number, consider to use MONTH()
You can have a much more elegant solution to this if you use a second table as a date dimension table, and join your date field to it, in order to extract more useful information. This table can contain dates, month names, financial quarters, years, days of week, weekends, etc.
It is a really tiny table, only 365(ish) rows per year of data you have... And you can easily write some code to populate this table with as much data as you require. I did mine in Excel, exported as a CSV file and then imported the data into a blank table.
It also gives lots of benefits, for example, imagine a monthly data table with the following fields (and any others you can think of!) fully populated for all the months in a given range;
Date (E.g. 2009-04-01)
Day (E.g. 1)
Day of Week (E.g. Wednesday)
Month (E.g. 4)
Year (E.g. 2009)
Financial Year (E.g. 2009/10)
Financial Quarter (E.g. 2009Q1)
Calendar Quarter (E.g. 2009Q2)
Then combining this with your own table as follows;
SELECT `DT`.`monthName`
FROM `your_table`
INNER JOIN `dateTable` as DT
ON `your_table`.`your_date_field` = `dateTable`.`theDate`
There are many other nice outputs that you can get from this data.
Hope that helps!