Showing posts with label figure. Show all posts
Showing posts with label figure. Show all posts

Thursday, March 22, 2012

DISTINCT doesnt seem to be working

Hi all,

I am using SQLEXPRESS 2005 and can't figure out why my SQL query is not working.

Select DISTINCT ads.clientid, ads.ad, ads.adid, ads.cost, ads.description, ads.bold, ads.info, ads.contact, ads.coupon, ads.logo, ads.map, ads.webpage, ads.weburl, tclients.clientname, tclients.clientphone, tclients.addNumber, tclients.addStreet, tclients.addsuite, tclients.addcity, tclients.addstate, tclients.addzipfrom tclients, adswhere tclients.clientid=ads.clientidAND AdActive=1ORDER BY COSTDESC

I expect only one row to be returned for each ads.clientid. However, every single record that matches the query is being returned, which results in multiple records with the same clientid.

This happens if it isads.clientid ortclients.clientid.

Martin

DISTINCT returns unique rows based on all columns in the query, not just the first column (clientid) that you've specified.|||

Ah - I'm glad it it working as intended and I am not going mad...

How would I go about getting what I wanted then? Just one clientid.

|||

Can't answer that.

You said what you wanted.

You showed the query you have and the columns that you want.

The two don't mix, or the distinct would have worked anyhow. You obviously have multiple records with the same clientid, and differing data. WHICH of the differing data would you like when a clientid has multiple?

Assuming your fingers have names (index,middle,ring,pinky,thumb), and your hands have names (left, right).

Please tell me the length of your finger on each hand. And I only want one 2 entries, one for each hand. And if you are left scratching your head, it's because the question doesn't match the data, and you can't answer it. If I said, tell me the length of the longest finger on each hand, NOW you can answer, or I could say tell me the length of your index finger on each hand, etc.

|||

Thanks for that post - it helped me realize what I am doing wrong in my query.

I assumed because I was ordering by COST that it would take the first record for each clientid. In your example, it would order all my fingers by length, then take the longest one from each hand.

Since I know this is not the case, I know have to figure out how to re-write the query.

|||

Motley,

This SEEMS to work, but I'm not a SQL expert, so I can't be sure it will in real data. Does it look good to you? The bolded part is what I added.

Select DISTINCT ads.clientid, ads.ad, ads.adid, ads.cost, ads.description, ads.bold, ads.info, ads.contact, ads.coupon, ads.logo, ads.map, ads.webpage, ads.weburl, tclients.clientname, tclients.clientphone, tclients.addNumber, tclients.addStreet, tclients.addsuite, tclients.addcity, tclients.addstate, tclients.addzipfrom tclients, adswhere tclients.clientid=ads.clientidAND AdActive=1AND costin (select max(ads.cost)from adsgroup by clientid)ORDER BY COSTDESC

Friday, February 24, 2012

Display Row Data As Cols

I have data in rows that I need to aggregate and display in a columnar fashion and I haven't been able to figure out how to do it. A simplified version of the data as it is stored in the table:

Station Month Day Reading

1 1 1 100

1 1 2 200

1 1 3 300

1 2 1 400

1 2 2 500

And I would like to create a query that returns:

Station Month Day 1 Day 2 Day 3

1 1 100 200 300

1 2 400 500

Any help you can provide or tips to steer me in the right direction are much appreciated.

Dianne Siebold

Are you looking for something like this?

http://msdn2.microsoft.com/en-us/library/ms177410.aspx

|||That is very cool. I'm using SQL Server 2005 but I need to maintain query compatibility with Oracle so I'm looking for something that uses joins or maybe case statements to make it more portable.|||

Then maybe the responses in this thread might be helpful for you

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=438605&SiteID=1

Starting from the second reply by limno.

|||I'm working on using that query logic but I'm just not getting how I can apply it to my query. I'll let you now if I have any success. Thanks!|||I think I got it. I was able to take the concepts from the thread you referred me to and use them in another query. Thanks!

Tuesday, February 14, 2012

display FLOAT datatype with commas?

Is it possible to display a FLOAT datatype with all its commas every
three digits? I can't figure out how to use CAST or CONVERT to do this.
(I know this kind of thing is usually done on the client side, but
that's not possible right now.) Thanks much.here you go
DECLARE @.v float
SELECT @.v = 1322323.6666
SELECT CONVERT(VARCHAR,convert(money,@.v),1)
http://sqlservercode.blogspot.com/|||Why is this a float?
Anyway, see http://tinyurl.com/zlbmw
This is not pretty.
"Rick Charnes" <rickxyz--nospam.zyxcharnes@.thehartford.com> wrote in message
news:MPG.1e7917a698fb998c989921@.msnews.microsoft.com...
> Is it possible to display a FLOAT datatype with all its commas every
> three digits? I can't figure out how to use CAST or CONVERT to do this.
> (I know this kind of thing is usually done on the client side, but
> that's not possible right now.) Thanks much.|||FANTASTIC. Thank you.
Is there a way to right-justify the output? (I know we have character
data now, so it may not be possible?) Thanks.
In article <1141853921.455400.117020@.j52g2000cwj.googlegroups.com>,
denis.gobo@.gmail.com says...
> here you go
> DECLARE @.v float
> SELECT @.v = 1322323.6666
> SELECT CONVERT(VARCHAR,convert(money,@.v),1)
> http://sqlservercode.blogspot.com/
>|||What if it's a bigger FLOAT value than MONEY can support?
DECLARE @.v float
SELECT @.v = 1321324441322323.6666
Also, you should always specify the size of your VARCHAR parameters, IMHO.
"SQL" <denis.gobo@.gmail.com> wrote in message
news:1141853921.455400.117020@.j52g2000cwj.googlegroups.com...
> here you go
> DECLARE @.v float
> SELECT @.v = 1322323.6666
> SELECT CONVERT(VARCHAR,convert(money,@.v),1)
> http://sqlservercode.blogspot.com/
>|||> Is there a way to right-justify the output?
Umm, sure, you could say RIGHT(SPACE(30)+...stuff..., 30)
But what application is consuming this result that can't right-justify ?
Are you going to ask for a way to embed <img> tags next? :-)
T-SQL is not meant to be a presentation language.|||What is the basic principle of a tiered architecture? We do display
formatting in the front end and never in the database. Since FLOAT
can come back as exponential notation, you really don't want to do it
in SQL.|||Brilliant. Thank you VERY much. Much appreciated. I know T-SQL is not
meant to be a presentation language, but for a very quick one-time
temporary project I am forced to use it as such. This is perfect.
Thank you.
In article <OPUigrvQGHA.4956@.TK2MSFTNGP09.phx.gbl>,
ten.xoc@.dnartreb.noraa says...
> Umm, sure, you could say RIGHT(SPACE(30)+...stuff..., 30)
> But what application is consuming this result that can't right-justify ?
> Are you going to ask for a way to embed <img> tags next? :-)
> T-SQL is not meant to be a presentation language.
>
>