Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Some tsql

Posted on 2012-03-18
2
Medium Priority
?
279 Views
Last Modified: 2012-03-28
Hello,

I have a table called Documents that holds a number of documents.  There are couple of columns that are interesting to me.  

The first is doc_Datetime that holds the datetime that the document was added.

The second is the doc_Exported (bit) that tells me the document has been exported.

I need help with a query that will show the total number of documents recieved each day, and the number of them that have been exported.

The result set should even include days when no records have been received.

Something like:

Date                  Documents received   Documents exported.
2012-03-18       123                               80
2012-03-19       10                                 9
2012-03-20       0                                   0
2012-03-21       12                               11

Thanks!
0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 37735181
the basic query is easy:

select date, count(*), sum(exported) as exported
from
(select  convert (varchar(10), doc_datetime ,101) as Date, case when doc_exported = 1 then 1 else 0 end as exported from YOURTABLE) as tmp
group by date
order by date

Returning rows for missing dates is tricky, I'll need to get back to you about that.  Do you have a table of dates, perchance?
0
 
LVL 42

Accepted Solution

by:
dqmq earned 2000 total points
ID: 37735224
try this:

;with cte as
(select min(cast(convert (varchar(10), doc_datetime ,101) as datetime)) as date
      , max(cast(convert (varchar(10), doc_datetime ,101) as datetime)) as max
 from YOURTABLE
 union all
 select date + 1, max from cte where date < max)

select cte.date, count(tmp.date) as received, isnull(sum(exported),0) as exported
from
(select cast(convert (varchar(10), doc_datetime ,101) as datetime) as Date, case when doc_exported = 1 then 1 else 0 end as exported from YOURTABLE) as tmp
right join cte on tmp.date = cte.date
group by cte.date
order by cte.date
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question