Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4118484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:57:59+00:00 2026-05-20T22:57:59+00:00

My table CREATE TABLE `data` ( `record_id` int(20) NOT NULL auto_increment, `id_fk` int(20) NOT

  • 0

My table

CREATE TABLE `data` (
  `record_id` int(20) NOT NULL auto_increment,
  `id_fk` int(20) NOT NULL,
  `plant_id_fk` int(20) NOT NULL,
  `date` date NOT NULL,
  `jsdate` varchar(20) character set latin1 collate latin1_general_ci NOT NULL,
  `value_1` varchar(20) collate utf8_bin default NULL,
  `category_1` varchar(200) character set latin1 collate latin1_general_ci default NULL,
  PRIMARY KEY  (`record_id`),
  KEY `id_fk` (`id_fk`),
  KEY `plant_id_fk` (`plant_id_fk`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

looks like this

29  1   50  2010-05-20  2010, 4, 20     10  expenses
23  1   52  2010-12-13  2010, 11, 13    10  expenses
22  1   50  2011-01-10  2011, 0, 10     10  expenses
21  1   51  2011-02-07  2011, 1, 07     10  expenses
14  1   50  2011-03-31  2011, 2, 31     12  exercise
20  1   50  2011-04-01  2011, 3, 01     10  expenses

I am using this SQL code to run a query

select up.id_fk, p.plant_name, up.plant_id_fk, ym2, ifnull(sum(data.value_1),0) totalvalue_1
from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%b %Y') ym2 from data where data.id_fk=1
cross join (select distinct data.id_fk, data.plant_id_fk from data where data.id_fk=1) up
inner join plants p on p.plant_id = up.plant_id_fk
left join data on date_format(data.date, '%Y-%m') = dates.ym
            and up.id_fk=data.id_fk
            and up.plant_id_fk=data.plant_id_fk
            and category_1='expenses'
group by up.id_fk, up.plant_id_fk, ym2, ym
order by up.id_fk, up.plant_id_fk, date(concat(ym,'-1'))

I would like to be able to filter the results using a date range that is passed by the user, for example

$dc = date("Y-m-d", strtotime("now")); 
$df = date("Y-m-d", strtotime((date('Y')) . "-01-01"));

and in mySQL use something like

WHERE data.date BETWEEN $dc AND $df

but haven’t found where to insert this clause in the query above.

Any suggestions to get this done? Any pointers are much appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:58:00+00:00Added an answer on May 20, 2026 at 10:58 pm

    According to http://www.tizag.com/sqlTutorial/sqlgroupby.php (at the bottom of the page), you should be able to do it if you change your where clause to a having clause and put it between the order by and the group by clauses:

    select up.id_fk, p.plant_name, up.plant_id_fk, ym2, ifnull(sum(data.value_1),0) totalvalue_1
    from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%b %Y') ym2 from data where data.id_fk=1
    cross join (select distinct data.id_fk, data.plant_id_fk from data where data.id_fk=1) up
    inner join plants p on p.plant_id = up.plant_id_fk
    left join data on date_format(data.date, '%Y-%m') = dates.ym
                and up.id_fk=data.id_fk
                and up.plant_id_fk=data.plant_id_fk
                and category_1='expenses'
    group by up.id_fk, up.plant_id_fk, ym2, ym
    HAVING data.date BETWEEN $dc AND $df
    order by up.id_fk, up.plant_id_fk, date(concat(ym,'-1'))
    

    Hopefully that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my table structure is CREATE TABLE [dbo].[Emp]( [ID] [int] NOT NULL, [EmpName] [varchar](50) NOT
> CREATE TABLE student( -> student_id INT(2) NOT NULL AUTO_INCREMENT PRIMARY KEY, -> first_name
CREATE TABLE hostname_table ( id INT NOT NULL AUTO_INCREMENT, hostname CHAR(65) NOT NULL, interval_avg
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
Given the following table and data. create table prices (productKey int ,PriceType char(10) ,BeginDate
First execute these tables and data dumps :- CREATE TABLE IF NOT EXISTS `Tags`
My table definition is: CREATE TABLE Product (ProID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
I have the following data structure and data: CREATE TABLE `parent` ( `id` int(11)
I want to create a data type as a TABLE but I can't use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.