|
|
I'm hoping someone can help me with this query that I've been struggling with. I'll use an example to explain my problem.
Let's say I have a table named Orders with the following three fields:
CustID, OrderDescr and OrderDate
Now I want to select from this table the first order (oldest date) for each customer. In other words I want a resulting table that lists one entry for every unique CustID in Orders that shows the oldest order for that CustID.
I was hoping the following SQL query would do the trick but it doesn't.
SELECT * FROM Orders O1 WHERE exists<br />
(SELECT TOP 1 * from Orders O2 WHERE O1.CustID = O2.CustID AND O1.OrderDate = O2.OrderDate Order by OrderDate)
Alternatively I was thinking that doing an INNER JOIN with the table on itself there might be a way but I have as yet not been able to figure it out.
|
|
|
|
|
If you're looking for the oldest date, remove the check for O1.OrderDate = O2.OrderDate and use a having min clause.
|
|
|
|
|
OK, sorry for being the klutz here but could you be a little bit more specific please?
I am familiar with the MIN() function as in SELECT MIN(OrderDate) FROM Orders but I'm not sure how to use it with the HAVING clause.
|
|
|
|
|
If the explanation was not clear, you need to learn how to use agregates in sql statement clauses.
|
|
|
|
|
edboe wrote: If the explanation was not clear, you need to learn how to use agregates in sql statement clauses.
So sorry for wasting your time. I'm sure the explanation was clear and I know that I need to learn how to use aggregates in sql statement clauses - which is exactly why I came here.
My apologies again if I asked for too much help. Here's hoping that someone else might be a little bit more forgiving.
|
|
|
|
|
Have you looked up the HAVING clause and realized how easy it actually is?
|
|
|
|
|
You could always do this:
select o1.* from orders o1<br />
where o1.orderdate = (select<br />
o2.orderdate from orders o2<br />
where o1.custid = o2.custid)
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
The only tricky part is getting the OrderDescr field into your result. There are a few ways to accomplish this and I have one listed below (TSQL) that should work. Life would be easier if you had a primary key on the order table.
SELECT
*
FROM
Orders o1
INNER JOIN
(SELECT
custID,
MIN(OrderDate)
FROM
Orders
GROUP BY
custId) o2
ON (o1.CustId = o2.CustId AND
o1.OrderDate = o2.OrderDate)
|
|
|
|
|
Hi everybody,
I hava a table like this:
Table Name- TmpTbl
id PD_Id Remark
1 1 Good
2 2 Bad
3 2 fair
4 3 Bad
5 3 Good
In this i want the select query for the result like this:
id PD_Id Remark
1 1 Good
2 2 Bad
4 3 Bad
Can you help me?
Regards,
Jegastar D.
|
|
|
|
|
In your example, just select where id = 1, 2 or 4.
If you are looking for more complex behaviour, then you need to specify what it is that you are looking for, as we can't divine it from your example.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Acctuly this is my table
id PD_Id Remark
1 1 Good
2 2 Bad
3 2 fair
4 3 Bad
5 3 Good
In this i want the select query for the following result
id PD_Id Remark
1 1 Good
2 2 Bad
4 3 Bad
|
|
|
|
|
Id, PD_ID, Remark are the 3 fields that i have used in this table.
|
|
|
|
|
There is (almost) nothing consistent or special about your desired result set (no specific visible relationship between the value of remark, id , PD_ID).
The resultset can be produced with
"select top 3 id,pd_Id,remark from table where Remark in ('Good','Bad') order by PD_Id ASC"
But that does not seem particularly useful.
|
|
|
|
|
iam having 10 triggers for a table
i want view the triggers
which trigger is firing
is it possible to know
or any system stored prodeure is avaliable in sqlserver 2000
with regards
|
|
|
|
|
In Enterprise Manager, right click on the table and select Manage Triggers. The listbox at the top allows you to view the different triggers - by default it shows the New trigger option.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
This will give you a list of all triggers on a table:
sp_helptrigger[^]
You might want to look at this too:
Using Nested Triggers[^]
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Hello everyone:
I have a MySQL server running on a linux that is working properly, and i have errors trying to connect to the server on a windows NT client using ODBC. The error i am getting is Can't connect to MySQL 10060. The driver i am using is ODBC 3.51,(i wish to create a vb app using the driver to access the database) I am quite sure that the parameters that i specified for the host name etc are correct. I even downloaded the MySQL control centre to test the connection to the MySQL linux server, but it still doesnt work. I tried using the same ODBC driver and the same windows client to connect to another dummy MySQL server running on a remote windows PC and it works. Also I have a website coded in PHP that can access the MySQL database on any windows pc, so it couldn't possibly be that the MySQL server blocks out my clients IP address right?
Please help Thanks in advance
andy
-- modified at 2:47 Monday 27th November, 2006
|
|
|
|
|
firebow3007 wrote: it couldn't possibly be that the MySQL server blocks out my clients IP address right?
It shouldn't. Sounds like problem on client end...
If you try to write that in English, I might be able to understand more than a fraction of it. - Guffa
|
|
|
|
|
How much expensive openning and closing connection is when connection pool is used ???
Assume I invoke four stored procedures in a page , I open and close the connection for invoking each . How much will it affect the performance of that page ??
calling this in a page :
<br />
Connection.Open();<br />
Connection.Close();<br />
<br />
Connection.Open();<br />
Connection.Close();<br />
<br />
Connection.Open();<br />
Connection.Close();<br />
<br />
Connection.Open();<br />
Connection.Close();<br />
Is it worth replacing it with this. I have made the site like above.It is so hard for me to replace that. What is your suggestion ?
<br />
Connection.Open();<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Connection.Close();<br />
|
|
|
|
|
I think the first way is acceptable. The close method returns connection to the pool and open method get a freely connection from pool. The second way seems to be a bit better performance but it will charge a lots of cost of changing sources
|
|
|
|
|
Hello All experts,
I am Jayachandra.C, My problem is
I am able to create New User in Oracle database and able to give previleges on sequences, insert, update like. User created. But when i am trying to create a table in the created user table is not creating. Please give me the solution & reason where i failed. please
thank you sir,
Jayachandra.C
-- modified at 4:07 Sunday 3rd December, 2006
|
|
|
|
|
in my project i am using sql server i would like to do normalization how to do help
|
|
|
|
|
|
Based on your varied questions all posted within two days it sure looks like you are asking for homework answers.
____________________________________________________
If at first you don't succeed, skydiving might not be for you.
|
|
|
|