|
Thank you for your answer,
you are right S.id is useless.
I played with your solution but on this way I only can get one item from the sells table ( = sellsdata). What's to do when I nead all the items from sells table row ?
tnx
Frank
|
|
|
|
|
fracalifa wrote: What's to do when I nead all the items from sells table row ?
Then you can go back to using a join to do so.
|
|
|
|
|
This was my first suggestion, but then I'm already at the initial position of the problem !
more ideas ?
|
|
|
|
|
I'm confused.
Originally from what I understand, your question was, how do you get the data such that you get all the rows of the first table, combined with 1 row of sell table for each row of the first table.
Now you say you want all of the rows from sell? What exactly do you want? Give me examples please.
|
|
|
|
|
Your first understanding is right.
I need for all customers their last sold product. But the last product information consits of 4 items. So the solution grid should look like that
ID-----Name----Address------> LastSoldItem----ProductNr.----ProductPrice----LastSellingDate
1111--Miller--1.st Avn----------Ship-----------00112233--------1000,00-----------05/09/2008
1123--Riven---Hallstreet--------Car------------00055564--------2000,00-----------07/08/2008
etc.
Every customer has sold many products and I want to see only the last sold product consisting of 4 items.
The tables are related by the customer-ID.
With your suggested solution I only get one item of the last sold product. (and not all the 4 I need)
I hope this clears your confusion....
tnx
frank
|
|
|
|
|
fracalifa wrote: Every customer has sold many products and I want to see only the last sold product consisting of 4 items.
fracalifa wrote: With your suggested solution I only get one item of the last sold product. (and not all the 4 I need)
You mean 4 columns from the sells table?
|
|
|
|
|
A Wong wrote: You mean 4 columns from the sells table?
If that's the case then:
SELECT C.id,C.customerdata,S.id,S.LastSoldItem, S.ProductNr, S.ProductPrice, S.LastSellingDate FROM dbo.customer AS C
LEFT OUTER JOIN
(SELECT s1.id,s1.LastSoldItem,s1.ProductNr, s1.ProductPrice, s1
.LastSellingDate FROM dbo.sells s1
where s1.LastSellingDate = (select max(s2.LastSellingDate) from dbo.sells s2 where s1.id = s2.id))
AS S ON C.id=S.id
|
|
|
|
|
You should ALWAYS provide the actual fields for each table to avoid misunderstandings.
This should solve the problem...
;WITH LatestSells AS (
SELECT s1.*
FROM dbo.Sells AS s1
INNER JOIN (
SELECT DISTINCT Customer_ID
, ( SELECT TOP 1 Sell_ID
FROM dbo.Sells
WHERE Customer_ID = sx.Customer_ID
ORDER BY Sell_Date DESC
) AS Sell_ID
FROM dbo.Sells AS sx
) AS s2 ON s1.Customer_ID = s2.Customer_ID AND s1.Sell_ID = s2.Sell_ID
)
SELECT c.*, s.*
FROM dbo.Customers AS c
LEFT OUTER JOIN LatestSells AS s
ON c.Customer_ID = s.Customer_ID
|
|
|
|
|
... and - as an addition - here is some code that everyone can test..
CREATE TABLE #Customers (
Customer_ID INT,
Customer_Name NVARCHAR(50)
) ;
INSERT INTO #Customers (Customer_ID, Customer_Name)
SELECT 1, 'John Smith' ;
INSERT INTO #Customers (Customer_ID, Customer_Name)
SELECT 2, 'Kate Taylor' ;
CREATE TABLE #Purchases (
Customer_ID INT,
Purchase_ID INT,
Product_Name NVARCHAR(50),
Quantity DECIMAL(8,2),
Sell_Date DATETIME
) ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 1, 1, 'Seagate HDD' , 4 , '2008-07-16 08:45' ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 2, 2, 'Maxtor HDD' , 2 , '2008-07-16 08:47' ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 1, 3, 'Mouse' , 2 , '2008-07-16 08:52' ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 2, 4, 'LCD' , 1 , '2008-07-16 09:15' ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 1, 5, 'Keyboard' , 7 , '2008-07-16 09:25' ;
INSERT INTO #Purchases (Customer_ID , Purchase_ID , Product_Name , Quantity, Sell_Date)
SELECT 1, 6, 'KVM' , 2 , '2008-07-16 10:30' ;
;WITH LatestSells AS (
SELECT s1.*
FROM #Purchases AS s1
INNER JOIN (
SELECT DISTINCT Customer_ID
, ( SELECT TOP 1 Purchase_ID
FROM #Purchases
WHERE Customer_ID = sx.Customer_ID
ORDER BY Sell_Date DESC
) AS Purchase_ID
FROM #Purchases AS sx
) AS s2
ON s1.Customer_ID = s2.Customer_ID AND s1.Purchase_ID = s2.Purchase_ID
)
SELECT c.*, s.*
FROM #Customers AS c
LEFT OUTER JOIN LatestSells AS s
ON c.Customer_ID = s.Customer_ID ;
DROP TABLE #Customers ;
DROP TABLE #Purchases ;
|
|
|
|
|
Thank you,
that's it.
Great
|
|
|
|
|
Hello i am trying to sync my data from my PDA(pocket pc) to my server which is located remotely.
So any one can help me how to right simple application with only one field on db like name only using vb2005-8 and sql server as data base that will sync remotely.
then i can change other to my project which is having many tables.
Nassoro Ally.
|
|
|
|
|
Hi 2 All
I developed a desktop application using Microsoft Visual Studio .NET 2003 (VB.NET) with Microsoft SQL Server 2000 with no service pack installed.
I deployed this application on network (Windows XP on all computers clients as well as server), running fine. I mean accessing and showing data from server machine (i installed connectivity only on the client machine, not client and server tools).
The problem which i am facing is that reports (used built in crystal reports) are not visible on clients machines, when i access reports on server machine it is fine, but when i access reports from client machine a dialoge box appear which demands
Server
database
user name
password
Even if i provide all above informations the report is not visible.
Please help me that how can i view reports on client machine ...
thanks in advance ...
bye
Regards,
Arfan Qadir
|
|
|
|
|
Hi,
I have a simple method that checks if a sheet name is present in the excel file or not. The code is simple and works fine when the sheet name doesn't contain any spaces:
private bool validateExcelSheetByName(string name, OleDbConnection connection) {
bool result = true;
try {
if (connection.State == ConnectionState.Closed)
connection.Open();
var command = new OleDbCommand(string.Format("select * from [{0}]", name), connection);
command.ExecuteNonQuery();
}
catch {
result = false;
if (connection.State == ConnectionState.Open)
connection.Close();
}
return result;
}
In case I have any space in the sheet name (for example 'My Sheet'), I get an exception saying:
Invalid bracketing of name 'My Sheet$'.
I store the sheet name in a variable:
private const string sheetName = "My Sheet$";
How can I fix this code so it will work when I have spaces in the sheet name?
Thank you!
Andrei
------------
Croitoriu Andrei
andrei.croitoriu@gmail.com
http://www.stud.usv.ro/~acroitoriu
http://spaces.msn.com/acroitoriu/
"No complexity beyond what is necessary"
|
|
|
|
|
I by mistake copied another declaration of a sheet in my file. The above code fails when the sheet name is something like:
private const string sheetName = "My. Sheet$";
So in case the name contains a dot and a space the exception will be thrown.
How can I fix this?
Thank you!
------------
Croitoriu Andrei
andrei.croitoriu@gmail.com
http://www.stud.usv.ro/~acroitoriu
http://spaces.msn.com/acroitoriu/
"No complexity beyond what is necessary"
|
|
|
|
|
Hi i am facing problem in nested cursor...,
In this first cursor for managers and second one is for employee details...,
Its going on well..,
But the employee detail it binding the all the managers, last record of employee at 2 times...,
but i want to bind those also at one time...,
See i given my procedure on down side:
pLz tell me what wrong in that?,
DECLARE curMgr CURSOR FOR
SELECT distinct ManagerID FROM @tbl_TempManagers
OPEN curMgr
FETCH NEXT FROM curMgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
WHILE (@@FETCH_STATUS = 0 )
BEGIN
SET @EmpDetail = ''
SELECT @Count=Count(*) FROM @tbl_TempEmployee
--
SET @Counter=1
DECLARE curDetailList CURSOR FOR
SELECT distinct EmployeeName from @tbl_Tempemployee where ManagerId = @mgID
OPEN curDetailList
FETCH NEXT FROM curDetailList INTO @EmployeeName1
SELECT
convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description],
TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date],
CreationBy as [Name Of Employee] FROM tbl_TSEnterData where CreationBy=@employeename1
AND TSDate BETWEEN
CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101)
order by CreationBy asc,TSDate Desc
WHILE (@@FETCH_STATUS = 0 AND @Counter<@Count)
BEGIN
SET @EmpDetail = @EmpDetail + @EmployeeName1 + ', '
FETCH NEXT FROM curDetailList INTO @EmployeeName1
SELECT
convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description],
TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date],
CreationBy as [Name Of Employee] FROM tbl_TSEnterData where CreationBy=@employeename1
AND TSDate BETWEEN
CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101)
order by CreationBy asc,TSDate Desc
SET @counter=@counter+1
END
CLOSE curDetailList
DEALLOCATE curDetailList
INSERT INTO @Result VALUES (@mgID, @EmpDetail)
FETCH NEXT FROM curmgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
END
CLOSE curmgr
DEALLOCATE curmgr
Thanks & Regards,
NeW OnE,
please don't forget to vote on the post
modified on Wednesday, July 16, 2008 2:52 AM
|
|
|
|
|
I have trouble understanding your question so I can't provide much actual help; however looking at the query one thing puzzles me.
Why do you have similar selects both outside your "WHILE" loops as well as inside? To me it looks like it will cause problems with the loop over FETCH NEXT.
Removing duplicate functionality could help you reduce the complexity of your query making it more manageable, and it could possible be from those double selects that your problems arise.
|
|
|
|
|
hi
m trying ot retrieve bytes stored in sql table....
these bytes represent the image....
i have tried but only first 13 bytes are retrived..
how can i retrieve all the bytes???
|
|
|
|
|
How are the information stored?
|
|
|
|
|
hi,
How do i convert a varchar field into the datetime data type? the reason i need this lies in the requirement that in the earlier data base the column that is holding the date value is having the data type as varchar. and in the new design the column data type is datetime. How i transfer old Data to new column without loss .from the older design to the newer and got stuck with this datetime convertion issue. do let me know the best possible solution.iam using sql server 2000
following are the sample date Format that is there in the older table for the date.
dd/mm/yyyy.
Regards
Rameez
|
|
|
|
|
Try:
Convert(DATETIME, 'your datestring in dd/mm/yyyy format here', 103)
|
|
|
|
|
Hi everyone, i was creating a stand alone application i.e. a project using Visual Studio 2005 with C#.NET. After creating six different GUI which links to each other, and also created datatable for each and everyone of the GUI. I started encountered problems interacting to each database.For instance on this application, i want my first page which required the Username and password before connecting to the next page. I want this username and password to be part of data inserted in the database table or else error message should display. Just like signing to codeproject account. And again, i want to assign one person as an administrator who will be given people access to the said application.
Secondly,after the necessary update on this application, i want my result to be displaying at the front-end i.e. after connecting a screen to the management pc.
I would be much more appreciate your concern about this, if anyone could help me out. I shall be waiting for your solution on this.
Cheers
|
|
|
|
|
So what exactly is your problem, which bit is not working? Post your code so we can try to help.
I suspect you have not really thought about your database design?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hello all,
I may not posed the question in a manner by which I could get a decent search response, so here I am.
I am trying to figure out a way to connect a table to an ODBC datasource on my PC. To illustrate, in Access you can "Link" or "Import" data from an ODBC Source. I am wondering if you can do the same with SQL Server 2007.
Any responses shall be greatly appreciated.
Best regards,
F
|
|
|
|
|
Are there any alternatives to Database Publishing Wizard[^] that allow you to select which objects to script?
Also, any suggestions for tools that help with change management in SQL database development?
Todd Smith
|
|
|
|
|
|