Tuesday, March 27, 2012
Count of different values
I have a table in which there is a column that can only contain two values: Male and Female.
How should I write a query that will give me a total of both Male and Female?
For example: Male: 23 Female:27
Thanks!
SanderHello Sander,
this is a SQL statement that give you the needed datas in two records
select MaleFemale, count(1) from statistic
group by MaleFemale
Is that good for you ?
Greetings
Manfred Peter
(Alligator Company)
http://www.alligatorsql.com
Sunday, March 25, 2012
Count from multiple tables
One is a user table and the other three contain records for the users. They all have a USERNAME column
I would like to get a count of records for each table grouped by USERNAME
My output would be:
username,totalFrom1,totalFrom2,totalFrom3
Thanks For the help!Mybe something like:
select|||Thanks! I would never have figured that one out on my own, but I see how it works.
username,
isnull(t1.ttlfrom1,0) ttlfrom1,
isnull(t2.ttlfrom2,0) ttlfrom2,
isnull(t3.ttlfrom3,0) ttlfrom3
from <users> u
left join
(
select username, count(username) ttlfrom1
from
<t1>
group by username
) t1 on t1.username = u.username
left join
(
select username, count(username) ttlfrom2
from
<t2>
group by username
) t2 on t2.username = u.username
left join
(
select username, count(username) ttlfrom3
from
<t3>
group by username
) t3 on t3.username = u.username
Thanks again
Greg
Friday, February 24, 2012
'could not be converted because of a potential loss of data'
I have a FoxPro dbf that includes From Milepost (f_mp) and To Milepost (t_mp) fields. These fields contain values between -1 and 9999.9999.
I don't have FoxPro installed, but when I attach the dbf to Access, I see the fields defined as datatype Double.
I have a SQL table that I'm trying to import the dbf data into. In that table the two fields are defined as datatype Real.
When I execute the task, it fails at the first milepost value with 4 digits to the left of the decimal point.
I read up on datatypes, then redefined the milepost fields as Floats, but nothing changed.
Any ideas or suggestions would be greatly appreciated.
ginnyk
Which component is givng the error message? If it's the OLE DB Source adapter, you can change its output column data type for the milepost fields that would allow a lossless conversion. If you can't find a suitable type, as a last resort, you could try converting the data to string.|||Ted,
The full error text is:
"[SQL DcsEx [574]] Error: There was an error with input column "t_mp" (646) on input "OLE DB Destination Input" (587). The column status returned was: "The value could not be converted because of a potential loss of data.".
No matter which FoxPro Ex source dbf I use, the task fails at the destination input, on the first record that has a number greater than or equal to 1000...specifically - 1022, 1341.76 and 1002.849. The 4 digits left of the decimal in the erroring field is the only thing the 3 records seem to have in common.
I have other tasks in the same package that convert Fox doubles to SQL real or float numbers, greater than 1000, with no trouble. I am completely stumped and don't know where to look next.
|||Could you give us more information on what your package actually does with this data. Do you only copy or have some transforms as well? Take a look at data types used in SSIS components for the "t_mp" column.
Thanks.
Tuesday, February 14, 2012
corrupted values in database
Many field contain numerical values stored as varchar. To make matters worse these strings contain both '.' and ',' as decimal sign.
So far this has really been a great pain :-(
The database and its application are bought as is, I cannot modify fieldtypes or anything else.
Can anyone give a good strategy to convert the strings back to numerical so I can calculate revenue's and such??
P.S.
In extreme cases string contain both ',' AND '.' , like:
5.608,42Use
convert (float, replace(column_name,',',''))
or convert (int,replace(replace(column_name,',',''),'.','') if you are sure the value is an integer|||Note quite what I need,
The string can contain either a . or a ,
With the conversion you give all figures with a ',' come out 100 fold higher than they should be, while those with a '.' come out right.
Some sort of validation seems to be needed to get all them right.
Right now I am focussing on prices which means the highest value is just 900.00 (or 900.00) so I have no problem in this column with both a ',' and a '.'|||The following seems to work:
SELECT
CASE
WHEN
SUBSTRING(REVERSE(RTRIM(LTRIM(PRIJS))),3,1) = ',' THEN
convert(float,replace(PRIJS,',',''))/100
WHEN
SUBSTRING(REVERSE(RTRIM(LTRIM(PRIJS))),3,1) = '.' THEN
convert(dec(9,2),PRIJS)
ELSE 0 END
not getting very good feelings though on having to do these king of conversions :-(|||No, me either! You should consider to clean your data before reporting them. You are not allowed to change the field type, but you should be allowed to update your data. So, define your required format, and update everything, that does not match your format, first. Your format may be "#.###,##" or "####.##"; I would prefer the last, because that can be converted into a number directly. You may even consider to convert everything to cents, which makes it easy for you to detect new values, that are not yet converted.
Another approach would be to identify your different formats, and make a union query, handling each of your formats in a separate branch.|||Can't do that.
Some of the fields are filled by procedures from the AS400 system.
THere is something fundamentaly wrong with the way the data is handled on the SQL database. (For which I just mailed an angry mail to all involved) ,
but doctor maybe you can help me with this one:
I have all the differnent flavors in the DB:
1.203,56
1,203.56
456,67
456.67
You see replacing the comma is okay if it comes as first one in the string.
If there is just a comma replace it by a '.'
If a '.' precedes a ',' replace ','by '.' and get rid of the '.'
Nice challenge ain't it?
Next private message next week, gotta cycle to the north this afternoon
:-)|||No, me either! You should consider to clean your data before reporting them. You are not allowed to change the field type, but you should be allowed to update your data. So, define your required format, and update everything, that does not match your format, first. Your format may be "#.###,##" or "####.##"; I would prefer the last, because that can be converted into a number directly. You may even consider to convert everything to cents, which makes it easy for you to detect new values, that are not yet converted.
I dont uderstand the format "#.###,##" . What does it exactly mean ?|||Originally posted by blom0344
Can't do that.
Some of the fields are filled by procedures from the AS400 system.
THere is something fundamentaly wrong with the way the data is handled on the SQL database. (For which I just mailed an angry mail to all involved) ,
I understand, that you don't have control of the import process, but who is preventing you from cleaning your data afterwards?
Originally posted by blom0344
I have all the differnent flavors in the DB:
1.203,56
1,203.56
456,67
456.67
You see replacing the comma is okay if it comes as first one in the string.
If there is just a comma replace it by a '.'
If a '.' precedes a ',' replace ','by '.' and get rid of the '.'
Nice challenge ain't it?
As I said, determine first your possible flavors. What is imported when your price is 456.- or 456.50? Is is 456[,|.]00 or just 456? So, actually I'm asking whether your decimal point is always on the 3rd last position? Your algorithm depends on that. Another point is whether you can expect a maximum number of digits, or not. So, can you also have, for example 1,234,567.89?
Assuming, your possible flavors are those 4 you gave me, a query like I propose would look like (your price field is called p)
SELECT p FROM T WHERE len(p)=6 AND SubString(p, 4,1)="."
UNION
SELECT replace(p,',','.') FROM T WHERE len(p)=6 AND SubString(p, 4,1)=","
UNION
SELECT replace(p,',','') FROM T WHERE len(p)=8 AND SubString(p, 6,1)="."
UNION
SELECT replace(replace(p,'.',''), ',','.') FROM T WHERE len(p)=8 AND SubString(p, 6,1)=","
Calling this union query U you can do your accumulation like
SELECT sum(cast(p as decimal(10,2))) from (<U>)|||-- replace(replace(p,'.',''), ',','.') --
Yep,
I am working with the replace on replace in my solution as well.
Does not seem to work like it should.
I want to use the CASE instead of UNION solution , cause I am going to assign it to a BO object
We'll continue next week...|||If you know you have only two decimal places, how about something along the lines of eliminating all commas/periods, then dividing by 100?
select convert(numeric (10, 2), replace(replace(value, ',', ''), '.', '')))/100.00
A bit pressed for time here, so I have not tested that code snippet. Hope it helps.|||Elegant solution, MCrowley. :cool:
...but make sure the values don't just only have two decimal places, but that the ALWAYS have two decimal places.
blindman|||Originally posted by blom0344
I just started working on reports on an SQL 2000 database.
Many field contain numerical values stored as varchar. To make matters worse these strings contain both '.' and ',' as decimal sign.
So far this has really been a great pain :-(
The database and its application are bought as is, I cannot modify fieldtypes or anything else.
Can anyone give a good strategy to convert the strings back to numerical so I can calculate revenue's and such??
P.S.
In extreme cases string contain both ',' AND '.' , like:
5.608,42
Dumb question ... but, any chance you are dealing with software that handles multi-currency? Do you have the CCSID translation turned on from the AS400 to the SQL server?|||Most certainly not a dumb question, but in this case we are talking about E-commerce orders from two euro countries, so every order amount is always just in one currency.
McCrowleys solution does indeed work for me, cause every order is calculated down to the euro-cent