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 8452989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:39:03+00:00 2026-06-10T11:39:03+00:00

I have a query that I don’t think should be that hard to make,

  • 0

I have a query that I don’t think should be that hard to make, however, I’ve spent a lot of time on it now and still can’t get it the way I want, so I hope someone here can help me.

Basically, I need to create a report that will give a value for each month, for each area. However, not all areas deliver data each month; in that case the view should return NULL for that month and area. So, the view need to look something like this:

Month      Area    Value
2012-08-01 Area1   2
2012-08-01 Area2   3
2012-09-01 Area1   3
2012-09-01 Area2   NULL

My data table looks something like this

Date       Area    Value
2012-08-01 Area1   2
2012-08-01 Area2   3
2012-09-01 Area1   3 -- Notice that Area2 is not present for September here

I have a table with all the available areas
Furthermore, I have created a table-valued function that returns all dates from a given date until now.

For example this statement

SELECT * FROM Periods_Months('2012-01-01')

would return 8 records like:

DateValue           Year    Month   YearMonth
2012-01-01 00:00:00.000 2012    1   20121
2012-02-01 00:00:00.000 2012    2   20122
2012-03-01 00:00:00.000 2012    3   20123
2012-04-01 00:00:00.000 2012    4   20124
2012-05-01 00:00:00.000 2012    5   20125
2012-06-01 00:00:00.000 2012    6   20126
2012-07-01 00:00:00.000 2012    7   20127
2012-08-01 00:00:00.000 2012    8   20128

Based on the suggestions, my query now looks like this:

WITH months AS (
    SELECT DateValue, YearMonth FROM Periods_Months('2011-01-01')
)
select m.DateValue
       ,CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,m.DateValue)+1,0)) AS Date) AS DateReported -- Get last day in month
       ,ResponseTime AS Value
       ,g.ExternalId 
from GISDB.dbo.GisObjects g
CROSS JOIN months m
LEFT OUTER JOIN
( -- SELECT data from data table, grouped by area and month
SELECT dbo.YearMonth(CloseDate) AS YearMonth
       ,MAX(CloseDate) AS LastDate
       ,GisObjectId
       ,SUM(DATEDIFF(HH,RegDate,CloseDate)) AS ResponseTime -- calculate response time between start and end data (the value we need)
FROM DataTable 
WHERE   CloseDate IS NOT NULL
AND     GisObjectId IS NOT NULL
GROUP BY GisObjectId, dbo.YearMonth(CloseDate)  -- group by area and month
) c
ON g.ObjectId = c.GisObjectId AND c.YearMonth = m.YearMonth 
WHERE g.CompanyId = 3 AND g.ObjectTypeId = 1 -- reduce the GIS objects that we compare to
ORDER BY m.DateValue, g.ObjectId 

But the result is this (Value is always NULL):

DateValue                   DateReported    Value   ExternalId
2011-01-01 00:00:00.000 31-01-2011  NULL    9994
2011-01-01 00:00:00.000 31-01-2011  NULL    9993
2011-01-01 00:00:00.000 31-01-2011  NULL    9992
2011-01-01 00:00:00.000 31-01-2011  NULL    9991
2011-01-01 00:00:00.000 31-01-2011  NULL    2339
2011-01-01 00:00:00.000 31-01-2011  NULL    2338
2011-01-01 00:00:00.000 31-01-2011  NULL    2337
2011-01-01 00:00:00.000 31-01-2011  NULL    2336
2011-01-01 00:00:00.000 31-01-2011  NULL    2335
2011-01-01 00:00:00.000 31-01-2011  NULL    2334
2011-01-01 00:00:00.000 31-01-2011  NULL    2327
2011-01-01 00:00:00.000 31-01-2011  NULL    2326
2011-01-01 00:00:00.000 31-01-2011  NULL    2325
2011-01-01 00:00:00.000 31-01-2011  NULL    2324
2011-01-01 00:00:00.000 31-01-2011  NULL    2323
2011-01-01 00:00:00.000 31-01-2011  NULL    2322

etc.

  • 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-06-10T11:39:04+00:00Added an answer on June 10, 2026 at 11:39 am

    I suppose you have a table with all your areas, which I call area_table.

    WITH month_table AS (
        SELECT dateValue FROM Periods_Months('2012-01-01')
    )
    select * from area_table 
    CROSS JOIN month_table
    LEFT OUTER JOIN myValueTable 
    ON area_table.name = myValueTable.area 
    AND myValueTable.date = left(convert(varchar(30),month_table.dateValue,120),10) 
    ORDER BY myValueTable.Month, myValueTable.area 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very simple query. I want to make sure that I don't have
I have this quite long query that should give me some information about shipments,
I have a query that should always be returning a single int. I have
I have this query that gets executed though Linq to Entities. First time the
Right now I have one query that returns a list of results and displays
i have one query that need some changes, and i don't get any clue
I'm having some trouble with MySql right now. I have an query that works
I am using in C# MYsql .I have query that works if I run
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them

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.