|
her is a simplified version of my problem...
2 tables...
CUSTOMER
customerId
...
ORDER
orderId
customerId
...
the relationship is clearly one (customer) to many (order)
i want to do a join which will only bring back firt order found.
so
select * from customer
inner join order on order.customerId = customer.customerId
but in my "bigger" query this brings back all orders for each customer. so do any outer joins.
IDEAS?
Thanks in advance
Anthony "having a thick day" Blake
|
|
|
|
|
SELECT TOP 1 ...
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
just gives the first customer - i want the first order found for each customer
cheers
anthony
|
|
|
|
|
You could always try this:
select * from customer
inner join order a on a.customerId = customer.customerId
and a.orderId = (SELECT First 1 b.orderID FROM order b WHERE b.customerID = customer.customerId) It's not going to be the most efficient SQL ever, but it should do the trick.
|
|
|
|
|
i will try something like that... i think that it will make a difference in speed but the overall query i'm working on returns 100,000+ rows so i'll have to just try and find out. cheers.
|
|
|
|
|
Rename your ORDER table to ORDERS since ORDER is a keyword (ORDER BY) that conflicts. I also took the liberty of renaming CUSTOMER to CUSTOMERS (since all table names should probably reflect a "plural" sense).
The key is to form a "virtual" orders table that only contains the first order for each customer, and then join it to the CUSTOMERS INNER JOIN ORDERS table to get the other fields you want while eliminating records you don't want. Using the vendor-specific TOP keyword should work, but a more standard SQL way might be:
SELECT *
FROM (ORDERS AS O
INNER JOIN CUSTOMERS AS C ON O.customerId = C.customerId)
INNER JOIN (SELECT customerId, MIN(orderId) AS M
FROM ORDERS
GROUP BY customerId) AS GB ON O.orderId = GB.M;
Assuming your orderId is incresing, this will give it to you. The GROUP BY is how to eliminate all the rows you don't want. HOWEVER, you'll notice that the output reproduces O.customerId, C.customerId, and GB.customerId, all of which are redundant. The only way I know to preventing listing them all is to spell-out the fields you want instead of using '*'.
Hope this helps.
David
|
|
|
|
|
Thats great. Don't worry about the table names - they were hyperthetical - trust me postng the actual query wouldnt have provoked any response it's huge.
|
|
|
|
|
I want to Connect to MySql Server using .NET but I dont have MySql client installed in my machine.
I am also connecting to SqlServer in my application using this Connection String
add name="localServer" connectionString="Data Source=
TRVWSPW001\SQLExpress;Initial Catalog=Dashboard;User Id=Dashboard_Users;Password=passw0rd@123;" providerName="System.Data.SqlClient"
Wat would be connection string to connect to MySql
i know these details Server name,database name,User id
Pwd
|
|
|
|
|
i would try www.connectionstrings.com
|
|
|
|
|
Hi,
I have a problem with a query on a locked table.
The Database I use is mySQL and I want to get a exception when the program try's
to execute a query on a locked table. I tried to set the CommandTimeout in the
OdbcCommand-Object and I also tried to set the "innodb lock wait timeout"-Attribut
from MysQL but it doesn't work.
Has anybody an idea..?
|
|
|
|
|
Have you figured out the problem?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi,
not realy... I've found an ugly workaround but that was not what
I'm searching for. In other Database Management Systems is something
like LOCK NOWAIT.
Do you have any idea?
Thanks
|
|
|
|
|
I have a difficulty in putting the array in to a recordset.
Please suggest.
Thanks in advance
|
|
|
|
|
What difficulty are you having? Be a bit more descriptive about your problem...
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
I am getting the data from database as usual in to a recordset.
I am having some data in array.I want to put the data back in some form in to a recordset so that the existing functionlity does not change.
Remark: The recordset has to be disconnected and database need not be updated.
It is just the calculation that I want to do on the recordset.
Please suggest.
Thanks and regards,
Anil
|
|
|
|
|
Just insert the individual elements of the array into the record set. The recordset class should have an Add method to allow you to add the new data into the recordset. I hope this sheds some light for you.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
In my case I want to get all rows from a table (however it could be the case I [or someone else] only want to get a specific few). How would I count how many rows have been effected by the SQL query?
Heres a bit of my code:
private const string sqlGetAll =
"SELECT ID , Name , PortalMarkupFile AS Portal " +
"FROM Swin_Devices";
command = new SqlCommand(sqlGetAll, connection);
SqlDataReader reader = command.ExecuteReader();
So I'd like something like reader.getAffectedRows or something similar?
Thanks in advance,
Pete
|
|
|
|
|
When you are just selecting data none of the rows get affected. Why don't you use sql Count function?
|
|
|
|
|
Quick bit of research into COUNT and using SELECT COUNT(*) worked a treat.
Thank you!
|
|
|
|
|
You are welcome
|
|
|
|
|
Hi All,
I need to get rid of paging. In other words, I have a webpage in which the data is now being displayed in 3 pages but I want to bring the data alll in one page. Would really appreciate if someone could give me the idea?
Thanks in advance
|
|
|
|
|
How is your paging being accomplished? Is it via a datagrid/repeater or through the database select? If it's via the datagrid, you can switch paging off by setting AllowPaging = false;
|
|
|
|
|
No idea what you are talking about.
Can you clarify please.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
I am sorry but now I would try to be more clear.
I have a webpage where the results are shown in more than one page(which is called paging in CF). Now, what I want to do is to put all the results in one page. so, basically no paging.
I hope this helps.
Thanks very much
|
|
|
|
|
Hi
Are you using ASP.Net(C#.Net or VB.Net)? Are you using any .Net server control to display data on the webpage?
Paging can be done by setting the attribute AllowPaging to TRUE in your .Net Server control. Check your code / HTML page of your web page and change it to FALSE.
Hope this helps.
Harini
|
|
|
|