Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure: Sort by week instead of month

Author  Topic 

meef
Posting Yak Master

113 Posts

Posted - 2010-07-14 : 11:10:55
I have a stored procedure here that currently sorts things by month, but I'd like to have it sort by week instead. I have no idea how to go about this, can someone guide me in the right direction?

The current code looks like this, it's a bit long, but I'm not sure what you need to see.

DECLARE @DayOfMonth integer
DECLARE @MinDate datetime

SET @ClientCode = (SELECT ClientCode FROM dbo.[User] WHERE UserLogin = @ReportUser)
SET @ClientName = (SELECT ClientName FROM dbo.Client WHERE ClientCode = @ClientCode)
SET @DayOfMonth = DAY(GETDATE())

begin
SELECT MASTID, Acct AS [Client Name], BTSINV AS [Fund Req #], BTSINVLN AS [Fund Line #],
BTSINVDT AS [Fund Req'd'], BTSFEE AS [TBL Fees],
BTSINVDTPD AS [Funds Rec'd'],
BTSCKDT AS [Check Cut],
BTSCKNO AS [Check #],
BN AS Batch, PK AS Rec, ACCT AS Client, ODIV AS Division, RCDT AS [Frt Bill Rec'd'],
SHDT AS Shipped, PERSH AS [Per Ship], PERPY AS [Per Paid], SCAC, SCX AS SCACX, CANM AS Carrier,
PRNO AS Pro, PRNOSEQ AS ProSeq, BLNO AS BOL, SHNM AS Shipper, OCI AS [Org City], OST AS [Org ST], OZP AS [Org Zip],
CONM AS Consignee, DCI AS [Dest City], DST AS [Dest ST], DZP AS [Dest Zip], IO AS Dir, PC AS Terms, LCT AS [FB Lines],
WTTTL AS Wgt, CL1, WT1 AS Wgt1, CL2, WT2 AS Wgt2, CL3, WT3 AS Wgt3, CL4, WT4 AS Wgt4, CL5, WT5 AS Wgt5, TPL AS Plt,
EXTTL AS [Billed Extended], DIPCT AS [Billed Disc Pct], DIAMT AS [Billed Disc Amt], LHAMT AS [Billed Linehaul],
FSPCT AS [Billed Fuel Pct], FSAMT AS [Billed Fuel Charge], ACAMT AS [Billed Acc Total], FBTTL AS [Billed Total],
I_EXTTL AS Extended, I_DIPCT AS [Disc Pct], I_DIAMT AS [Disc Amt], I_LHAMT AS Linehaul, I_FSPCT AS [Fuel Pct],
I_FSAMT AS [Fuel Charge], I_ACAMT AS [Acc Total], I_FBTTL AS Total, ACCD1 AS Acc1, ACAMT1 AS [Acc1 Amt],
ACCD2 AS Acc2, ACAMT2 AS [Acc2 Amt], ACCD3 AS Acc3, ACAMT3 AS [Acc3 Amt], ACCD4 AS Acc4, ACAMT4 AS [Acc4 Amt],
ACNM AS [Your Name], ACCI AS [Your City], ACST AS [Your ST], ACZP AS [Your Zip], OPNM AS [Other Name], OPCI AS [Other City],
OPST AS [Other ST], OPZP AS [Other Zip], ORNO AS [Order], PONO AS PO, ACNO AS [GL Code], CASES AS Cs, PCS AS Pc,
M4 AS [Order mult], CASE bts WHEN 1 THEN 'Y' ELSE 'N' END AS TBL,WgtBrk as [Wgt Brks], CASE WHEN BTSInvDt > DATEADD(YEAR,-1,GETDATE()) THEN 'CURRENT' ELSE 'PREVIOUS' END AS [REP PER]
FROM dbo.FreightPayment
WHERE (BTSINVLN > 0) and
(BTSInvDt >= DATEADD(MONTH,-(@months),DATEADD(DAY,-@DayOfMonth,GETDATE())) AND BTSInvDt < DATEADD(DAY,-@DayOfMonth,GETDATE()))
end

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-07-14 : 11:40:32
you don't have an ORDER BY clause, so it doesn't sort by anything.

add the sort using the datepart function

ORDER BY DatePart(week, date_field)
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2010-07-14 : 11:56:06
Thanks, but I think "sort" was the wrong word to use on my part. Currently the SP is being used to generate a report. That "PERPY as Per_Paid" is the field that has the date. Somehow it's being used to only show the year/month, I'm not sure where it's doing that in the SP. So for 06/2010, it's displaying "1006" and so on.

I edited my original post to add some declarations that are in the SP as well.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-07-14 : 13:31:37
I don't see any manipulation.. I see this: PERPY AS [Per Paid]. That's just aliasing the column "PERPY" with a new name "Per Paid"

Is the column PERPY an actual datetime field? If not what is it?

If you wanted to post some DDL and DML, this link might help:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2010-07-14 : 13:47:11
Nope, PERPY is a varchar field... Maybe it's something being done in the actual report? EDIT: Don't think so, just put the field elsewhere on the report and it's still giving me the 1006 result, so I don't think it's set that way in the report.

As for that link, I don't see anything that says "All Tasks" when I right click the table, just "Script Table As". I'm using SQL Server 2005.
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2010-07-15 : 09:25:34
Stumped like I am? :)

EDIT: Well duh, I finally ran a query for the data I am seeing in the report and PER_SHIP is actually in the "1006" format in the database... That would explain it.

I don't know how I'm supposed to do this by week then, I must have to use a different field or something.

The line BTSINVDTPD AS [Funds Rec'd'], that field has a full length date in it that seems to correspond with the PER_SHIP field, so if that's a full date, how could I get it to display only the week? Can that be done via SQL or does it actually have to be in that format?

EDIT2: Getting closer.... This line added two columns to the query results, one is the year and the other seems to be the week...

YEAR(BTSINVDTPD), DATEPART(Week,BTSINVDTPD),


I don't know how to rename the fields using these YEAR and DATEPART bits though, and I'd like to get the results in one column rather than two... Is that possible?
Go to Top of Page
   

- Advertisement -