Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql question

Posted on 2015-02-04
12
Medium Priority
?
148 Views
Last Modified: 2015-02-05
Hi Experts,

I have the following two queries that would like to total the quantity of the records in the table, and for some reason I am missing some records.

Can someone explain where is the hole?

query1
SELECT dbo_TimeSheet.Day, dbo_TimeSheet.TimeEntered, Format([TimeEntered],"mm/dd/yy") AS Expr1
FROM dbo_TimeSheet WHERE (((dbo_TimeSheet.Day) Is Not Null) AND ((dbo_TimeSheet.TimeEntered) Is Not Null) AND ((Format([TimeEntered],"mm/dd/yy"))<>CStr([day])));

Open in new window

query2
SELECT dbo_TimeSheet.Day, dbo_TimeSheet.TimeEntered, Format([TimeEntered],"mm/dd/yy") AS Expr1
FROM dbo_TimeSheet WHERE (((dbo_TimeSheet.Day) Is Not Null) AND ((dbo_TimeSheet.TimeEntered) Is Not Null) AND ((Format([TimeEntered],"mm/dd/yy"))=CStr([day])));[/

Open in new window


P.S. there are only few records with no Day field entered or no TimeEntered, not enough to justify the discrepancy.
0
Comment
Question by:bfuchs
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40590323
>total the quantity of the records in the table
Total ... what exactly?  Sum of days, sum of time entered, ...
Both queries together, each one separately...
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40590327
Hi,
Total quantity of records from both queries together should equal the quantity of records in the table.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40590332
Define 'quantity', by the column name, and the aggregate you wish to perform (SUM, COUNT, etc.)
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 4

Author Comment

by:bfuchs
ID: 40590338
I Have a table containing 2 columns

Day where date is entered
TimeEntered where date+time is entered

would like to see all those records that day column has a different date than the date portion in TimeEntered field.

This is the reason I am running those queries.

In order to ensure I am getting the correct results, I want to sum all those records that dates are the same with those that they are not the same, and get the total qty of records equal to what I have in the table.
0
 
LVL 1

Expert Comment

by:lalitgada
ID: 40590362
hi,

Kindly check date entered in the table is correct or not.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 total points
ID: 40590374
I would suggest using DATEDIFF(interval, date1, date2) that returns an integer of the chosen interval between two dates instead of relying on character representations of dates. Here the interval of day ("d") can be used as you are only concerned with the date and not time.

You could check the numbers like this:
SELECT
      COUNT(*) AS TOT
    , SUM(IIF(DATEDIFF("d", [day], [TimeEntered]) = 0,1,0)) AS EQL
    , SUM(IIF(DATEDIFF("d", [day], [TimeEntered]) <> 0,1,0)) AS UNEQL
FROM dbo_TimeSheet
WHERE (dbo_TimeSheet.Day) IS NOT NULL
      AND (dbo_TimeSheet.TimeEntered) IS NOT NULL)
;

Open in new window


and get the rows like this
SELECT
      dbo_TimeSheet.Day
    , dbo_TimeSheet.TimeEntered
    , Format([TimeEntered], "mm/dd/yy") AS EXPR1
FROM dbo_TimeSheet
WHERE DATEDIFF("d", [day], [TimeEntered]) <> 0
;

Open in new window


SELECT
      dbo_TimeSheet.Day
    , dbo_TimeSheet.TimeEntered
    , Format([TimeEntered], "mm/dd/yy") AS EXPR1
FROM dbo_TimeSheet
WHERE DATEDIFF("d", [day], [TimeEntered]) = 0
;

Open in new window


Note: if either  [day] or [TimeEntered] are NULL then datediff() cannot return a value so only those rows with values in both fields can meet the  <> 0 or = 0 criteria.
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 1000 total points
ID: 40590493
As you want to check the total count of records, you would have to count those with Nulls as well.

You could have one single query:

Select
    dbo_TimeSheet.Day,
    dbo_TimeSheet.TimeEntered,
    DateValue([TimeEntered] AS DayOfTimeEntered,
    dbo_TimeSheet.Day Is Null Or dbo_TimeSheet.TimeEntered Is Null As NullDate,
    DateDiff("d", Nz(dbo_TimeSheet.Day, #00:00#), Nz(dbo_TimeSheet.TimeEntered, #00:00#)) = 0 As DateMatch
From
    dbo_TimeSheet

Now you can use this directly and filter as you wish, or you can use it as the source for other queries that filter the output.

/gustav
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40590538
How many records returns each query? And how many records was you expecting to be returned?
Without some sample data it's hard for us to try to guess.
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40592129
@PortletPaul,
In middle of testing yours..

@Gustav,
See attached.
untitled.bmp
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40592265
It is:

   DateValue([TimeEntered]) AS DayOfTimeEntered,

/gustav
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 40592301
Working well, thank you!
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40593018
You are welcome!

/gustav
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

927 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