Tuesday, March 27, 2012
Can't get MAX DISTINCT to work the way I want it to
SupervisorLineID (autoincrementing ordinal), SupervisorID and LineID.
SupervisorLineID is the only definite unique value; there are cases where
one supervisor can manage more than one production line, and thus be
represented many times in the table. There are also cases where more than
one supervisor can manage a line, complicating things further.
What I need to return is one unique record for each LineID, with the highest
SupervisorLineID number as the selecting factor, along with the matching
SupervisorID. I tried using MAX DISTINCT on the SupervisorLineID, but oddly
enough it caused the opposite of what I expected: all supervisors were
returned EXCEPT the one represented by the max SupervisorLineID per line!
This caused lines to appear more than once in many cases.
I've tried various approaches and none produce the desired results. I know
this has to be possible-- any ideas?
Thanks,
Randall ArnoldRandall Arnold wrote:
> I've tried various approaches and none produce the desired results.
> I know this has to be possible-- any ideas?
Please provide DDL and some testdata, you are talking about more than 1
table I presume? We can't guess all the tablestructures.
Kind regards,
Stijn Verrept|||Hi
Since you have not posted a table stucture + sample data I did some testing
on Northwind database and Orders table
SELECT * FROM Orders
WHERE OrderDate=(SELECT MAX(OrderDate) from Orders O
WHERE O.CustomerId=Orders.CustomerId)
Getting highest OrderDate for each Customer
"Randall Arnold" <randall.arnold@.nokia.com> wrote in message
news:OwClf.16116$Nb2.285732@.news1.nokia.com...
>I have 3 numeric fields in a table that need to be returned by a query:
>SupervisorLineID (autoincrementing ordinal), SupervisorID and LineID.
>SupervisorLineID is the only definite unique value; there are cases where
>one supervisor can manage more than one production line, and thus be
>represented many times in the table. There are also cases where more than
>one supervisor can manage a line, complicating things further.
> What I need to return is one unique record for each LineID, with the
> highest SupervisorLineID number as the selecting factor, along with the
> matching SupervisorID. I tried using MAX DISTINCT on the
> SupervisorLineID, but oddly enough it caused the opposite of what I
> expected: all supervisors were returned EXCEPT the one represented by the
> max SupervisorLineID per line! This caused lines to appear more than once
> in many cases.
> I've tried various approaches and none produce the desired results. I
> know this has to be possible-- any ideas?
> Thanks,
> Randall Arnold
>|||Randall wrote on Wed, 07 Dec 2005 14:41:50 GMT:
> I have 3 numeric fields in a table that need to be returned by a query:
> SupervisorLineID (autoincrementing ordinal), SupervisorID and LineID.
> SupervisorLineID is the only definite unique value; there are cases where
> one supervisor can manage more than one production line, and thus be
> represented many times in the table. There are also cases where more than
> one supervisor can manage a line, complicating things further.
> What I need to return is one unique record for each LineID, with the
> highest SupervisorLineID number as the selecting factor, along with the
> matching SupervisorID. I tried using MAX DISTINCT on the
> SupervisorLineID, but oddly enough it caused the opposite of what I expect
ed:
> all supervisors were returned EXCEPT the one represented by the max
> SupervisorLineID per line! This caused lines to appear more than once in
> many cases.
> I've tried various approaches and none produce the desired results. I
> know this has to be possible-- any ideas?
> Thanks,
> Randall Arnold
DISTINCT applies to an entire row, not a single column.
Without DDL I've had to make some assumptions.
I think this might work ...
SELECT A.LineID, A.SLID, B.SupervisorID FROM
(SELECT LineID, MAX(SupervisorLineID) AS SLID FROM Table GROUP BY LineID) AS
A)
INNER JOIN Table B ON A.SLID = B.SupervisorLineID AND A.LineID = B.LineID
There's probably an easier way to write this.
Dan|||Actually it's only one table, and I provided the structure (field list) in t
he first post. That's really all there is to the table. Sorry if I didn't
make that clear, but I would have listed other tables if any were involved.
Test data would be as follows:
RecentLineSupervisor_View SupervisorID SupervisorLineID LineID
18 9804 1
108 9913 1
128 -->9964 1
7 9715 19
11 9752 19
118 -->9894 19
1 9831 20
24 9688 20
108 -->9927 20
6 9782 22
77 9771 22
78 -->9978 22
As I said, I just want to see each LineID represented once, with the highest
value for SupervisorLineID determining which record is returned. Desired re
cords marked with arrows.
Thanks,
Randall Arnold
"Stijn Verrept" <stjin@.entrysoft.com> wrote in message news:YLWdnelcNs6FZAveRVnyig@.scarlet.
biz...
> Randall Arnold wrote:
>
>
> Please provide DDL and some testdata, you are talking about more than 1
> table I presume? We can't guess all the tablestructures.
>
> --
>
> Kind regards,
>
> Stijn Verrept|||Randall Arnold wrote:
> I have 3 numeric fields in a table that need to be returned by a query:
> SupervisorLineID (autoincrementing ordinal), SupervisorID and LineID.
> SupervisorLineID is the only definite unique value; there are cases where
> one supervisor can manage more than one production line, and thus be
> represented many times in the table. There are also cases where more than
> one supervisor can manage a line, complicating things further.
>
If those are the only three columns then the combination of
(supervisorid, lineid) should be unique and declared as such -
otherwise your table is full of redundant garbage. Therefore the answer
would be:
SELECT supervisorid, lineid
FROM your_table AS T
WHERE supervisorlineid =
(SELECT MAX(supervisorlineid)
FROM your_table
WHERE lineid = T.lineid) ;
However, you then seem to be breaking the golden rule of using an
IDENTITY column - don't assign any business significance to it. In
future, please post DDL and sample data so that we don't have to guess
your requirements.
David Portas
SQL Server MVP
--|||Please don't assume I designed this table; rest assured I would NOT have
taken this approach! I'm stuck with trying to extract meaningful
information from a previous employee's cowboy database. In fact, the only
reason I have to create this query at all is to select a single
supervisor-to-line record for each line, and that ability *should* have been
designed into the structure. Had the former data manager done his job
properly, I wouldn't be dealing with this.
As for DDL and structure, it seemed to me I posted enough info. As I told
another gentleman, had there been another table involved I would have
indicated so. I also figured that listing the only 3 fields involved and
describing their type and role would cover it; note that everyone who
provided a suggestion based on a single table was on the right track so it
seems to me it wasn't TOO confusing. But I'll try to be more pedantic in
the future <g>.
Anyway, it looks like one of the methods listed here will work. Thanks all,
Randall Arnold
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1133968276.805046.224980@.o13g2000cwo.googlegroups.com...
> Randall Arnold wrote:
> If those are the only three columns then the combination of
> (supervisorid, lineid) should be unique and declared as such -
> otherwise your table is full of redundant garbage. Therefore the answer
> would be:
> SELECT supervisorid, lineid
> FROM your_table AS T
> WHERE supervisorlineid =
> (SELECT MAX(supervisorlineid)
> FROM your_table
> WHERE lineid = T.lineid) ;
> However, you then seem to be breaking the golden rule of using an
> IDENTITY column - don't assign any business significance to it. In
> future, please post DDL and sample data so that we don't have to guess
> your requirements.
> --
> David Portas
> SQL Server MVP
> --
>|||Randall Arnold wrote:
> Please don't assume I designed this table; rest assured I would NOT have
> taken this approach! I'm stuck with trying to extract meaningful
> information from a previous employee's cowboy database. In fact, the only
> reason I have to create this query at all is to select a single
> supervisor-to-line record for each line, and that ability *should* have be
en
> designed into the structure. Had the former data manager done his job
> properly, I wouldn't be dealing with this.
> As for DDL and structure, it seemed to me I posted enough info. As I told
> another gentleman, had there been another table involved I would have
> indicated so. I also figured that listing the only 3 fields involved and
> describing their type and role would cover it; note that everyone who
> provided a suggestion based on a single table was on the right track so it
> seems to me it wasn't TOO confusing. But I'll try to be more pedantic in
> the future <g>.
> Anyway, it looks like one of the methods listed here will work. Thanks al
l,
> Randall Arnold
>
In the absence of the DDL it's reasonable to assume that we lack the
information about the alternate key(s) because that information was so
conspicuously absent from your post. I didn't assume you designed it. I
assumed you'd want to fix it.
David Portas
SQL Server MVP
--|||> As for DDL and structure, it seemed to me I posted enough info.
That's all fine and good. However, in order for us to provide you with a
meaningful, accurate and testable solution, instead of guessing, we ask for
a bit more. I don't understand where the vehement objection to providing
proper specs comes from. The prevailing opinion seems to be that we are
lazy and are trying to be a pain in the ass. Nothing could be further from
the truth... please read http://www.aspfaq.com/5006 before assuming that
what you provide should be enough for anyone to solve the problem.|||There was no "vehement objection" to providing proper specs, Aaron. Just a
minor goof IMO on my part based on personal tendencies (ie, omitting
reference to what *isn't* there such as other tables). I don't understand
where this perception of a "vehement objection" comes from. Certainaly not
from anything I've posted! Personally, I'm all too eager to provide as much
detail as possible. Again, I *thought* I had. Mea culpa. But no need for
a hangin', please. The verbal beating has been enough.
This is a silly axle anyway. I gotta unwrap myself and get back to work
<g>.
Randall Arnold
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:eyRFaW0%23FHA.3676@.tk2msftngp13.phx.gbl...
> That's all fine and good. However, in order for us to provide you with a
> meaningful, accurate and testable solution, instead of guessing, we ask
> for a bit more. I don't understand where the vehement objection to
> providing proper specs comes from. The prevailing opinion seems to be
> that we are lazy and are trying to be a pain in the ass. Nothing could be
> further from the truth... please read http://www.aspfaq.com/5006 before
> assuming that what you provide should be enough for anyone to solve the
> problem.
>
Tuesday, March 20, 2012
Cant Export Fields w/ over 255 characters
2000 characters and it is truncated. What is not setup in MS SQL
correctly so that DTS will export the csv correctly.
Thanks,
John<quinniii@.yahoo.com> wrote in message
news:1109013962.276779.70380@.o13g2000cwo.googlegro ups.com...
>I have a query where I am trying to export a column that has around
> 2000 characters and it is truncated. What is not setup in MS SQL
> correctly so that DTS will export the csv correctly.
> Thanks,
> John
This might be the issue:
http://www.sqldts.com/?297
If that doesn't help, then I suggest you post more information - what
version of MSSL, what data source and target types do you have, what sort of
task/transformation are you using to move the data etc.
Simon
Monday, March 19, 2012
cant do that request
I'm trying to do a request but I can't figure out how to do it.
Basically I have 1 table with 3 fields (field1,field2,field3).
Values in those 3 fields can be the same, but I want to select all of
these values only once (i.e distinct values)
Example
Field1 Field2 Field3
A A A
B E B
C F F
D G H
The select should return: A,B,C,D,E,F,G,H
I've tried with union and a select not in but I ended up with something
big and I got confused.
Can someone help ?
ThxSELECT col1
FROM YourTable
UNION
SELECT col2
FROM YourTable
UNION
SELECT col3
FROM YourTable
If that doesn't give the result you wanted then please post DDL and
sample data as described here:
http://www.aspfaq.com/etiquette.asp?id=5006
--
David Portas
SQL Server MVP
--|||Seems to work. Thanks !
Regarding the DDL, I've looked at it but I post my messages via the
google group thing and I'm not sure if I can upload attached stuff ?|||Glad it helped.
Put DDL and other scripts in the body of your message. Most people
can't or won't download attachments anyway.
--
David Portas
SQL Server MVP
--
Saturday, February 25, 2012
Cant create a foreign key using non_primary key fields
I'm just a bit on the frustrated side cause I want to create a foreign key but SQL Server won't let me. :(
I have table A with a primary key of main_id
I have table B with a primary key of another_id
Table A has a corresponding field called another_id.
I'm trying to create a foreign key between Table A & Table B on another_id but since it's not the Primary Key in Table A I get the following error:
There are no primary or candidate keys in the referenced table 'A' that match the referencing column list in the foreign key 'fk_classB_classA'.
Am I missing something totally obvious here? Why should I have to create a foreign key on a primary key?
What I find interesting is that I can create the relationship from enterprise manager but when I script it out is when I run into problems.
Here's the script I'm using:
Begin Code
alter table B add
constraint fk_classB_classA foreign key
(classB) references A (classB)
on delete no action
on update no action
End Code
Any help is greatly appreciated.
tamAdd a UNIQUE constraint and then SQL Server will oblige. It won't let you create an FK relationship to a column that could have more than one row with the "target" value, due to the chaos that can cause.
-PatP|||Hey Pat,
Many thanks for the quick response and I'll give that a try, but could you explain to me why it does it from enterprise manager? If I go into diagrams and drag and drop from one table to the other it comes up as a foreign key not a unique constraint.
Is it actually creating a unique constraint behind the curtain and labeling it as a Foreign key?
I'm more curious than anything. To me it just doesn't make much sense.
Again, thanks for the answer.
tam|||I don't think EM is actually creating FK relationship on the columns that you think it does. Right-mouse click on the relationship and select Properties, and see what fields participate in the relationship.|||The two ideas (UNIQUE CONSTRAINT and Foreign Key) are related, but they are decidedly not the same thing.
A unique constraint means that the column(s) that the constraint applies to are unique within the table, only one row can exist with a particular value. As an example, you might have an employee table that has an EmployeeID column which is the Primary Key, and an SSN column that is Unique. There can be only one row with any given value of EmployeeID, and NULL values are never allowed. There can be NULL values for SSN (which is good, since some employees may not have one), but the database engine won't allow two rows to have the same value.
Various tables in your schema would link to your employee table using the EmployeeID as a Foreign Key. You might get a table from the IRS that has employee data organized by SSN, and you could make that SSN a Foreign Key to the employee table too (because it has a Unique constraint). Not every employee may have this detail data, but the detail can apply to only one employee.
You might get data from a shoe manufacturer telling you about their schwell new work shoes. Even if they provide information by shoe size, you can't make the shoe size a foreign key to employee, because in the employee table the shoe size column couldn't have a unique constraint (at least not in most companies anyway!).
-PatP|||UNIQUE constraint on SSN? Then there may be only 1 employee without a valid unique SSN, because UNIQUE constraint will allow only 1 NULL-valued row for that field.|||Well I'll be horny-swoggled! I'd forgotten that they'd changed that. Good catch!
Ok, so much for my sterling example then... It used to hold water, long ago and far away!
-PatP