The following query returns a value of 0 for the unit percent when I do a count/subquery count. Is there a way to get the percent count using a subquery? Another section of the query using the sum() works.
Here is a test code snippet:
--Test Count/Count subquery
declare @Date datetime
set @date = '8/15/2007'
select -- count returns unit data Count(substring(m.PTNumber,3,3)) as PTCnt, -- count returns total for all units
(select Count(substring(m1.PTNumber,3,3))
from tblVGD1_Master m1
left join tblVGD1_ClassIII v1 on m1.SlotNum_ID = v1.SlotNum_ID
Where left(m1.PTNumber,2) = 'PT' and m1.Denom_ID <> 9
and v1.Act = 1 and m1.Active = 1 and v1.MnyPlyd <> 0
and not (v1.MnyPlyd = v1.MnyWon and v1.ActWin = 0)
and v1.[Date] between DateAdd(dd,-90,@Date) and @Date) as TotalCnt, -- attempting to calculate the percent by PTCnt/TotalCnt returns 0 (Count(substring(m.PTNumber,3,3)) /
(select Count(substring(m1.PTNumber,3,3))
from tblVGD1_Master m1
left join tblVGD1_ClassIII v1 on m1.SlotNum_ID = v1.SlotNum_ID
Where left(m1.PTNumber,2) = 'PT' and m1.Denom_ID <> 9
and v1.Act = 1 and m1.Active = 1 and v1.MnyPlyd <> 0
and not (v1.MnyPlyd = v1.MnyWon and v1.ActWin = 0)
and v1.[Date] between DateAdd(dd,-90,@Date) and @Date)) as AUPct -- main select
from tblVGD1_Master m
left join tblVGD1_ClassIII v on m.SlotNum_ID = v.SlotNum_ID
Where left(m.PTNumber,2) = 'PT' and m.Denom_ID <> 9
and v.Act = 1 and m.Active = 1 and v.MnyPlyd <> 0
and not (v.MnyPlyd = v.MnyWon and v.ActWin = 0)
and v.[Date] between DateAdd(dd,-90,@Date) and @Date
However, as you can see, the original select query is run twice and joined together.What I was hoping for is this to be done in the original query without the need to duplicate the original query.
When I insert a row of data into a table via a stored procedure, the row SOMETIMES gets inserted at the bottom, and sometimes gets inserted at another random location (top, midding, etc). I am using Microsoft SQL Server.
Dea anybody know the cause of this? Or how to ensure that the row gets inserted at the bottom?
I am a novice SQL Server user, but this seems like a simple problem that the more advanced users solved long ago. Thanks in advance!
empno empname dept sal ----------- ------------------------------ -------------------- - 1 mike HR 4000 2 John HR 2000 3 Patty HR 3500 4 Carly HR 2200 5 ABC HR 1000 6 DEF HR 4500 7 Ben HR 2200 8 HGI IT 4000 9 GOG IT 2000 10 LILY IT 3500 11 Cathy IT 2200 12 Paul IT 1000 13 Ben IT 4500 14 Ben Fin 2200 15 HGI Fin 4000 16 GOG Fin 2000 17 LILY Fin 3500 18 Cathy Fin 2200 19 Paul Fin 10800 20 Ben Fin 4500
How will i get Maximum and Minimum 3 salaries of EACH DEPARTMENT ie i want to know who are the employees who are drawing top 3 max and min salaries in each department?
Is there a way to sort records the following way.Original Table:KeyField Column 11 02 5003 2004 3005 0To this sorted tableKeyField Column 13 2004 3002 5001 05 0so that the numbers in column 1 are sorted asc but it treats a 0 asthe largest number. We need the first number greater than 0 to befirst in the table.
neo writes "hi i am a beginner at sql this query confuses me...
q:A LIST OF SERVICE TRANSACTIONS(WITH A FULL DESCRIPTION OF THE SERVICE) ON A PARTICULAR DAY ALONG WITH THE NAMES OF THE CUSTOMER AND THEIR ROOM NUMBERS.THE TOTAL AMOUNT TRANSACTED NEEDS TO BE DISPLAYED AT THE BOTTOM OF THE LIST
REQD TABLES ARE SERVICES,TRANSACTIONS AND RESERVATIONS...
all i can do was this answer...
SELECT T.TRANID,S.*,R.FNAME,R.LNAME,R.ROOMID FROM SERVICES AS S ,RESERVATIONS AS R,TRANSACTIONS AS T WHERE S.SERNO=T.SERNO AND T.CUSTID=R.CUSTID ORDER BY T.TRANID COMPUTE SUM(S.SERCHAR)
is there a better way to display in the same table the sum too?"
I have a results set that is sorted on LineNum. Not all items have aLineNum (some are null). I want my result set to return all items inASC LineNum order and then list all items with NULL LineNum at the endof the results. Any Suggestions?Thanks.
I know the function of being able to set a section of a report to print at the bottom of the page, but does anyone know any work arounds for this? I have a main report (statement) which ive done a work around, to include the details i need at the bottom of the page into the footer, but then i have a second report (invoice) that needs to be included as subreport, which also needs infomation at the bottom of the page, but as its a subreport, sql reports does not allow for footer/header infomation in a subreport.
Unfortuantly i can not split the two reports out as they need to be printed together.
Any help/recomendations would be greatly received.
How can you set the text direction to BT-RL 'bottom to top' in SRS? There is control for TB-RL but not BT-RL. Need this for SRS label printing application.
With the function below, I receive this error:Error:Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0.Function:Public Shared Function DeleteMesssages(ByVal UserID As String, ByVal MessageIDs As List(Of String)) As Boolean Dim bSuccess As Boolean Dim MyConnection As SqlConnection = GetConnection() Dim cmd As New SqlCommand("", MyConnection) Dim i As Integer Dim fBeginTransCalled As Boolean = False 'messagetype 1 =internal messages Try ' ' Start transaction ' MyConnection.Open() cmd.CommandText = "BEGIN TRANSACTION" cmd.ExecuteNonQuery() fBeginTransCalled = True Dim obj As Object For i = 0 To MessageIDs.Count - 1 bSuccess = False 'delete userid-message reference cmd.CommandText = "DELETE FROM tblUsersAndMessages WHERE MessageID=@MessageID AND UserID=@UserID" cmd.Parameters.Add(New SqlParameter("@UserID", UserID)) cmd.Parameters.Add(New SqlParameter("@MessageID", MessageIDs(i).ToString)) cmd.ExecuteNonQuery() 'then delete the message itself if no other user has a reference cmd.CommandText = "SELECT COUNT(*) FROM tblUsersAndMessages WHERE MessageID=@MessageID1" cmd.Parameters.Add(New SqlParameter("@MessageID1", MessageIDs(i).ToString)) obj = cmd.ExecuteScalar If ((Not (obj) Is Nothing) _ AndAlso ((TypeOf (obj) Is Integer) _ AndAlso (CType(obj, Integer) > 0))) Then 'more references exist so do not delete message Else 'this is the only reference to the message so delete it permanently cmd.CommandText = "DELETE FROM tblMessages WHERE MessageID=@MessageID2" cmd.Parameters.Add(New SqlParameter("@MessageID2", MessageIDs(i).ToString)) cmd.ExecuteNonQuery() End If Next i ' ' End transaction ' cmd.CommandText = "COMMIT TRANSACTION" cmd.ExecuteNonQuery() bSuccess = True fBeginTransCalled = False Catch ex As Exception 'LOG ERROR GlobalFunctions.ReportError("MessageDAL:DeleteMessages", ex.Message) Finally If fBeginTransCalled Then Try cmd = New SqlCommand("ROLLBACK TRANSACTION", MyConnection) cmd.ExecuteNonQuery() Catch e As System.Exception End Try End If MyConnection.Close() End Try Return bSuccess End Function
Hello all, I'd like to know if anyone has been able to get SQL Server 6.5 to send mail to a MAPI mail server other than Exchange. I would like to implement this functionality so that my group and I can better monitor the results of backups and other scheduled tasks but we do not have Exchange within our site. We currently use Netscape's mail server so it would be GREAT if someone with this knowledge could reply with any tips or URLs or whatever. Just something to get me heading in the right direction. Thanks for your time.
I keep getting extra space at the bottom of each page on my report when it is deployed to the web server but I don't get this extra space on the report server. Does anyone know if this is in the report layout or if the problem is in the web page itself?
I write few lines to do a bottom-up calculation, with 'fetch last' and 'fetch prior'.
It seems that the condition 'WHILE @@FETCH_STATUS = 0' does not work when cursor arrives at the first line, as there is an error message:
'(1 row(s) affected) 6255.84 (1 row(s) affected) Msg 16931, Level 16, State 1, Line 18
There are no rows in the current fetch buffer.
The statement has been terminated.'
how to fix the error?
Here is my code:
DECLARE @lastprice real DECLARE @updatedprice real DECLARE @updatedRe real DECLARE @updatedAUX real SET @lastprice = ( SELECT Close_P from #ClosePrice where #ClosePrice.DateTD = (SELECT MAX(#ClosePrice.DateTD) FROM #ClosePrice) )
Ok so this is more then likely very easy for most. But for me it's hard. I have a stored proc that I've been able to get to retun just the top 10 rows and have been able to order it asending. But it will only return the top 10 and I need to bottom 10 of the selection.
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO
ALTER proc [dbo].[proj_ListFiles](@project_id int) as select top 10(@project_id) id, project_id, filename, contenttype, length from proj_file where project_id=@project_id
How can I make it return to bottom 10. On a side note about this, when it returns the recordset they all have the 'id' of the 'project_id' that I selected. Not that the side note really bothers me as it will not be returning the ID's to be visible.
I'm having a relatively big issue with printing my check report. The report continues, time after time, and regardless of the margins I choose for the report, to be displaced an additional 1" up from the bottom of the page. The printing method is through our newly designed application which sends the emf rendering to the printer. We have gone through the liberty of modifying the actual print document in VB with margins set at 0,0,0,0 (L, R, T, B) and have paid close attention to the printer's maximum printing area. We have come to the conclusion that the problem is coming from reporting services. In addition to the extra margin space on the bottom of the page, there is also a slight yet noticeable shrinking of the page. Despite the fact that those are trade symptoms of another issue after the emf rendering we have modified every possible function of the printdocument and printer settings. So we continue to push the question, is this reporting services? We are fairly certain. I'm asssuming this is a bug in SSRS 2005, yet, I have yet to hear of any similiar case. This is our VS version info just incase:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) Microsoft .NET Framework Version 2.0.50727 Installed Edition: Enterprise Microsoft Visual Basic 2005 77642-113-3000004-41917 Microsoft Visual Basic 2005 Microsoft Visual C# 2005 77642-113-3000004-41917 Microsoft Visual C# 2005 Microsoft Visual C++ 2005 77642-113-3000004-41917 Microsoft Visual C++ 2005 Microsoft Visual J# 2005 77642-113-3000004-41917 Microsoft Visual J# 2005 Microsoft Visual Studio Tools for Office 77642-113-3000004-41917 Microsoft Visual Studio Tools for the Microsoft Office System Microsoft Visual Web Developer 2005 77642-113-3000004-41917 Microsoft Visual Web Developer 2005 Visual Studio 2005 Team Edition for Developers 77642-113-3000004-41917 Microsoft Visual Studio 2005 Team Edition for Software Developers Crystal Reports AAC60-G0CSA4B-V7000AY Crystal Reports for Visual Studio 2005
Microsoft Visual Studio 2005 Premier Partner Edition - ENU Service Pack 1 (KB926601) This service pack is for Microsoft Visual Studio 2005 Premier Partner Edition - ENU. If you later install a more recent service pack, this service pack will be uninstalled automatically. For more information, visit http://support.microsoft.com/kb/926601 SQL Server Analysis Services Microsoft SQL Server Analysis Services Designer Version 9.00.1399.00 SQL Server Integration Services Microsoft SQL Server Integration Services Designer Version 9.00.1399.00 SQL Server Reporting Services Microsoft SQL Server Reporting Services Designers Version 9.00.1399.00
I'm planning to talk to MS if nobody recognizes these symptoms. Any ideas would be greatly appreciated! Thanks!
UPDATE (12/20/2007 1:43:07 pm): We have made a small break-through discovery. We found that that if we convert our report using a pdf rendering that the additional 1 inch added to the bottom margin is no longer there. So, pdf rendering would be an option if we became more desperate. However, this is too time consuming and we would much rather have the report directly sent to the print spooler instead of loading then spooling. Does anybody know what is going on with this apparent conflict between SSRS 2005 and our emf renderer?
I have to display Top 5 values in 'Green' and Bottom 5 in 'Red' out of some data.
Ex: my data is as below
ID Marks 1 98 2 62 3 89 4 97 5 92 6 67
[Code] ....
Expected result is : if the marks are >= 80 then 'Green' (pick only top 5) if marks are <= 70 then 'Red' (pick bottom 5) else 'Black'
As per my data even though we have values >=80 for more than 5 students display only top 5 in Green and same rule applies for Bottom 5 that means
ID 's : 1 ,3 , 4 , 5, 9 and their marks in Green (pick only Top 5 ) even though we have 6 students who got >=80 ID's : 2 , 6, 16 , 15 and their marks in Red (Bottom 5 but we have only 4 students who got <=70 )
I'm a new SQL Reporting user and could use some guidance.
Here is a simple explanation of my application: imagine a report which prints out a grade report for each of 10 students. Depending on how many notes a teacher makes on the student's individual grade report, each student's report might be 1 or 2 pages long.
To create this report I just used a List report item and laid out a typical grade report and filled in the appropriate fields (name, teacher, etc.) from my dataset query.
When I run the report, it is running great and the students and their grade reports all print out.
The problem I am running into is I want to make sure each student's name is on page 2 if the grade report for that student goes more than 1 page (so pages aren't separated from each other or mixed up). I tried using a Report Footer, but that just put the first student's name at the bottom of every page. I could put a textfield at the bottom the list control but that would just appear at whatever length page 2 ended up being (basically you could have a "footer" at mid-page if the whole list only took up 1.5 pages).
How can I create a footer for each set of pages? Basically this would be a "footer" to the list control itself.
I have a report (bills of lading for shipments) that uses a table to organize the data coming in and groups by customer. Each customer grouping has a header, body and footer in the table. A print run might generate 100 bills of lading, each between 1 and 4 pages long.
I have marked the group to force a new page when each new group occurs.
As it is the report runs beautifully EXCEPT that the footer prints wherever it happens to end up -- midway down the final page of the bill and looks quite goofy.
What I really need (and my client is really surprised that SSRS won't me) is to print the group footer at the bottom of the final page of each bill of lading.
Bills of lading may be several pages per customer, so until the group footer is hit, I don't want any footer info at all. As I said, it all works fine except for the final page of each bill, where the group footer occurs whereever it has ended up.
Jim
12/4/07
This post didn't get much of a response. So perhaps I should amend it -- If what I am trying to do is impossible, what would be the preferred way?
I have a pretty simple query: Select title, avg(score) from votes group by title order by avg(score) Desc This returns a result like this: Abbey Road 4.0The White Album 3.5Meet the Beatles 3.0 The values in the score field are always 1, 2, 3, 4, or 5 What I really need from my query is 5 more columns (1, 2, 3, 4, and 5) with a count of how many votes each of those columns received. So the result set might be like this. Abbey Road 4.0 0 0 1 1 1 The White Album 3.5 0 1 0 0 1Meet the Beatles 3.0 2 2 2 2 2 I have tried to use the count function, but it counts all the values in the field. I can't figure out how to just count the values that match a certain criteria. When I tried to create subqueries, I got an error that said my subqueries where returning a multiple result sets. Any suggestions would be greatly appreciated, Chris
I am trying to count a column in my tbl. I have a table with the following, What i am trying to do is count the number of days of dtAttendance for an individual where bitpresent = 1. So this individual should have 2 days of attendance. I wrote some code belwo but it does not seem to count correctly, some it does and some it does not. intAssignedPersonnelID intUICID strSSN dtAttendance bitPresent intDrillStatus intMileage bitDatePayed210 1 333333333 5/26/2008 1 2 210 1 333333333 5/27/2008 1 2 210 1 333333333 5/28/2008 0 2
& "(select count(*) from saddotnet.dbo.tblAssignedPersonnel h where p.strSSN = h.strSSN and h.dtattendance <= p.dtattendance) " _ & "as count_attendance_as_at_dtattendance, p.dtattendance, bitpresent, intdrillstatus, " _ & "'<input name=chbxPresent' + convert(varchar(10), p.intAssignedPersonnelID) + ' type=checkbox>' as 'theCheckBox' " _ & "from cms.dbo.tblSIDPERS as s INNER JOIN " _ & "saddotnet.dbo.tblAssignedPersonnel as p on p.strSSN = s.sidstrSSN_SM where sidstrSSN_SM = left('" & SSN & "', 9)"
I have a table that is grouped by a date. Some records have the same date. I am just trying to return the total row count of the query.
Here is the sql:
SELECT ActiveWeek FROM wcWARS WHERE CreatedBy = 9034 AND FlagComplete = 1 GROUP BY ActiveWeek
I just want to return the total grouped column.... I thought Oracle had a ROWCOUNT function that returned the total rows of the query. I was unable to find one for SQL Server.
**Note: I already tried @@ROWCOUNT ~ This returns the total rows of the table.
Hello All, I have two tables A and B. A and B have column x in common. The relationship between A and B is one to many. I would like to count the number number of x's in table B and group by the x and then join this result back to table a. My attempt : SELECT COUNT(T1.x), T1.x, A.meeting_invite_idFROM (SELECT meeting_invite_idFROM REMINDER) T1, AWHERE A.meeting_invite_id *= T1.meeting_invite_idGROUP BY A.meeting_invite_id Anyone with any ideas.
assume that there are many stores listed in the table... how can i have the number of stores that has an ordercount?... ive tried some mdx queries but i still cant get it right...
I have two tables, Sales Headers and SalesLines. The SalesHeaders table will hold basic details of a sale, and the Sales Lines table will hold details of the items in the sale.
select distinct csd1.csd_orig + csd1.csd_subj + csd1.csd_type + csd1.csd_numb + csd1.csd_revi as "Document Reference No.", csd1.csd_labl + ' ' as "Description", csd1.csd_issu as "Docu Dt", csd1.csd_altr as "Alternate Number", csd2.csd_altr as "Transmittal Reference No.", csd2.csd_issu as "TIssu Dt", trd1.trd_recd as "TRecd Dt", apr1.apr_reqd as "RReqd Dt", case when apr1.apr_stat is null then 'Unknown / Pending / No Reply' else apc1.apc_libe end as "Document Status",
csd3.csd_altr as "Reply Reference No.", csd3.csd_issu as "RIssu Dt", trd2.trd_recd as "RRecd Dt"
order by csd1.csd_orig + csd1.csd_subj + csd1.csd_type + csd1.csd_numb + csd1.csd_revi
---
I need to count the resulting records of the query. How do i have to do this, could you please tell me ... I am new to SQl and I tried but could not solve it ...