Saturday, February 25, 2012
Magic Date?
If you put that date with a time in sql server EM and a vb call will return only the time portion...
More background and sample code
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30709In vb, the date is a floating-point where the integer portion is the date. 12/30/1899 is the base date. So if you enter a time only, the integer portion of the datetime will be 0 and the decimal portion will be the time.|||What's EM Built in? If you just enter a time through EM, it'll ne 1899-12-30...
And isn't 1900-01-01 the 0 date for sql server?
SELECT CONVERT(datetime,0,101)
???
And why, if you call sql server from vb through ado, does it pass back just the time component...with no conversion function?|||I wonder if there is a magic time, that just returns the date...
Why the original developer didn't use CONVERT is betond me...|||Do a query against that datetime column that only has a time and add 1 to it - your question will be answered.|||SELECT CONVERT(datetime,-2,101)
gives '1899-12-30 00:00:00.000'|||Originally posted by rnealejr
Do a query against that datetime column that only has a time and add 1 to it - your question will be answered.
What does that mean?
Datetime is stored as a number...4 before the decimal, 4 after...
What do you mean time only?|||So what does this prove?
SELECT DATEADD(d,1,CONVERT(datetime,0.1))
Add 1 what?
What do you mean with no date? There's always a date component?
0 is the default
(Unless of course you add through EM then it's -2)
huh?
And why, if you make a sql call from vb, and the date is 1899-12-30, it only returns the time..VERY bizzare|||Just had the developer do it from an Excel workbook too.
It puts just the time in the Cell with that 1899-12-03 date
It doesn't return the date...bizzaro
Anyone else seen this?|||http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27101
Hey Brett ... some more with excel problems ...
I believe the reason is that while SQL takes the default date to be 1900-01-01
and other MS applications use 1899-12-30
Here is a link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/whatisdatehowdiditgetthere.asp|||Thanks...forgot all about that thread...
Still doesn't explain you only get the decimal portion of the datetime field though (that's the time component)
Monday, February 20, 2012
Lowest Cost
I have a table, we'll call Table1 like so:
ProductID Supplier Cost
12345 A 14.50
12345 B 13.49
12345 C 12.00
43222 A 15.00
43222 B 15.21
43222 C 13.99
12312 B 14.00
15421 A 21.99
15421 C 20.00
And I want to Get the name of the Supplier with the Lowest cost,
I know I can go like:
SELECT ProductID, MIN(Cost) FROM Table1 GROUP BY ProductID
and get the lowest cost, but what would be the most effiecent way to get all three fields returned by the query? I need the ProductID, Supplier and Cost.
Thanks,
|||Thanks, that was what I was looking for. I'm still getting duplicate records if there are two suppliers with the same lowest cost for a product. But thats something I can work out.
select d.productid, t.supplier, d.cost
from table1 t
join
(
select productid, min(cost) cost
from table1
group by productid) d on d.productid = t.productid and d.cost = t.cost
Thanks.|||Just throw a DISTINCT on the outer query|||The extra distinct wont eliminate multiple suppliers with matching lowest prices
try adding a Top 1 to the outer query|||>>Just throw a DISTINCT on the outer query
>>>The extra distinct wont eliminate multiple suppliers with matching lowest prices
try adding a Top 1 to the outer query
You are correct mbanavige, I misread the problem to be the same supplier with multiple min(costs).
Though, TOP 1 will not do it either
DeoDev, I am having a hard time understanding why supplier has any relevance then in this resultset??|||Its for a ordering system, where it will automatically order products that are out of stock from the supplier who has the lowest price, so I need to know who has the lowest price so I can place the order with them, and I'd prefer to return the information in one table.
But with having two suppliers with the same lowest cost, It doesn't matter which one to order from out of the two. I think I have to do a little bit more thinking on the design of this function.|||I see.
Though I would hate to be the supplier whose name doesn't get returned in this result
:)