Thursday, March 29, 2012
Help with CURRENT DATE Query
the first field and several other fields...one of which is DATE... now, I
want to run a query where my view is based upon the DATE. however I want to
only show transactions that are the same date as the system date (this is to
see how much work my employees have done). Please see my example below, this
code works...
SELECT transid, [date], description, amt, taxamt
FROM dbo.taxtransactions
WHERE ([date] = '9/9/2007')
However, where it has 9/9/2007, I want it to be TODAYS Date
Thanks for your help in advance!WHERE [date] = DATEDIFF(DAY, 0, CURRENT_TIMESTAMP);
"SQL Brad" <SQLBrad@.discussions.microsoft.com> wrote in message
news:58DEF545-F6C1-4086-B247-9080CEFBC125@.microsoft.com...
> Hello, I have a table that lists my bank transactions. I have a uniqueid
> in
> the first field and several other fields...one of which is DATE... now,
> I
> want to run a query where my view is based upon the DATE. however I want
> to
> only show transactions that are the same date as the system date (this is
> to
> see how much work my employees have done). Please see my example below,
> this
> code works...
> SELECT transid, [date], description, amt, taxamt
> FROM dbo.taxtransactions
> WHERE ([date] = '9/9/2007')
> However, where it has 9/9/2007, I want it to be TODAYS Date
> Thanks for your help in advance!
>|||Try:
SELECT transid, [date], description, amt, taxamt
FROM dbo.taxtransactions
WHERE
[date] >= convert(char(8), getdate(), 112) and
and [date] < dateadd(day, 1, convert(char(8), getdate(), 112))
go
AMB
"SQL Brad" wrote:
> Hello, I have a table that lists my bank transactions. I have a uniqueid in
> the first field and several other fields...one of which is DATE... now, I
> want to run a query where my view is based upon the DATE. however I want to
> only show transactions that are the same date as the system date (this is to
> see how much work my employees have done). Please see my example below, this
> code works...
> SELECT transid, [date], description, amt, taxamt
> FROM dbo.taxtransactions
> WHERE ([date] = '9/9/2007')
> However, where it has 9/9/2007, I want it to be TODAYS Date
> Thanks for your help in advance!
>|||Aaron...thanks for your help, it worked perfectly!! I also changed the 0 to
a 1 and it went to yesterday....very much appreciated!
"Aaron Bertrand [SQL Server MVP]" wrote:
> WHERE [date] = DATEDIFF(DAY, 0, CURRENT_TIMESTAMP);
>
> "SQL Brad" <SQLBrad@.discussions.microsoft.com> wrote in message
> news:58DEF545-F6C1-4086-B247-9080CEFBC125@.microsoft.com...
> > Hello, I have a table that lists my bank transactions. I have a uniqueid
> > in
> > the first field and several other fields...one of which is DATE... now,
> > I
> > want to run a query where my view is based upon the DATE. however I want
> > to
> > only show transactions that are the same date as the system date (this is
> > to
> > see how much work my employees have done). Please see my example below,
> > this
> > code works...
> >
> > SELECT transid, [date], description, amt, taxamt
> > FROM dbo.taxtransactions
> > WHERE ([date] = '9/9/2007')
> >
> > However, where it has 9/9/2007, I want it to be TODAYS Date
> >
> > Thanks for your help in advance!
> >
>
>sql
Help with constraint
I have a table where I want a certain condition with 3 of
its fields. I want only 1 of them not to be null.
For example, if the fields are A,B,C these combinations are ok:
A B C
NULL 3432 NULL
NULL NULL 554
333 NULL NULL
Howere these are not:
A B C
NULL NULL NULL
NULL 5454 554
333 545 6858
I'm not very familiar with the syntax of constraints, and I did this:
check(
case when A is null then 0 else 1 end+
case when B is null then 0 else 1 end+
case when C is null then 0 else 1 end
=1)
but I get a syntax error.
Any ideas?
Thanks!You could do something like this.
check(
A IS NOT NULL
OR B IS NOT NULL
OR C IS NOT NULL)
"Star" wrote:
> Hi,
> I have a table where I want a certain condition with 3 of
> its fields. I want only 1 of them not to be null.
> For example, if the fields are A,B,C these combinations are ok:
> A B C
> NULL 3432 NULL
> NULL NULL 554
> 333 NULL NULL
> Howere these are not:
> A B C
> NULL NULL NULL
> NULL 5454 554
> 333 545 6858
> I'm not very familiar with the syntax of constraints, and I did this:
> check(
> case when A is null then 0 else 1 end+
> case when B is null then 0 else 1 end+
> case when C is null then 0 else 1 end
> =1)
> but I get a syntax error.
> Any ideas?
> Thanks!
>|||That doesn′t work but I appreciate your help.
Even with that condition I can have something like this:
A B C
NULL 5454 554
I want only one of them to be populated.
Patrik wrote:
> You could do something like this.
> check(
> A IS NOT NULL
> OR B IS NOT NULL
> OR C IS NOT NULL)
>
> "Star" wrote:
>|||drop table aa
go
create table aa(a int, b int, c int)
go
alter table aa add constraint chk_abc check(
(a is null and b is null and c is not null) or
(a is null and b is not null and c is null) or
(a is not null and b is null and c is null)
)
go
insert into aa values(1,null,null)
insert into aa values(null,1,null)
insert into aa values(null,null,1)
insert into aa values(1,1,null)
insert into aa values(null,1,1)
insert into aa values(1,null,1)
insert into aa values(1,1,1)
insert into aa values(null,null,null)
go
select * from aa|||I think I got it to work.
Just in case someone is interested:
(case when ([A] is null) then 0 else 1 end + case when ([B] is null)
then 0 else 1 end + case when ([C] is null) then 0 else 1 end = 1)|||Ok, misunderstood you.
Try this
check(
(case isnull(a,-1) when -1 then 0 else 1 end + case isnull(b,-1) when -1
then 0 else 1 end + case isnull(c,-1) when -1 then 0 else 1 end) = 1
)
"Star" wrote:
> That doesn′t work but I appreciate your help.
> Even with that condition I can have something like this:
> A B C
> NULL 5454 554
> I want only one of them to be populated.
>
>
> Patrik wrote:
>|||drop table aa
go
create table aa(a int, b int, c int)
go
alter table aa add constraint chk_abc check(
(a is null and b is null and c is not null) or
(a is null and b is not null and c is null) or
(a is not null and b is null and c is null)
)
go
insert into aa values(1,null,null)
insert into aa values(null,1,null)
insert into aa values(null,null,1)
insert into aa values(1,1,null)
insert into aa values(null,1,1)
insert into aa values(1,null,1)
insert into aa values(1,1,1)
insert into aa values(null,null,null)
go
select * from aa|||On Thu, 26 Jan 2006 13:51:41 -0500, Star wrote:
>Hi,
>I have a table where I want a certain condition with 3 of
>its fields. I want only 1 of them not to be null.
>For example, if the fields are A,B,C these combinations are ok:
>A B C
>NULL 3432 NULL
>NULL NULL 554
>333 NULL NULL
>Howere these are not:
>A B C
>NULL NULL NULL
>NULL 5454 554
>333 545 6858
>I'm not very familiar with the syntax of constraints, and I did this:
>check(
>case when A is null then 0 else 1 end+
>case when B is null then 0 else 1 end+
>case when C is null then 0 else 1 end
>=1)
>but I get a syntax error.
>Any ideas?
Hi Star,
Though you've gotten some alternative formulations, I fail to see why
you would have gotten syntax errors. The code below runs fine for me:
CREATE TABLE Star(A int, B int, C int,
check(case when A is null then 0 else 1 end +
case when B is null then 0 else 1 end +
case when C is null then 0 else 1 end = 1)
)
go
-- Accepted
INSERT INTO Star (A, B, C)
select 1, null, null
union all
select null, 1, null
union all
select null, null, 1
-- Rejected
INSERT INTO Star (A, B, C)
SELECT 1, 1, null
INSERT INTO Star (A, B, C)
SELECT null, null, null
INSERT INTO Star (A, B, C)
SELECT 1, 1, 1
-- Show results
select * from Star
go
DROP TABLE Star
go
Hugo Kornelis, SQL Server MVP|||>From the samp[le data, it looks like they are all positive integers, so
we can use this trick:
CHECK ( COALESCE (SIGN(a), 0)
+ COALESCE (SIGN(b), 0)
+ COALESCE (SIGN(c), 0) = 1)
if you have negative numbers use SIGN (ABS(x)) and if you have zeroes,
use SIGN (ABS(x+1))|||usually problems like this arise when mutually exclusive subtypes are
stored in one table. Is that the case? Are you considering splitting up
the table?
Friday, March 23, 2012
Help with a View
contains fields: filenumber, status, date, qualifier and comments. The
qualifier field is either 100 or 101, meaning the file is confirmed or
estimated, respectively. I then join tblElectronic to another table
(tblFile) in a view. I am having trouble building the view. The field
Qualifier in tblElectronic can have the value 100 or 101.
filenumber statusDate Qualifier Comments
1111xxxx01/01/01 100Comments
1111xxxx01/01/01 101Comments
1112xxxy01/01/01 101Comments
1113xyxy01/01/01 100 Comments
I want to use the record where qualifier = 100 in my view, except in the
case where 101 is the only qualifier that exists, meaning it has not
been confirmed yet.
So my view should pull the rows:
filenumberstatusdate qualifiercomments
1111xxxx01/01/01 101Comments
1112xxxy01/01/01 101Comments
1113xyxy01/01/01 100 Comments
I have tried case statements but to no avail. Here is my view:
SELECT tblFile.Filenumber, tblFile.DataofFile, tblElectronic.status,
tblElectronic.date, tblElectronic.comments
FROM tblFile inner join
tblElectronic on tblFile.filenumber = tblElectronic.filenumber
where tblElectronic.qualifier = ??
Not sure what to put here, since I want it to be where qualifier = 100
unless that doesn't exist, in which case I want it to be where qualifier
= 101.
Thanks for the help.
Rubia
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!Hi
It is always better to post DDL ( CREATE TABLE statements etc..) and example
data (as INSERT statements) with the expected output from that data. This
will remove any ambiguities from your descriptions and helps everyone try
out their replies. There is no definition for tblFile which does not help!!
It is not clear if you want one or all records. But this may help
SELECT F.Filenumber, F.DataofFile, E.status, E.date, E.comments, E.qualifier
FROM tblFile F
join tblElectronic E on F.filenumber = E.filenumber
WHERE E.qualifier = 100
OR ( E.qualifier = 101
AND NOT EXISTS ( SELECT 1 FROM tblElectronic T
WHERE T.filenumber = E.filenumber
AND T.qualifier = 100 )
)
John
"Rubia 078" <rubia078@.yahoo.com> wrote in message
news:40ec7b0c$0$16462$c397aba@.news.newsgroups.ws.. .
> I have a table tblElectronic that recieves data electronically. It
> contains fields: filenumber, status, date, qualifier and comments. The
> qualifier field is either 100 or 101, meaning the file is confirmed or
> estimated, respectively. I then join tblElectronic to another table
> (tblFile) in a view. I am having trouble building the view. The field
> Qualifier in tblElectronic can have the value 100 or 101.
> filenumber status Date Qualifier Comments
> 1111 xxxx 01/01/01 100 Comments
> 1111 xxxx 01/01/01 101 Comments
> 1112 xxxy 01/01/01 101 Comments
> 1113 xyxy 01/01/01 100 Comments
> I want to use the record where qualifier = 100 in my view, except in the
> case where 101 is the only qualifier that exists, meaning it has not
> been confirmed yet.
> So my view should pull the rows:
> filenumber status date qualifier comments
> 1111 xxxx 01/01/01 101 Comments
> 1112 xxxy 01/01/01 101 Comments
> 1113 xyxy 01/01/01 100 Comments
>
> I have tried case statements but to no avail. Here is my view:
> SELECT tblFile.Filenumber, tblFile.DataofFile, tblElectronic.status,
> tblElectronic.date, tblElectronic.comments
> FROM tblFile inner join
> tblElectronic on tblFile.filenumber = tblElectronic.filenumber
> where tblElectronic.qualifier = ??
> Not sure what to put here, since I want it to be where qualifier = 100
> unless that doesn't exist, in which case I want it to be where qualifier
> = 101.
> Thanks for the help.
> Rubia
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!|||John,
Thank you, I believe that will work. I won't be able to test it until
tomorrow, but it would appear to be what I needed.
Thank you!
Rubia
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Monday, March 19, 2012
Help with a query
I have a table tScores with 3 fields: user, score and stage.
What I want is to select the 2 best scores (highest) of each user.
Ex.
tScores
--
A, 20, 1
A, 10, 2
A, 30, 3
A, 40, 4
A, 10, 5
B, 20, 1
B, 30, 2
B, 10, 3
C, 10, 1
The result would be:
A, 40, 4
A, 30, 3
B, 30, 2
B, 20, 1
C, 10, 1
Thanks in advance,
John LewisJohn Lewis wrote:
> Hi,
>
> I have a table tScores with 3 fields: user, score and stage.
> What I want is to select the 2 best scores (highest) of each user.
>
> Ex.
> tScores
> --
> A, 20, 1
> A, 10, 2
> A, 30, 3
> A, 40, 4
> A, 10, 5
> B, 20, 1
> B, 30, 2
> B, 10, 3
> C, 10, 1
>
> The result would be:
>
> A, 40, 4
> A, 30, 3
> B, 30, 2
> B, 20, 1
> C, 10, 1
>
> Thanks in advance,
>
> John Lewis
How do you want to handle the case where the user has the same highest
score more than once (or more than twice)? I'll assume you want all
tied scores even if that means you return more than two rows per user:
SELECT [user], score, stage
FROM tScores AS S
WHERE 2 >
(SELECT COUNT(*)
FROM tScores
WHERE [user] = S.[user]
AND score > S.score) ;
User is a bad choice for a column name because it's a reserved word and
because it is too vague (user_name, user_login, user_id ?)
David Portas
SQL Server MVP
--|||Hello David,
I'll offer this as an alternative for SqlServer 2005:
drop table #tests
go
create table #tests
(
[user] char(1) not null,
score tinyint not null,
stage tinyint not null
)
go
insert into #tests values('A', 20, 1)
insert into #tests values('A', 10, 2)
insert into #tests values('A', 30, 3)
insert into #tests values('A', 40, 4)
insert into #tests values('A', 10, 5)
insert into #tests values('B', 20, 1)
insert into #tests values('B', 30, 2)
insert into #tests values('B', 10, 3)
insert into #tests values('C', 10, 1)
go
;with tests([user],score,stage,rank) as
(
select [user],score,stage,
rank() over (partition by [user] order by score desc) as rank
from #tests
)
select [user],score,stage
from tests
where rank < 3
order by [user],score desc
go
Thank you,
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/|||"John Lewis" <julugu@.gmail.com> wrote in message
news:1132057809.298727.286240@.o13g2000cwo.googlegroups.com...
> Hi,
>
> I have a table tScores with 3 fields: user, score and stage.
> What I want is to select the 2 best scores (highest) of each user.
>
<snip />
> Thanks in advance,
>
> John Lewis
>
Multipost (over in microsoft.public.access.queries, under "Easy
query? Not to me.").
Sincerely,
Chris O.|||Sorry to post in another group, but I thought it was
sqlserver.queries.
Thanks to all for the answers.
Help with a query
State, City, Name, Primary_Contact
IL, Springfield, Bill, n
IL, Springfield, Frank, n
IL, Springfield, Larry, n
IL, Bloomington, Steve, n
IL, Bloomington, Chris, y
IL, Chicago, Betty, n
IL, Chicago, Linda, n
IL, Chicago, Sue, n
I need a query to return the state and cities that don't have a
Primary_Contact='y'
So the results would be:
IL, Springfield
IL, Chicago
That's it. Any help is greatly appreciated.--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1
SELECT State, City
FROM table_name
WHERE Primary_Contact<>'y'
GROUP BY State, City
--
MGFoster:::mgf00 <atearthlink <decimal-pointnet
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **
--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBRUBYA4echKqOuFEgEQLJTACgsOAgruNlQX254w4Abe/ychTn9IAAn11t
O+xXdFBxIeubcPHE0uh6fyoi
=YNNo
--END PGP SIGNATURE--
foneguy2 wrote:
Quote:
Originally Posted by
I have a table with 4 relevant fields (blank lines added for clarity).
State, City, Name, Primary_Contact
IL, Springfield, Bill, n
IL, Springfield, Frank, n
IL, Springfield, Larry, n
>
IL, Bloomington, Steve, n
IL, Bloomington, Chris, y
>
IL, Chicago, Betty, n
IL, Chicago, Linda, n
IL, Chicago, Sue, n
>
I need a query to return the state and cities that don't have a
Primary_Contact='y'
So the results would be:
IL, Springfield
IL, Chicago
>
That's it. Any help is greatly appreciated.
>
Quote:
Originally Posted by
>I have a table with 4 relevant fields (blank lines added for clarity).
>State, City, Name, Primary_Contact
>IL, Springfield, Bill, n
>IL, Springfield, Frank, n
>IL, Springfield, Larry, n
>
>IL, Bloomington, Steve, n
>IL, Bloomington, Chris, y
>
>IL, Chicago, Betty, n
>IL, Chicago, Linda, n
>IL, Chicago, Sue, n
>
>I need a query to return the state and cities that don't have a
>Primary_Contact='y'
>So the results would be:
>IL, Springfield
>IL, Chicago
>
>That's it. Any help is greatly appreciated.
Hi foneguy2,
The solution posted by MGFoster won't work, unfortunately. It will give
you all State/City combo's that have at least one Primary_Contact='n'.
Here's the "straightforward" solution:
SELECT DISTINCT a.State, a.City
FROM YourTable AS a
WHERE NOT EXISTS
(SELECT *
FROM YourTable AS b
WHERE b.State = a.State
AND b.City = a.City
AND b.Primary_Contact = 'n');
And here's a more clever (but harder to graps) solution that might run a
bit faster:
SELECT State, City
FROM YourTable
GROUP BY State, City
HAVING MIN(Primary_Contact) = 'n';
(Untested - see www.aspfaq.com/5006 if you prefer a tested reply).
--
Hugo Kornelis, SQL Server MVP|||>I have a table with 4 relevant fields [sic] .. <<
Suggestions:
1) learn why a column is nothing like a field
2) Use a numeric code instead of a fake Boolean flag. That way the
MIN() will give you a contact in every location. I would bet you spend
time updating the flags.
CREATE TABLE Contacts
(state_code CHAR(2) NOT NULL,
city_name CHAR(25) NOT NULL,
contact_name CHAR(25) NOT NULL,
contact_priority INTEGER NOT NULL
CHECK (contact_priority 0),
PRIMARY KEY (state_code, city_name, contact_name, contact_priority));
This will show one contact in every city
CREATE VIEW PrimaryContacts (state_code, city_name, contact_name)
AS
SELECT C1.state_code, C1.city_name, C1.contact_name
FROM Contacts AS C1
WHERE contact_priority
= (SELECT MIN (C2.contact_priority)
FROM Contacts AS C2
WHERE C1.state_code = C2.state_code
AND C1.city_name = C2.city_name);
Help With a query
and Qty. Can you help me with this query...
table1 has two rows
Row 1 Contains ProductA with a Qty of two
Row 2 Contains ProductB with a Qty of four
table2 has has two rows
Row 1 Contains ProductB with a Qty of two
Row 2 Contains ProductC with a Qty of three
What I want to return from a SQL query is a table with three fields...
Product, Table1QTY and Table2QTY
With the example above I would like the result to have three rows, one
for each product.
Any help?
CiarnOn 9 Nov 2004 03:48:54 -0800, Ciar?n wrote:
>If I have table1 and table2, which both have the same fields Product
>and Qty. Can you help me with this query...
>
>table1 has two rows
>Row 1 Contains ProductA with a Qty of two
>Row 2 Contains ProductB with a Qty of four
>table2 has has two rows
>Row 1 Contains ProductB with a Qty of two
>Row 2 Contains ProductC with a Qty of three
>
>What I want to return from a SQL query is a table with three fields...
>Product, Table1QTY and Table2QTY
>With the example above I would like the result to have three rows, one
>for each product.
>Any help?
>Ciarn
Hi Ciarn
Try if this works for you:
SELECT COALESCE(a.Product, b.Product) AS Product,
COALESCE(a.Qty,0) + COAELSCE(b.Qty,0) AS Qty
FROM table1 AS a
FULL OUTER JOIN table2 AS b
ON a.Product = b.Product
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||>
> Hi Ciarn
> Try if this works for you:
> SELECT COALESCE(a.Product, b.Product) AS Product,
> COALESCE(a.Qty,0) + COAELSCE(b.Qty,0) AS Qty
> FROM table1 AS a
> FULL OUTER JOIN table2 AS b
> ON a.Product = b.Product
> Best, Hugo
Hugo,
Thanks a million.
Had to tweak the script ever so slightly to get the Qtys in seperate
columns like below, but wouldn't have figured it out without your
help.
SELECT COALESCE (A.Product, B.Product) AS PRODUCT, COALESCE
(A.Qty, 0) AS QTY_A, COALESCE (B.Qty, 0) AS QTY_B
FROM Sheet1 A FULL OUTER JOIN
Sheet2 B ON A.Product = B.Product
WHERE A.Qty>B.Qty
Cheers again.
Ciarn|||On 9 Nov 2004 07:19:49 -0800, Ciar?n wrote:
>Had to tweak the script ever so slightly to get the Qtys in seperate
>columns like below, but wouldn't have figured it out without your
>help.
Hi Ciarn,
I see I misread your query - somehow, I thought you wanted the total.
Apologies for that.
>SELECT COALESCE (A.Product, B.Product) AS PRODUCT, COALESCE
>(A.Qty, 0) AS QTY_A, COALESCE (B.Qty, 0) AS QTY_B
>FROM Sheet1 A FULL OUTER JOIN
> Sheet2 B ON A.Product = B.Product
>WHERE A.Qty>B.Qty
Are you sure about the WHERE clause? With default ANSI settings, this
would remove the rows for all products that are pressent in only Sheet1 or
in only Sheet2, since a comparison involving NULL will never evaluate to
TRUE.
Since this requirement is far from obvious from your first post, I wonder
why you included this WHERE clause and what the acutal business problem
you have to solve is.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Monday, March 12, 2012
Help with 2 datetime fields-1 stores date, the other time
We have a lame app that uses 2 datetime(8) fields, 1 stores the date, the
other the time.
example query:
select aud_dt, aud_tm
from orders
results:
aud_dt aud_tm
2006-06-08 00:00:00.000 1900-01-01 12:32:26.287
I'm trying to create a query that give me records from the current date in
the past hour.
Here's a script that gives me todays date but I cannot figure out the time:
select aud_dt, aud_tm, datediff(d,aud_dt,getdate()), datediff(mi, aud_tm,
getdate())
from orders
where (datediff(d,aud_dt,getdate()) = 0)
results:
aud_dt aud_tm
datediff(0=today) timediff (since 1900-01-01)
2006-06-08 00:00:00.000 1900-01-01 12:32:26.287 0
55978689
I added this next part to the above query but it does not work since the
date/time is from 1900-01-01
and (datediff(mi, aud_tm, getdate()) <= 60)
Thanks for any help.rdraider wrote:
> Hi,
> We have a lame app that uses 2 datetime(8) fields, 1 stores the date, the
> other the time.
> example query:
> select aud_dt, aud_tm
> from orders
> results:
> aud_dt aud_tm
> 2006-06-08 00:00:00.000 1900-01-01 12:32:26.287
> I'm trying to create a query that give me records from the current date in
> the past hour.
> Here's a script that gives me todays date but I cannot figure out the time:
> select aud_dt, aud_tm, datediff(d,aud_dt,getdate()), datediff(mi, aud_tm,
> getdate())
> from orders
> where (datediff(d,aud_dt,getdate()) = 0)
> results:
> aud_dt aud_tm
> datediff(0=today) timediff (since 1900-01-01)
> 2006-06-08 00:00:00.000 1900-01-01 12:32:26.287 0
> 55978689
>
> I added this next part to the above query but it does not work since the
> date/time is from 1900-01-01
> and (datediff(mi, aud_tm, getdate()) <= 60)
>
> Thanks for any help.
The correct way would be to fix the database and use one datetime
column. I'll assume you already know this and that it isn't possible
for some reason.
So, if you want to combine those two into one datetime field (which you
could then use in a query however you like) you can use something like
this:
cast((cast(aud_dt as float) + cast(aud_tm as float)) as datetime)
although you might lose some precision in the miliseconds. If that's
unacceptable, you can instead do this:
convert(datetime, convert(varchar(10), aud_dt, 1) + ' ' +
convert(varchar(10), aud_tm, 14))|||These both work well. Miliseconds don't matter.
Thank you.
"ZeldorBlat" <zeldorblat@.gmail.com> wrote in message
news:1149832253.251499.139790@.h76g2000cwa.googlegr oups.com...
> rdraider wrote:
>> Hi,
>> We have a lame app that uses 2 datetime(8) fields, 1 stores the date, the
>> other the time.
>> example query:
>>
>> select aud_dt, aud_tm
>> from orders
>>
>> results:
>> aud_dt aud_tm
>> 2006-06-08 00:00:00.000 1900-01-01 12:32:26.287
>>
>> I'm trying to create a query that give me records from the current date
>> in
>> the past hour.
>> Here's a script that gives me todays date but I cannot figure out the
>> time:
>>
>> select aud_dt, aud_tm, datediff(d,aud_dt,getdate()), datediff(mi, aud_tm,
>> getdate())
>> from orders
>> where (datediff(d,aud_dt,getdate()) = 0)
>>
>> results:
>> aud_dt aud_tm
>> datediff(0=today) timediff (since 1900-01-01)
>> 2006-06-08 00:00:00.000 1900-01-01 12:32:26.287 0
>> 55978689
>>
>>
>> I added this next part to the above query but it does not work since the
>> date/time is from 1900-01-01
>> and (datediff(mi, aud_tm, getdate()) <= 60)
>>
>>
>> Thanks for any help.
> The correct way would be to fix the database and use one datetime
> column. I'll assume you already know this and that it isn't possible
> for some reason.
> So, if you want to combine those two into one datetime field (which you
> could then use in a query however you like) you can use something like
> this:
> cast((cast(aud_dt as float) + cast(aud_tm as float)) as datetime)
> although you might lose some precision in the miliseconds. If that's
> unacceptable, you can instead do this:
> convert(datetime, convert(varchar(10), aud_dt, 1) + ' ' +
> convert(varchar(10), aud_tm, 14))
Friday, March 9, 2012
Help using Lookup
Hi
I am trying to use lookup to see if a item esists in my table ( 3 key fields ). If the lookup fails I want to insert the records. If it succeeds I have put a recordcount to catch the items that are not required. I don't think that I understand the settings for failed rows. I have tried setting the Configure Error Output to redirect, but this does not seem to work. I have the below errors.
[SQL Server Destination [151]] Error: Unable to prepare the SSIS bulk insert for data insertion.
[DTS.Pipeline] Error: component "SQL Server Destination" (151) failed the pre-execute phase and returned error code 0xC0202071.
Can someone please advise me how to set up this component to work for my application
Thanks
ADG
SQL Server Destinations only work on the machine that have the actual SQL Server installed locally. Is this the case?|||Hi Phil
I have SQL Server 2005 Developer Edition installed on a stand alone machine at the moment. Previous two task flows use a SQL Server Destination (same table) and work OK.
I did not know that I could not use a SQL Server Destination on a network. Eventually I will migrate my solution to one of our group servers, currently I am developing the solution, or rather battling to learn SQL server. I guess that maybe I should use another type of destination once the above bug is ironed out.
Regards
ADG
|||Bug fixed
I deleted the Look up and set it up again and all is well. Not sure what i did first time, ( I fiddled with too many settings I suspect
Friday, February 24, 2012
Help selecting the proper child record
I have a person table with personID. I have a personRate table with
personID, rateID, and effectiveDate.
I need to select fields from personRate, but I want the fields from the
proper record.
I need the one child record that has the most current date of the largest
rateID.
For example a person may have many rate records. I need the record that has
the most current date of the largest rateID they have. Does that make
sense?
I am making a view that has data from both tables. I need to display the
most current rate info.
Any ideas? TIA ~ CKHi CK,
I kind of guessed on the DDL but something like the select statement below
(after the Creates and Inserts) should do it for you.
Create Table Person(
PersonID int identity(1,1) Primary Key,
PersonName varchar(50))
Create Table PersonRate(
RateID int identity(1,1) Primary Key,
PersonID int references Person(PersonID),
Rate decimal(9,2),
EffectiveDate smalldatetime)
insert Person(PersonName) values ('Fred')
insert Person(PersonName) values ('Barney')
insert Person(PersonName) values ('Wilma')
insert PersonRate(PersonID, Rate, EffectiveDate) values(1,10,'1/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(1,11,'2/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(1,12,'3/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(2,10,'1/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(2,11,'2/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(2,12,'3/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(3,10,'1/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(3,11,'2/1/2005')
insert PersonRate(PersonID, Rate, EffectiveDate) values(3,12,'3/1/2005')
select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.Rate
from Person p
inner join (select pr.PersonID, pr.EffectiveDate, pr.RateID, pr.Rate
from PersonRate pr
inner join (select PersonID, Max(RateID) as MaxRateID
from PersonRate
group by PersonID) pr1
on pr.personid = pr1.personid
and pr.rateid = pr1.maxRateID) pp
on p.PersonID = pp.Personid
--
-Dick Christoph
"CK" <c_kettenbach@.hotmail.com> wrote in message
news:Z1CUf.58897$Jd.37489@.newssvr25.news.prodigy.n et...
> Good Morning,
> I have a person table with personID. I have a personRate table with
> personID, rateID, and effectiveDate.
> I need to select fields from personRate, but I want the fields from the
> proper record.
> I need the one child record that has the most current date of the largest
> rateID.
> For example a person may have many rate records. I need the record that
> has the most current date of the largest rateID they have. Does that make
> sense?
> I am making a view that has data from both tables. I need to display the
> most current rate info.
> Any ideas? TIA ~ CK|||See changes below. Also not every Person has a PersonRateRecord but I need
all the persons so I am thinking LEFT OUTER JOIN on PersonRate.
Thanks for the pointers!!
"DickChristoph" <dchristo99@.yahoo.com> wrote in message
news:XuEUf.18265$iR1.4137@.tornado.rdc-kc.rr.com...
> Hi CK,
> I kind of guessed on the DDL but something like the select statement below
> (after the Creates and Inserts) should do it for you.
> Create Table Person(
> PersonID int identity(1,1) Primary Key,
> PersonName varchar(50))
> Create Table PersonRate(
*****PersonRateID int identity(1,1) Primary Key,
***** RateID int references Rate(RateID),
> PersonID int references Person(PersonID),
> Rate decimal(9,2),
> EffectiveDate smalldatetime)
Create Table Rate(
RateID int identity(1,1) Primary Key,
RateType varchar(20))
> insert Person(PersonName) values ('Fred')
> insert Person(PersonName) values ('Barney')
> insert Person(PersonName) values ('Wilma')
>
> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,10,'1/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,11,'2/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,12,'3/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,10,'1/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,11,'2/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,12,'3/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,10,'1/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,11,'2/1/2005')
> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,12,'3/1/2005')
>
> select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.Rate
> from Person p
> inner join (select pr.PersonID, pr.EffectiveDate, pr.RateID, pr.Rate
> from PersonRate pr
> inner join (select PersonID, Max(RateID) as MaxRateID
> from PersonRate
> group by PersonID) pr1
> on pr.personid = pr1.personid
> and pr.rateid = pr1.maxRateID) pp
> on p.PersonID = pp.Personid
> --
> -Dick Christoph
> "CK" <c_kettenbach@.hotmail.com> wrote in message
> news:Z1CUf.58897$Jd.37489@.newssvr25.news.prodigy.n et...
>> Good Morning,
>> I have a person table with personID. I have a personRate table with
>> personID, rateID, and effectiveDate.
>> I need to select fields from personRate, but I want the fields from the
>> proper record.
>>
>> I need the one child record that has the most current date of the largest
>> rateID.
>>
>> For example a person may have many rate records. I need the record that
>> has the most current date of the largest rateID they have. Does that
>> make sense?
>>
>> I am making a view that has data from both tables. I need to display the
>> most current rate info.
>>
>> Any ideas? TIA ~ CK
>>|||Hi CK
Alrighty then how about this. By the way I think the PersonRate.Rate column
is not where it should be. Probably it belongs in the Rate table but I don't
know what that column really means. You left it in PersonRate so I left it
in PersonRate.
Create Table Person(
PersonID int identity(1,1) Primary Key,
PersonName varchar(50))
Create Table Rate(
RateID int identity(1,1) Primary Key,
RateType varchar(20))
Create Table PersonRate(
PersonRateID int identity(1,1) Primary Key,
RateID int references Rate(RateID),
PersonID int references Person(PersonID),
Rate decimal(9,2),
EffectiveDate smalldatetime)
insert Person(PersonName) values ('Fred')
insert Person(PersonName) values ('Barney')
insert Person(PersonName) values ('Wilma')
insert Rate(RateType) values ('A')
insert Rate(RateType) values ('B')
insert Rate(RateType) values ('C')
--Fred
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(1,1,10,'1/1/2005')
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(2,1,11,'2/1/2005')
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(3,1,12,'3/1/2005')
--Barney
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(1,2,10,'1/1/2005')
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(2,2,11,'2/1/2005')
insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
values(3,2,12,'3/1/2005')
--No PersonRate For Wilma
select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.RateType,
pp.Rate
from Person p
left outer join (select pr.PersonID, pr.EffectiveDate, pr.RateID,
r.RateType, pr.Rate
from PersonRate pr
inner join Rate r
on pr.RateID = r.RateID
inner join (select PersonID, Max(RateID) as MaxRateID
from PersonRate
group by PersonID) pr1
on pr.personid = pr1.personid
and pr.rateid = pr1.maxRateID) pp
on p.PersonID = pp.Personid
--
-Dick Christoph
dchristo@.mn.rr.com
612-724-9282
"CK" <c_kettenbach@.hotmail.com> wrote in message
news:5HEUf.62662$dW3.28210@.newssvr21.news.prodigy. com...
> See changes below. Also not every Person has a PersonRateRecord but I
> need all the persons so I am thinking LEFT OUTER JOIN on PersonRate.
> Thanks for the pointers!!
> "DickChristoph" <dchristo99@.yahoo.com> wrote in message
> news:XuEUf.18265$iR1.4137@.tornado.rdc-kc.rr.com...
>> Hi CK,
>>
>> I kind of guessed on the DDL but something like the select statement
>> below (after the Creates and Inserts) should do it for you.
>>
>> Create Table Person(
>> PersonID int identity(1,1) Primary Key,
>> PersonName varchar(50))
>>
>> Create Table PersonRate(
> *****PersonRateID int identity(1,1) Primary Key,
> ***** RateID int references Rate(RateID),
>> PersonID int references Person(PersonID),
>> Rate decimal(9,2),
>> EffectiveDate smalldatetime)
>>
> Create Table Rate(
> RateID int identity(1,1) Primary Key,
> RateType varchar(20))
>
>> insert Person(PersonName) values ('Fred')
>> insert Person(PersonName) values ('Barney')
>> insert Person(PersonName) values ('Wilma')
>>
>>
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,10,'1/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,11,'2/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,12,'3/1/2005')
>>
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,10,'1/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,11,'2/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,12,'3/1/2005')
>>
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,10,'1/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,11,'2/1/2005')
>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,12,'3/1/2005')
>>
>>
>> select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.Rate
>> from Person p
>> inner join (select pr.PersonID, pr.EffectiveDate, pr.RateID, pr.Rate
>> from PersonRate pr
>> inner join (select PersonID, Max(RateID) as MaxRateID
>> from PersonRate
>> group by PersonID) pr1
>> on pr.personid = pr1.personid
>> and pr.rateid = pr1.maxRateID) pp
>> on p.PersonID = pp.Personid
>>
>> --
>> -Dick Christoph
>> "CK" <c_kettenbach@.hotmail.com> wrote in message
>> news:Z1CUf.58897$Jd.37489@.newssvr25.news.prodigy.n et...
>>> Good Morning,
>>> I have a person table with personID. I have a personRate table with
>>> personID, rateID, and effectiveDate.
>>> I need to select fields from personRate, but I want the fields from the
>>> proper record.
>>>
>>> I need the one child record that has the most current date of the
>>> largest rateID.
>>>
>>> For example a person may have many rate records. I need the record that
>>> has the most current date of the largest rateID they have. Does that
>>> make sense?
>>>
>>> I am making a view that has data from both tables. I need to display
>>> the most current rate info.
>>>
>>> Any ideas? TIA ~ CK
>>>
>>
>>|||Rate is a $ amount , rateType is string indicating what the $ amount is for.
Like a "raise", "offer" , "request". Just some entities this company uses.
Yes it is in the correct place. Thanks a lot. Max didn't give me the desired
result. I use SELECT TOP 1.
here's what worked. I added a couple other tables as i needed values from
them as well
SELECT vC.*, CR.EffectiveDate, rt.TypeName, jc.CategoryName,
CASE
WHEN CR.Rate IS NULL THEN CAST('None' AS char(4))
ELSE CAST(CR.Rate AS varchar(6))
END AS Rate
FROM vCandidates vC
LEFT OUTER JOIN CandidateRate CR
ON vC.CandidateID = CR.CandidateID
AND CR.CandidateRateID IN
(
SELECT TOP 1 CandidateRateID
FROM CandidateRate
WHERE RateTypeID < 4 AND CandidateID = CR.CandidateID
ORDER BY RateTypeID DESC, EffectiveDate DESC
)
LEFT OUTER JOIN RateType rt ON rt.RateTypeID = CR.RateTypeID
LEFT OUTER JOIN Innova.dbo.JobCategory jc ON jc.JobCatID = CR.JobCatID
"DickChristoph" <dchristo99@.yahoo.com> wrote in message
news:dYEUf.18266$iR1.4639@.tornado.rdc-kc.rr.com...
> Hi CK
> Alrighty then how about this. By the way I think the PersonRate.Rate
> column is not where it should be. Probably it belongs in the Rate table
> but I don't know what that column really means. You left it in PersonRate
> so I left it in PersonRate.
> Create Table Person(
> PersonID int identity(1,1) Primary Key,
> PersonName varchar(50))
> Create Table Rate(
> RateID int identity(1,1) Primary Key,
> RateType varchar(20))
> Create Table PersonRate(
> PersonRateID int identity(1,1) Primary Key,
> RateID int references Rate(RateID),
> PersonID int references Person(PersonID),
> Rate decimal(9,2),
> EffectiveDate smalldatetime)
>
> insert Person(PersonName) values ('Fred')
> insert Person(PersonName) values ('Barney')
> insert Person(PersonName) values ('Wilma')
> insert Rate(RateType) values ('A')
> insert Rate(RateType) values ('B')
> insert Rate(RateType) values ('C')
> --Fred
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(1,1,10,'1/1/2005')
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(2,1,11,'2/1/2005')
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(3,1,12,'3/1/2005')
> --Barney
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(1,2,10,'1/1/2005')
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(2,2,11,'2/1/2005')
> insert PersonRate(RateID, PersonID, Rate, EffectiveDate)
> values(3,2,12,'3/1/2005')
> --No PersonRate For Wilma
>
> select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.RateType,
> pp.Rate
> from Person p
> left outer join (select pr.PersonID, pr.EffectiveDate, pr.RateID,
> r.RateType, pr.Rate
> from PersonRate pr
> inner join Rate r
> on pr.RateID = r.RateID
> inner join (select PersonID, Max(RateID) as MaxRateID
> from PersonRate
> group by PersonID) pr1
> on pr.personid = pr1.personid
> and pr.rateid = pr1.maxRateID) pp
> on p.PersonID = pp.Personid
>
> --
> -Dick Christoph
> dchristo@.mn.rr.com
> 612-724-9282
> "CK" <c_kettenbach@.hotmail.com> wrote in message
> news:5HEUf.62662$dW3.28210@.newssvr21.news.prodigy. com...
>> See changes below. Also not every Person has a PersonRateRecord but I
>> need all the persons so I am thinking LEFT OUTER JOIN on PersonRate.
>>
>> Thanks for the pointers!!
>>
>> "DickChristoph" <dchristo99@.yahoo.com> wrote in message
>> news:XuEUf.18265$iR1.4137@.tornado.rdc-kc.rr.com...
>>> Hi CK,
>>>
>>> I kind of guessed on the DDL but something like the select statement
>>> below (after the Creates and Inserts) should do it for you.
>>>
>>> Create Table Person(
>>> PersonID int identity(1,1) Primary Key,
>>> PersonName varchar(50))
>>>
>>> Create Table PersonRate(
>> *****PersonRateID int identity(1,1) Primary Key,
>> ***** RateID int references Rate(RateID),
>>> PersonID int references Person(PersonID),
>>> Rate decimal(9,2),
>>> EffectiveDate smalldatetime)
>>>
>>
>> Create Table Rate(
>> RateID int identity(1,1) Primary Key,
>> RateType varchar(20))
>>
>>
>>> insert Person(PersonName) values ('Fred')
>>> insert Person(PersonName) values ('Barney')
>>> insert Person(PersonName) values ('Wilma')
>>>
>>>
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,10,'1/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,11,'2/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(1,12,'3/1/2005')
>>>
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,10,'1/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,11,'2/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(2,12,'3/1/2005')
>>>
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,10,'1/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,11,'2/1/2005')
>>> insert PersonRate(PersonID, Rate, EffectiveDate) values(3,12,'3/1/2005')
>>>
>>>
>>> select p.PersonID, p.PersonName, pp.EffectiveDate, pp.RateID, pp.Rate
>>> from Person p
>>> inner join (select pr.PersonID, pr.EffectiveDate, pr.RateID, pr.Rate
>>> from PersonRate pr
>>> inner join (select PersonID, Max(RateID) as MaxRateID
>>> from PersonRate
>>> group by PersonID) pr1
>>> on pr.personid = pr1.personid
>>> and pr.rateid = pr1.maxRateID) pp
>>> on p.PersonID = pp.Personid
>>>
>>> --
>>> -Dick Christoph
>>> "CK" <c_kettenbach@.hotmail.com> wrote in message
>>> news:Z1CUf.58897$Jd.37489@.newssvr25.news.prodigy.n et...
>>>> Good Morning,
>>>> I have a person table with personID. I have a personRate table with
>>>> personID, rateID, and effectiveDate.
>>>> I need to select fields from personRate, but I want the fields from the
>>>> proper record.
>>>>
>>>> I need the one child record that has the most current date of the
>>>> largest rateID.
>>>>
>>>> For example a person may have many rate records. I need the record that
>>>> has the most current date of the largest rateID they have. Does that
>>>> make sense?
>>>>
>>>> I am making a view that has data from both tables. I need to display
>>>> the most current rate info.
>>>>
>>>> Any ideas? TIA ~ CK
>>>>
>>>
>>>
>>
>>
Sunday, February 19, 2012
Help Required Regarding Formatting of Fields
I am using SQL SERVER Reporting Services 2005. I am having a formatting problem. The problem is that I hav a textbox in which numeric field is coming. I want to show the currency format of the Regional Setting of the user pc. I have set the Language setting of the textbox to Default. Now when I am changing the Regional setting from US to UK then in the criteria section the datetime criteria (calendar's value) is changed according to the UK setting, but in the report section the selected date time is not changed (coming in US setting). And the currecy format is also not changing to UK format. Any help ?
Regards,
FurqanSet the format to =User!Language instead of default. I think that will work
help regarding SQL connection
Though i am familiar with both web development languages and SQL, I am having difficulty connecting DB to my web pages(jsp). Can any one help with ODBC/JDBC settings required?
regards
sumit dagar
What kind of difficulties you are experiencing?
For an intro how to use JDBC to connect to SQL server see:
http://msdn2.microsoft.com/en-us/library/ms378672.aspx
|||i wanted to know if I have to change control panel->Administrative Tools->ODBC data sources settings.If yess then what would these settings be.
regards
Sumit Dagar