|
I have two colums, WPID and WNum. WPID is distinct, but WNum can be reapeated.
For instance, I could have WPID 1-10, and each value for WNum would be 55555. What I need to do is get the maximum value for WPID associated to WNum, in this case 10.....and spin through all the records this way.
I've tried (and other ways):
SELECT MAX(WPID) AS Expr1, [WNum]
FROM WP_General_Info
GROUP BY [WNum], WPID
...but it's not working.
|
|
|
|
|
SELECT TOP 1 WPID, WNum
FROM WP_General_Info
ORDER BY WPID DESC
|
|
|
|
|
Thanks knarf_scot! That helps!
I'm basically using this for a SQL Server Report, and am having trouble with the footer (which shows the highest/latest value for WPID). I have my grouping right and can see my latest value for WPID, but now the values in the textboxes aren't showing for the max value of the footer.
|
|
|
|
|
Your GROUP BY clause includes the column you are trying to summarize. As a result, the query will return the maximum value of WPID for each row, which is equivalent to a simple SELECT statement.
To get the maximum value of WPID for a given WNum , use:
SELECT WNum, Max(WPID) As Expr1
FROM WP_General_Info
WHERE WNum = @WNumToFind
GROUP BY WNum
To return all values from WNum with the maximum value of WPID for each, use:
SELECT WNum, Max(WPID) As Expr1
FROM WP_General_Info
GROUP BY WNum
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
The problem I'm running into now is all the field from my child table have to be in the group by clause.
I get an error saying:
"Column "" is not valid in the select list because it is not contained in the aggregate function or the Group By clause"
|
|
|
|
|
As the error message says, any column in your grouped SELECT statement must be in an agregate function (MIN , MAX , SUM , etc), or in the GROUP BY clause. Both of the examples in my previous post satisfy this condition.
If you need to return other columns from the row with the maximum WPID , you could use a sub-query:
SELECT
I.WPID,
G.WNum,
I.OtherColumn,
I.MoreInfo,
...
FROM
WP_General_Info As I INNER JOIN
(
SELECT
WNum,
Max(WPID) As MaxWPID
FROM
WP_General_Info
GROUP BY
WNum
) As G
ON I.WPID = G.MaxWPID And I.WNum = G.WNum
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Richard!
Great! That's what I needed. Thanks so much!
|
|
|
|
|
Hai,
How i can know a stored procedure is existing in the databse or not by writing code in the vb.net?Any way is there?I am sure somebody can show my way.
Thank You,
Rahul.P.Menon.
SoftwareDeveloper(.NET)
|
|
|
|
|
This query should point you in the right direction.
SELECT SPECIFIC_NAME FROM INFORMATION_SCHEMA.ROUTINES
|
|
|
|
|
This will work or something like it in SQL Server. If you wish to use this from .NET wrap this into a Stored Proc and call from ADO.NET.
#########################################
DECLARE @SPS varchar(200)
SET @SPS = 'my_stored_proc'
if exists (select * from dbo.sysobjects where id = object_id(@SPS)
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
SELECT 'Yes it existed '
END
ELSE
BEGIN
SELECT 'Cant find Proc
END
###########################################
|
|
|
|
|
Hai Michael Potter,Knarf_scot
Really great.Thanks much for your fantastic help and showed me the right way.
Thank You,
Rahul.P.Menon.
SoftwareDeveloper(.NET)
|
|
|
|
|
Hi, this is a database/schema design question. Should I move image column to a different table, possible to a different file group on a different disk, would it improve performance (appreciable improvement)?
NOW:
<br />
CREATE TABLE tblInventoryItem (<br />
ItemID INTEGER IDENTITY(1,1),<br />
ItemName NVARCHAR(30),<br />
ItemDescription NVARCHAR(30),<br />
...<br />
pic IMAGE<br />
)<br />
Would this (following) improves performance?
Code:
<br />
CREATE TABLE tblInventoryItem (<br />
ItemID INTEGER IDENTITY(1,1),<br />
ItemName NVARCHAR(30),<br />
ItemDescription NVARCHAR(30),<br />
...<br />
)<br />
<br />
-- Two columns only:<br />
CREATE TABLE tblEmployee (<br />
ItemID INTEGER IDENTITY(1,1),<br />
pic IMAGE<br />
)<br />
Thanks in advance.
Norman Fung
|
|
|
|
|
Keep your data where it belongs. If the pic column is dependent upon the ItemID and there is only one of them per ItemID , it belongs in the tblInventoryItem table.
Just rememeber that the pic column is in the table when you construct your queries. Don't use SELECT * when a more restricted query will do. The image data is already stored in another section of the database. SQL only stores a pointer in the actual table and does the grunt work when you need the image.
|
|
|
|
|
|
hai
I had a Dataset With table Product . Whose Fields are
productID int
Name string
Price int
I had populated the Data Set with records.
From the Data Set i need to found
+ Maximum Price
+ Minimum Price
+ Sum of All Price
Is there any way to solve this with out much coding
Plese help me to solve this
Thanks & Regards
|
|
|
|
|
You can split in several queries or do it in just one:
SELECT max(Price), min(Price), sum(Price) from Product
then access the corresponding field of the result set.
Marc Soleda.
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
|
|
|
|
|
i don't think your select will work as there isn't any Group By statement for the agregate functions you use (max() , min() , sum() )...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Obviously is better to split in several queries or group them but depending on the table design it'll work. Try it with the table that anig1234 provided. I did it.
Marc Soleda.
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
|
|
|
|
|
Try this
*** Max Price *****
select top 1 * from Product
order by price DESC
*** Min Price *****
select top 1 * from Product
order by price
*** Sum of all Prices ***
select sum(price) from Product
|
|
|
|
|
Hi folks,
My asp.net app is having trouble updating a table in Access 2K. I don't get any errors but the data is not updated. When I run the same query in Access Query IDE it runs fine. Any idea on what might be going on?
Thanks
Mike
private void btnSubmit_Click(object sender, System.EventArgs e)<br />
{<br />
OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["ConnectionString"]);<br />
<br />
OleDbCommand myCommand = new OleDbCommand("UPDATE tblZtest SET PropertyType = " +<br />
"@Type WHERE zTest = @PropertyID", myConnection);<br />
<br />
myCommand.CommandType = CommandType.Text;<br />
<br />
OleDbParameter parameterPropertyId = new OleDbParameter("@PropertyID", OleDbType.Integer, 4);<br />
parameterPropertyId.Value = Convert.ToInt32(txtPid.Text);<br />
myCommand.Parameters.Add(parameterPropertyId);<br />
<br />
OleDbParameter parameterType = new OleDbParameter("@Type", OleDbType.Integer, 4);<br />
parameterType.Value = Convert.ToInt32(txtID.Text);<br />
myCommand.Parameters.Add(parameterType); <br />
<br />
try <br />
{<br />
myConnection.Open();<br />
myCommand.ExecuteNonQuery(); <br />
myConnection.Close();<br />
<br />
<br />
lblMessage.Text = "Done";<br />
<br />
<br />
}<br />
catch ( OleDbException myException )<br />
{<br />
string errorMessages = "";<br />
for (int i=0; i < myException.Errors.Count; i++)<br />
{<br />
errorMessages += "Index #" + i + "\n" +<br />
"Message: " + myException.Errors[i].Message + "\n" +<br />
"NativeError: " + myException.Errors[i].NativeError + "\n" +<br />
"Source: " + myException.Errors[i].Source + "\n" +<br />
"SQLState: " + myException.Errors[i].SQLState + "\n";<br />
}<br />
lblMessage.Text = errorMessages;<br />
<br />
}
}
|
|
|
|
|
It looks like your trying to mix Stored Procedure with SQL on the ADO.NET side.
Try this simpler code. It uses SQLconnection rather than OLE so you may have to slightly adjust your connection string.
Note: this has no try catch to simplify the code.
###########################################
string myType = "Type"; ' varchar on SQL Server
string myPropID = "12"; ' Int on SQL Server
string SQLstring = "UPDATE tblZtest SET PropertyType = '" + myType + "' " +
" WHERE zTest = " + myPropID
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
SqlCommand myCommand = new SqlCommand( SQLstring, myConnection );
myCommand.ExecuteNonQuery();
myConnection.Close();
##############################################
|
|
|
|
|
Change your SQL statemente to this:
<br />
OleDbCommand myCommand = new OleDbCommand("UPDATE tblZtest SET PropertyType = " +<br />
"? WHERE zTest = ?", myConnection);
The Oledb classes expect '?' placeholers for parameters, not the names. The parameters collection must be filled in the same order as the '?' placeholders. The OledbParameter objects still need unique names.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
I have a stored procedure that takes two parameters
(1) insert statement
(2) user name
It just executes the insert statement.
There is only one database user in the database.
My requirement is
(1) I need a way for the insert trigger to access the user name for inserting data into audit trail tables.
(2) In case when many users are logged in at the same time, how do I know which user name to use.
In our firm we have the constraint of one database user per database.
If there is any other solution please advise.
Live Life King Size
Alomgir Miah
|
|
|
|
|
Alomgir Miah wrote:
I need a way for the insert trigger to access the user name for inserting data into audit trail tables.
Why? you are inside a stored procedure, let it do the work.
Alomgir Miah wrote:
In our firm we have the constraint of one database user per database.
Unless you have a very good reason for this then that is a crazy constraint. How do you share information? (Which is one of the major advantages of having a database in the first place)
Also, if you only have one user per database then what is the problem? Surely you know who's using what database.
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
Thanks a lot for your reply.
I think I was not very clear in my questions.
I am passing
(1) An insert query string
(2) UserName
to the stored procedure. As soon as the insert happens, the insert trigger is fired. In the insert trigger I am auditind the data by storing the changes(insert in this case) into an
audit trail table.
The answers to your questions are
(1) The trigger can access the database user only through user_name
INSERT INTO AuditTrail
SELECT user_name(),getdate(),'Fund','FamilyID',@rowid,'Insert',null,convert(varchar,FamilyID)
FROM inserted
But I need a way to access the parameter passed into SP.
(2) I know the constraint sucks. This is enforced for security reasons. More users means more activity tracking for all users. This means more work for the support groups and security holes in the application. ( Frankly, I dont agree to this at all like you said. ). But it does not means one user. It means many kerberos user and only one database user.
So the only solution I can think of is having a LastUpdatedBy column in the tables we are auditing. So the insert statement in the Trigger changes to
INSERT INTO AuditTrail
SELECT LastUpdatedBy,getdate(),'Fund','FamilyID',@rowid,'Insert',null,convert(varchar,FamilyID)
FROM inserted
and I wont need the stored procedure in discussion at all.
Is it the best way to go keeping in mind the constraints we have.
Live Life King Size
Alomgir Miah
|
|
|
|