Showing posts with label basically. Show all posts
Showing posts with label basically. Show all posts

Thursday, March 29, 2012

Getting the latest row from a batch

Hi All

This is a belter that my little brain can't handle.

Basically I have 1 SQL table that contains the following fields:

Stock Code
Stock Desc
Reference
Transaction Date
Qty
Cost Price

Basically this table stores all the transaction lines of when a user
books stock items into stock so that they can look at a journal of
this goods in as and when they please.

My task is that the user wants a list of all the stock items with the
last cost price that they were booked in at.

So I think I have to find the last transaction date used for each
stock code and then bring this back as 1 row per stock code with the
above fields of data.

How the whats-its can I do this? Is it acutally possible?

Any help you can give is much appreciated.

Rgds

LaphanSELECT S1.stockcode, S1.transdate, S1.costprice
FROM Stock AS S1
JOIN
(SELECT stockcode, MAX(transdate)
FROM Stock
GROUP BY stockcode)
AS S2(stockcode, transdate)
ON S1.stockcode = S2.stockcode
AND S1.transdate = S2.transdate
GROUP BY S1.stockcode, S1.transdate, S1.costprice

--
David Portas
----
Please reply only to the newsgroup
--|||Many thanks David

Very much appreciated.

Rgds

Laphan

David Portas <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:nJmdnSNww5bmVkmi4p2dnA@.giganews.com...
SELECT S1.stockcode, S1.transdate, S1.costprice
FROM Stock AS S1
JOIN
(SELECT stockcode, MAX(transdate)
FROM Stock
GROUP BY stockcode)
AS S2(stockcode, transdate)
ON S1.stockcode = S2.stockcode
AND S1.transdate = S2.transdate
GROUP BY S1.stockcode, S1.transdate, S1.costprice

--
David Portas
----
Please reply only to the newsgroup
--|||Many thanks for the help David. The below script works perfectly
apart from the fact that I need to add a cost price field from another
table:

SELECT S1.STOCKID AS 'Stock Code', S1.TRANSACTIONDATE AS 'Transaction
Date', S1.QUANTITY AS 'Quantity', S1.COSTPRICE AS 'Cost Price'
FROM STOCKTRANSACTIONS AS S1
JOIN
(SELECT STOCKID, MAX(TRANSACTIONDATE)
FROM STOCKTRANSACTIONS
WHERE TRANSACTIONTYPE = 3
GROUP BY STOCKID)
AS S2(STOCKID, TRANSACTIONDATE)
ON S1.STOCKID = S2.STOCKID
AND S1.TRANSACTIONDATE = S2.TRANSACTIONDATE
GROUP BY S1.STOCKID, S1.TRANSACTIONDATE, S1.QUANTITY, S1.COSTPRICE

Could you please let me know how I can add an additional COSTPRICE
column from a STOCKPRICES table to this script.

FYR, if I was to perform a straightforward query to join the
STOCKTRANSACTIONS and the STOCKPRICES tables together this is how it
would look to get the required data, but this wouldn't contain the new
fangled 'find last date' thing that you sent me:

SELECT STOCKTRANSACTIONS.STOCKID, STOCKTRANSACTIONS.TRANSACTIONDATE,
STOCKTRANSACTIONS.QUANTITY, STOCKTRANSACTIONS.CURRENCYID,
STOCKTRANSACTIONS.COSTPRICE, STOCKPRICES.COSTPRICE
FROM STOCKPRICES, STOCKTRANSACTIONS
WHERE STOCKTRANSACTIONS.CURRENCYID = STOCKPRICES.CURRENCYID AND
STOCKTRANSACTIONS.STOCKID = STOCKPRICES.STOCKID AND
((STOCKTRANSACTIONS.TRANSACTIONTYPE=3) AND
(STOCKPRICES.PRICELEVELID='1'))

Any ideas on how to sync these 2 queries??

Rgds

Laphan

Tuesday, March 27, 2012

Getting the database name in Server-Side trace event

I'm trying to audit all the database activities using Server-side trace
in SQL Server 2000.
Basically I need to get the login information (login Id, Login time
etc) along with the name of the database.Can some one suggest a way to
do that?

Thanks in advance.
JJacand4321 (jacand4321@.yahoo.com) writes:
> I'm trying to audit all the database activities using Server-side trace
> in SQL Server 2000.
> Basically I need to get the login information (login Id, Login time
> etc) along with the name of the database.Can some one suggest a way to
> do that?

You can find in Books Online exactly which columns that are populated for
which events. The database name is not always populated, but in such
case the database ID usually is. What I do when I analyse trace files is
that I load them into a table, and then supplement columns like database
name and object name, when only the ID is present. (In some cases, not
even the object id is populated, in which case I try to extract it from
TextData.)

The place to look in Books Online is
Administering SQL Server
Monitoring Server Performance and Activity
Monitoring with SQL Profiler
Monitoring with SQL Profiler Event Categories

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Getting Textbox value

I have a calculated value in a textbox in a report that I need to be
able to see from another textbox. Basically, I have two sums that I
need to subtract and place in a third textbox. I've tried
textboxName.value and Fields!textboxName.Value, but RS doesn't like
either method. Is it possible to get to the value of a textbox?You can reference the value of other textboxes (with certain restrictions -
mainly scoping) through the ReportItems collection. Try this:
=ReportItems!TextboxName.Value
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"no one" <noone@.yahoo.com> wrote in message
news:420FD310.9CCF63A0@.yahoo.com...
>I have a calculated value in a textbox in a report that I need to be
> able to see from another textbox. Basically, I have two sums that I
> need to subtract and place in a third textbox. I've tried
> textboxName.value and Fields!textboxName.Value, but RS doesn't like
> either method. Is it possible to get to the value of a textbox?
>

Wednesday, March 7, 2012

getting MAX value and displaying record

Hi all,
I basically want to display the single row that has the highest 'jobid' using a SP. I was playing with MAX(jobid) but getting errors about no group by, etc. Where do I begin with this?
SELECT Purchord, JobNo, Descr, customer, jobid
FROM JobsSELECT Purchord, JobNo, Descr, customer, jobid
FROM Jobs
INNER JOIN (SELECT MAX(jobid) jobid FOMR Jobs) HighestID ON Jobs.jobid = HighestID.jobid

-or-

SELECT TOP 1 Purchord, JobNo, Descr, customer, jobid
FROM Jobs
order by jobid desc|||select top 1 Purchord, JobNo, Descr, customer, jobid
from Jobs
order by jobid desc

Edit: beaten!!|||Ohhhh......!!!! Thank you very much guys!|||2 MINUTES 2 LATE, RUDY!

BOO-YAH!

Fastest fingers in the midwest...