Monday, March 26, 2012
Memo fields from Access is empty in SQL Server
I have a DTS packet where I import data from a Access database to SQL Server.
But when I try to import data in memo fields in Access to a nvarchar field in a table on SQL server, there is no data imported.
Anyone know how to solve this problem?Not positive on this one, but I would try to map a memo field in access to a TEXT field in SQL. It should be pretty straightforward to test.
Regards,
Hugh Scott
Memo field in SQL Server 2005
I'm trying to create a site which allows me to add Memo type fields but when I insert or edit my record it will not take any of my text after an enter (vbNewLine).
In Access I used the field type "Memo" but I do not see that type in SQL Server 2005 just a nvarchar(max) which does not seam to work.
Thanks for the help!
Chad
Are you trying to enter the data directly or through a query in query analyzer. There is no Memo field in SQL 2005. varchar should be sufficient. Whether you need varchar(max) depends on how much data you want to put into it.|||I tried directly in SQL Server and I also tried through my edit form in my application.
No work!
|||If you are doing it from your application, and assuming you have some kind of textbox for users to enter data, you can replace linebreaks with vbcrlf when you isnert the data into the table.
txtBox1.text.replace(environment.newline,"<br>")
and do the reverse when you need to show the data back to the user.
|||How would you do that in ASP.net when I'm using a FormView to update and insert?|||Whether its update or insert, you have to pass the value the user entered to the proc/T-SQl. Instead of passing the value directly you can use the code I provided.|||I'm sorry, I'm still not sure where you would add your code...I actually have used the replace alot in ASP but .net is a little diferent.
Do you add this in the code behind somewhere or in the aspx page?
again sorry for asking so much!
|||Yes it would go in your code behind. What I provided is .NET code. I assume for insert/update you prbly have the event code for the respective action.sql
Friday, March 23, 2012
median query without using function
VIN, Class, sell_price
101, sports, 10000
102, sports, 11000
103, luxury, 9000
104, sports, 11000
105, sports, 11000
106, luxury, 5000
107, sports, 11000
108, sports, 11000
109, luxury, 9000
i need to write a query that WITHOUT USING A FUNCTION will return the
median selling price for each class of car. result should look like:
Class, Med_Price
luxury, 9000
sports, 11000
thanks to all u SQLers>From your sample data it looks like you want the most commonly
occurring price for each vehicle (I thought this was
called the "mode", not the "median").
CREATE TABLE Cars(VIN INT,Class VARCHAR(10),sell_price INT)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(101, 'sports', 10000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(102, 'sports', 11000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(103, 'luxury', 9000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(104, 'sports', 11000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(105, 'sports', 11000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(106, 'luxury', 5000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(107, 'sports', 11000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(108, 'sports', 11000)
INSERT INTO Cars(VIN,Class,sell_price) VALUES(109, 'luxury', 9000)
GO
CREATE VIEW CarNums
AS
SELECT Class,sell_price,COUNT(*) as Num
FROM Cars
GROUP BY Class,sell_price
GO
SELECT c1.Class,c1.sell_price AS Med_Price
FROM CarNums c1
INNER JOIN (
SELECT Class,MAX(Num)
FROM CarNums
GROUP BY Class) C2(Class,Num) ON C2.Class=C1.Class
AND C2.Num=C1.Num|||My mistake, you can get the medians using this
CREATE VIEW CarRank
AS
SELECT c1.VIN,
c1.Class,
c1.sell_price,
(SELECT COUNT(*) FROM Cars c2
WHERE c2.Class=c1.Class
AND ((c2.sell_price<c1.sell_price)
OR (c2.sell_price=c1.sell_price AND c2.VIN<=c1.VIN))) as
Rank,
(SELECT COUNT(*) FROM Cars c2
WHERE c2.Class=c1.Class) as MaxRank
FROM Cars c1
GO
SELECT Class,AVG(sell_price) AS Med_Price
FROM CarRank
WHERE Rank IN ((MaxRank+1)/2,(MaxRank/2)+1)
GROUP BY Class|||I have a whoel chapter on verious ways to do this in SQL FOR SMARTIES.
Here is one answer.
Median with Characteristic Function
Anatoly Abramovich, Yelena Alexandrova, and Eugene Birger presented a
series of articles in SQL Forum magazine on computing the median (SQL
Forum 1993, 1994). They define a characteristic function, which they
call delta, using the Sybase sign() function. The delta or
characteristic function accepts a Boolean expression as an argument and
returns a 1 if it is TRUE and a zero if it is FALSE or UNKNOWN.
In SQL-92 we have a CASE expression, which can be used to construct the
delta function. This is new to SQL-92, but you can find vendor
functions of the form IF...THEN...ELSE that behave like the condition
expression in Algol or like the question markPcolon operator in C.
The authors also distinguish between the statistical median, whose
value must be a member of the set, and the financial median, whose
value is the average of the middle two members of the set. A
statistical median exists when there is an odd number of items in the
set. If there is an even number of items, you must decide if you want
to use the highest value in the lower half (they call this the left
median) or the lowest value in the upper half (they call this the right
median).
The left statistical median of a unique column can be found with this
query:
SELECT P1.bin
FROM Parts AS P1, Parts AS P2
GROUP BY P1.bin
HAVING SUM(CASE WHEN (P2.bin <= P1.bin) THEN 1 ELSE 0 END)
= (COUNT(*) + 1) / 2;
Changing the direction of the theta test in the HAVING clause will
allow you to pick the right statistical median if a central element
does not exist in the set. You will also notice something else about
the median of a set of unique values: It is usually meaningless. What
does the median bin number mean, anyway? A good rule of thumb is that
if it does not make sense as an average, it does not make sense as a
median.
The statistical median of a column with duplicate values can be found
with a query based on the same ideas, but you have to adjust the HAVING
clause to allow for overlap; thus, the left statistical median is found
by
SELECT P1.weight
FROM Parts AS P1, Parts AS P2
GROUP BY P1.weight
HAVING SUM(CASE WHEN P2.weight <= P1.weight
THEN 1 ELSE 0 END)
>= ((COUNT(*) + 1) / 2)
AND SUM(CASE WHEN P2.weight >= P1.weight
THEN 1 ELSE 0 END)
>= (COUNT(*)/2 + 1);
Notice that here the left and right medians can be the same, so there
is no need to pick one over the other in many of the situations where
you have an even number of items. Switching the comparison operators in
the two CASE expressions will give you the right statistical median.
The author's query for the financial median depends on some Sybase
features that cannot be found in other products, so I would recommend
using a combination of the right and left statistical medians to return
a set of values about the center of the data, and then averaging them,
thus:
SELECT AVG(P1.weight)
FROM Parts AS P1, Parts AS P2
HAVING (SUM(CASE WHEN P2.weight <= P1.weight -- left median
THEN 1 ELSE 0 END)
>= ((COUNT(*) + 1) / 2)
AND SUM(CASE WHEN P2.weight >= P1.weight
THEN 1 ELSE 0 END)
>= (COUNT(*)/2 + 1))
OR (SUM(CASE WHEN P2.weight >= P1.weight -- right median
THEN 1 ELSE 0 END)
>= ((COUNT(*) + 1) / 2)
AND SUM(CASE WHEN P2.weight <= P1.weight
THEN 1 ELSE 0 END)
>= (COUNT(*)/2 + 1));
An optimizer may be able to reduce this expression internally, since
the expressions involved with COUNT(*) are constants. This entire query
could be put into a FROM clause and the average taken of the one or two
rows in the result to find the financial median. In SQL-89, you would
have to define this as a VIEW and then take the average.
If you have SQL-2005, you can try something like (untested):
SELECT AVG(x),
ROW_NUMBER () OVER (ORDER BY x ASC) AS hi,
ROW_NUMBER () OVER (ORDER BY x DESC) AS lo,
FROM Foobar
WHERE hi IN (lo, lo-1, lo+1);|||i don't do homework for college students.
Median calculation
I want to calculate the MEDIAN time interval between the two date field. In an OLAP cube one of the dimension consists of the two date fields as well as Interval(days) field (calculated at the data source view). How to create a Measure in the OLAP cube to calculate the MEDIAN Interval?
MEDIAN( <<Set>>[, <<Numeric Expression>>])
Since I'm not sure of the details your dimension, here's an Adventure Works sample query, for the Median duration in days of a Promotion:
>>
With Member [Measures].[MedPromoDays] as
Median(existing [Promotion].[Promotion].[Promotion],
DateDiff("d", [Promotion].[Promotion].Properties("Start Date", TYPED),
[Promotion].[Promotion].Properties("End Date", TYPED)))
select {[Measures].[MedPromoDays]} on 0,
[Promotion].[Promotion Type].Members on 1
from [Adventure Works]
MedPromoDays
All Promotions 83.5
Discontinued Product 53
Excess Inventory 61
New Product 91
No Discount 1309
Seasonal Discount 30
Volume Discount 1095
>>
|||And to slightly change Deepak's query to be more efficient:
With Member [Measures].[MedPromoDays] as
Median(existing [Promotion].[Promotion].[Promotion],
DateDiff("d", [Promotion].[Start Date].MemberValue,
[Promotion].[End Date].MemberValue))
select {[Measures].[MedPromoDays]} on 0,
[Promotion].[Promotion Type].Members on 1
from [Adventure Works]
Wednesday, March 21, 2012
Measures dependant on another measure.
I have a fact table that has sales data for products.
I have to fields in my fact table, WantDateId and InvoicedDateId which are integer representation of a date that join to the time dimension table.
I also have measures for feet, pounds, sales dollars, and material cost.
I have a bunch of dimensions for product type, sales rep, territory, shipped date…
I my cube I want to show a picture of sales for three things broken down over the dimensions.
What has been ordered
What has been invoiced
What is outstanding (backlog).
Most important is the current month where some will be shipped and some will be backlog.
The key here is that if the invoiceDateId = 0 then it has not shipped.
In order for aggregations to work, do I have to add columns to my fact table?This would mean I would need three of each measure.Is there a way to get this with calculated measures?
Hello. Here you have some links to design advices for your question.
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2000%20/KimballDT2MultipleTime.pdf
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2002/KimballDT37ModelingPipeline.pdf
http://www.kimballgroup.com/html/designtipsPDF/DesignTips2003/KimballDT42Combining.pdf
http://www.kimballgroup.com/html/articlesArchitecture/articlesAdvancedFact.html
I am not sure of what you would like to accomplish but sales data and snapshots, like OrderStock are normally not included in the same fact table.
HTH
Thomas Ivarsson
Monday, March 19, 2012
MDX: sum with condition on 2 fields
Hello,
I have a table which contains 2 fields, "Contract Number" and "CN Number".
When the 2 fields are equals I want to do the difference between the price link to the contract number - the price link to the CN Number.
Moreover the Contract Number and the CN number are never the same on the same record.
Here is an example:
Contract Number / CN Number / Price
...
1000A 100
....
1B 1000A 30
....
So for the Contract Number 1000A, I want 100 - 30 = 70
I don't know how to do that in MDX...
Any idea is welcome.
Thanks,
Guillaume
i would never do this in mdx because dependend on your requirements it may be really difficult or impossible, but sure slow.
why you not calculate the differnce by SQL and add the result as a new measure. (hidden if required)- it fast compared to mdx
best regards
HANNES