Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Cross tab based on DateDIFF

 
   Database Forums (Home) -> MSEQ RSS
Next:  Restore a database to another server  
Author Message
Geoff

External


Since: Jun 06, 2006
Posts: 5



(Msg. 1) Posted: Thu Nov 15, 2007 10:44 am
Post subject: Cross tab based on DateDIFF
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I'm trying trying to build a crosstab query using a Case statement to count
the number of records in one column with the DateDiff >=0 and the other
column to be a count of the Records with the DateDiff <0. I can do this
easlily in MS Access with the IIF Function in a crosstab query but I can't
get the Sql Statement to work in an MS Sql 2000 View

 >> Stay informed about: Cross tab based on DateDIFF 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 2) Posted: Thu Nov 15, 2007 2:02 pm
Post subject: Re: Cross tab based on DateDIFF [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Geoff,

Is this what you are after?

SELECT
SUM (CASE
WHEN Col1 >= 0 THEN 1
ELSE 0
END) AS NonNegative,
SUM (CASE
WHEN Col1 >= 0 THEN 0
ELSE 1
END) AS Negative
FROM YourTable

--RLF

"Geoff" wrote in message

> I'm trying trying to build a crosstab query using a Case statement to
> count
> the number of records in one column with the DateDiff >=0 and the other
> column to be a count of the Records with the DateDiff <0. I can do this
> easlily in MS Access with the IIF Function in a crosstab query but I can't
> get the Sql Statement to work in an MS Sql 2000 View

 >> Stay informed about: Cross tab based on DateDIFF 
Back to top
Login to vote
Geoff

External


Since: Jun 06, 2006
Posts: 5



(Msg. 3) Posted: Thu Nov 15, 2007 2:02 pm
Post subject: Re: Cross tab based on DateDIFF [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

How do I put in the condition of wether the DateDiff is >= 0. I was trying
someting similiar to this view below but replacing the Count(CASE ACTCAT WHEN
N'X4' Then ACTCAT END with something to make one column count the number of
records where DATEDIFF(DD, dbo.JobLogTbl.JSTime, dbo.ContActivTbl.AddDate)
AS DATEDIFF >=0 and another column show the count if DATEDIFF(DD,
dbo.JobLogTbl.JSTime, dbo.ContActivTbl.AddDate) AS DATEDIFF < 0 in a view




SELECT TOP 100 PERCENT DATENAME(MM, dbo.JobLogTbl.JSTime) AS Month,
MONTH(dbo.JobLogTbl.JSTime) AS MoNum,
COUNT(CASE ACTCAT WHEN N'X4' THEN ACTCAT END) AS X4,
COUNT(CASE ACTCAT WHEN N'AG' THEN ACTCAT END) AS AG
FROM dbo.ContActivTbl INNER JOIN
dbo.JobLogTbl ON dbo.ContActivTbl.JobNo =
dbo.JobLogTbl.JobNo
WHERE (dbo.ContActivTbl.ActCat = 'X4') OR
(dbo.ContActivTbl.ActCat = 'AG') AND
(dbo.JobLogTbl.JSTime >= 01 / 01 / 07)
GROUP BY DATENAME(MM, dbo.JobLogTbl.JSTime), MONTH(dbo.JobLogTbl.JSTime)
ORDER BY MONTH(dbo.JobLogTbl.JSTime)

I can do this in MS Access with

TRANSFORM Count(ContActivTbl.JobNo) AS CountOfJobNo
SELECT Format([JSTime],"mmmm") AS [Month], Count(ContActivTbl.JobNo) AS
[Total Shipments], JobLogTbl.DLocNo
FROM ContActivTbl RIGHT JOIN JobLogTbl ON ContActivTbl.JobNo = JobLogTbl.JobNo
WHERE (((ContActivTbl.ActCat)="X4") AND ((JobLogTbl.JobStart)>#1/1/2007#)
AND ((JobLogTbl.JSTime) Is Not Null) AND ((JobLogTbl.SoType)="1") AND
((JobLogTbl.ShipStatus)<>"Cancelled"))
GROUP BY Month([JSTime]), Format([JSTime],"mmmm"), ContActivTbl.CoNo,
JobLogTbl.DLocNo, JobLogTbl.SoType, JobLogTbl.ShipStatus
ORDER BY Month([JSTime])
PIVOT IIf([JobLogTbl].JSTime>=[ContActivTbl].AddDate,"Before Arrival","After
Arrival");

"Russell Fields" wrote:

> Geoff,
>
> Is this what you are after?
>
> SELECT
> SUM (CASE
> WHEN Col1 >= 0 THEN 1
> ELSE 0
> END) AS NonNegative,
> SUM (CASE
> WHEN Col1 >= 0 THEN 0
> ELSE 1
> END) AS Negative
> FROM YourTable
>
> --RLF
>
> "Geoff" wrote in message
>
> > I'm trying trying to build a crosstab query using a Case statement to
> > count
> > the number of records in one column with the DateDiff >=0 and the other
> > column to be a count of the Records with the DateDiff <0. I can do this
> > easlily in MS Access with the IIF Function in a crosstab query but I can't
> > get the Sql Statement to work in an MS Sql 2000 View
>
>
>
 >> Stay informed about: Cross tab based on DateDIFF 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 4) Posted: Thu Nov 15, 2007 4:28 pm
Post subject: Re: Cross tab based on DateDIFF [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Geoff,

If I am tracking you correctly then you would want something like:

SELECT
DATENAME(MM, dbo.JobLogTbl.JSTime) AS Month,
MONTH(dbo.JobLogTbl.JSTime) AS MoNum,
SUM (CASE
WHEN DATEDIFF(DAY,dbo.JobLogTbl.JSTime, GETDATE())>= 0 THEN 1
ELSE 0
END) AS NonNegative,
SUM (CASE
WHEN DATEDIFF(DAY,dbo.JobLogTbl.JSTime, GETDATE()) >= 0 THEN 0
ELSE 1
END) AS Negative
FROM YourTable
GROUP BY DATENAME(MM, dbo.JobLogTbl.JSTime),
MONTH(dbo.JobLogTbl.JSTime)
ORDER BY MONTH(dbo.JobLogTbl.JSTime)

I have not put all columns in, but I hope that this helps.

RLF


"Geoff" wrote in message

> How do I put in the condition of wether the DateDiff is >= 0. I was trying
> someting similiar to this view below but replacing the Count(CASE ACTCAT
> WHEN
> N'X4' Then ACTCAT END with something to make one column count the number
> of
> records where DATEDIFF(DD, dbo.JobLogTbl.JSTime,
> dbo.ContActivTbl.AddDate)
> AS DATEDIFF >=0 and another column show the count if DATEDIFF(DD,
> dbo.JobLogTbl.JSTime, dbo.ContActivTbl.AddDate) AS DATEDIFF < 0 in a view
>
>
>
>
> SELECT TOP 100 PERCENT DATENAME(MM, dbo.JobLogTbl.JSTime) AS Month,
> MONTH(dbo.JobLogTbl.JSTime) AS MoNum,
> COUNT(CASE ACTCAT WHEN N'X4' THEN ACTCAT END) AS X4,
> COUNT(CASE ACTCAT WHEN N'AG' THEN ACTCAT END) AS AG
> FROM dbo.ContActivTbl INNER JOIN
> dbo.JobLogTbl ON dbo.ContActivTbl.JobNo =
> dbo.JobLogTbl.JobNo
> WHERE (dbo.ContActivTbl.ActCat = 'X4') OR
> (dbo.ContActivTbl.ActCat = 'AG') AND
> (dbo.JobLogTbl.JSTime >= 01 / 01 / 07)
> GROUP BY DATENAME(MM, dbo.JobLogTbl.JSTime), MONTH(dbo.JobLogTbl.JSTime)
> ORDER BY MONTH(dbo.JobLogTbl.JSTime)
>
> I can do this in MS Access with
>
> TRANSFORM Count(ContActivTbl.JobNo) AS CountOfJobNo
> SELECT Format([JSTime],"mmmm") AS [Month], Count(ContActivTbl.JobNo) AS
> [Total Shipments], JobLogTbl.DLocNo
> FROM ContActivTbl RIGHT JOIN JobLogTbl ON ContActivTbl.JobNo =
> JobLogTbl.JobNo
> WHERE (((ContActivTbl.ActCat)="X4") AND ((JobLogTbl.JobStart)>#1/1/2007#)
> AND ((JobLogTbl.JSTime) Is Not Null) AND ((JobLogTbl.SoType)="1") AND
> ((JobLogTbl.ShipStatus)<>"Cancelled"))
> GROUP BY Month([JSTime]), Format([JSTime],"mmmm"), ContActivTbl.CoNo,
> JobLogTbl.DLocNo, JobLogTbl.SoType, JobLogTbl.ShipStatus
> ORDER BY Month([JSTime])
> PIVOT IIf([JobLogTbl].JSTime>=[ContActivTbl].AddDate,"Before
> Arrival","After
> Arrival");
>
> "Russell Fields" wrote:
>
>> Geoff,
>>
>> Is this what you are after?
>>
>> SELECT
>> SUM (CASE
>> WHEN Col1 >= 0 THEN 1
>> ELSE 0
>> END) AS NonNegative,
>> SUM (CASE
>> WHEN Col1 >= 0 THEN 0
>> ELSE 1
>> END) AS Negative
>> FROM YourTable
>>
>> --RLF
>>
>> "Geoff" wrote in message
>>
>> > I'm trying trying to build a crosstab query using a Case statement to
>> > count
>> > the number of records in one column with the DateDiff >=0 and the other
>> > column to be a count of the Records with the DateDiff <0. I can do this
>> > easlily in MS Access with the IIF Function in a crosstab query but I
>> > can't
>> > get the Sql Statement to work in an MS Sql 2000 View
>>
>>
>>
 >> Stay informed about: Cross tab based on DateDIFF 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Selecting data from one table based on conditions in anoth.. - I'm used to doing simple queries from individual tables, so I hope someone can help with this. I need to select unique data from a column in table1 based on the value of a column in table2. The data in the column of table1 is not all unique, so I ..

SELECT DISTINCT records based only on two columns - I'm working on a mailing list and want to select records where the both the SSN number and Address fields are unique, since I may want to send a piece of mail to more than one address for a person, but not multiple pieces to the same address for that....

EXEC (select... ) problem Help! - How can I get this to work? declare @WkEmpID declare @sql varchar(1000) set @sql = 'select distinct @WkEmpID = EmpID from Employee' exec (@sql) Now this is a simplified version of a more comples query which is forcing me to use this method rather than...

Query returning multiple rows - Hi, I have a query that returns back rows that have multiple entries for a given UniqueID. I want the query to be expanded so that it then only returns 1 of the multiple rows based on the maximum date. Thefore, my return set looks like this: ..

Having a stored procedure copy tables & also preserve inde.. - Hello, I created a stored procedure that renames a table to OLD_xxxxx and replaces that table with another (copy) that resides on a different database. I pull the tablename names through the use of a cursor table and construct a SELECT INTO statement a...
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]