I have a report with a table and it has 58 rows, in some of the rows i am displaying the data like this
Roth contribution (Heading)
data for the roth contribution.
So depending on the options checked sometimes the heading Roth contribution is at the end of the page and the data is the next page. so how can i group 2 table rows together.
I have tried clicking on the 2 rows and grouping it, but nothing seems to appear in the report..
I have a table, Table1 with 3 columns as follows: colItemKey, colGrouping1, colGrouping2. colItemKey is the primary key. Say colGrouping1 has 4 different types: Grp1A, Grp1B, Grp1C and Grp1D and colGrouping2 has 5 as follows: Grp2A, Grp2B, Grp2C, Grp2D and Grp2E. How do I setup my select so that the result set is as follows:
I'm no expert at SQL, I can't figure out how to solve that problem.
The following statement:
SELECT TeamMemberId, max(MonthId) as MonthId FROM Position INNER JOIN [Month] m INNER JOIN [Year] y ON m.YearId = y.Id AND (y.Id = @yearId OR y.Id < @yearId) ON m.Id = Position.MonthId GROUP BY TeamMemberId
Returns TeamMemberId MonthId 1 14 6 20
which is exactly what I want: The greates MonthId for each TeamMember in a specific year.
But I need more data for this postition, like the PositionName.
But the statement:
SELECT TeamMemberId, max(MonthId) as MonthId, PositionName FROM Position INNER JOIN [Month] m INNER JOIN [Year] y ON m.YearId = y.Id AND (y.Id = 2 OR y.Id < 2) ON m.Id = Position.MonthId GROUP BY TeamMemberId, PositionName
Returns TeamMemberId MonthId PositionName 1 2 ***. d. Geschäftsführung 6 20 ***. d. Geschäftsführung 1 14 CEO 6 16 CEO
The red rows are the ones I wanna get. How can I achieve that only the max. MonthId rows are returned, even when I need to select more columns? Any help is much appreciated.
I am using Sql Server 2008 R2.I have a existing query that basically says
Select Top 50 Subscriber_ID, Â Member_Name, Group_ID from my_table order by rand(checksum(newid()))
However the client now wants to have at least two from each group_id. There are 17 different groups. When I run this as is I get about six of the 17 groups in the results. How can I change this to get at least two results from each group_id?
insert into Channels values (1, 'Channel 1'); insert into Channels values (2, 'Channel 2'); insert into Channels values (3, 'Channel 3'); insert into Channels values (4, 'Channel 4');
insert into Blogs values ('1', 'blah'); insert into Blogs values ('3', 'blah'); insert into Blogs values ('4', 'blah'); insert into Blogs values ('12', 'blah'); insert into Blogs values ('34', 'blah'); insert into Blogs values ('35', 'blah'); insert into Blogs values ('67', 'blah');
insert into Episodes values (1, 3, '', '', 'Episode 1, blog 3'); insert into Episodes values (2, 3, '', '', 'Episode 2, blog 3'); insert into Episodes values (3, 3, '', '', 'Episode 3, blog 3'); insert into Episodes values (4, 3, '', '', 'Episode 4, blog 3'); insert into Episodes values (5, 1, '', '', 'Episode 5, blog 1'); insert into Episodes values (6, 1, '', '', 'Episode 6, blog 1'); insert into Episodes values (7, 1, '', '', 'Episode 7, blog 1'); insert into Episodes values (8, 4, '', '', 'Episode 8, blog 4'); insert into Episodes values (9, 4, '', '', 'Episode 9, blog 4'); insert into Episodes values (10, 4, '', '', 'Episode 10, blog 4'); insert into Episodes values (11, 4, '', '', 'Episode 11, blog 4'); insert into Episodes values (12, 4, '', '', 'Episode 12, blog 4'); insert into Episodes values (13, 12, '', '', 'Episode 13, blog 12'); insert into Episodes values (14, 12, '', '', 'Episode 14, blog 12'); insert into Episodes values (15, 12, '', '', 'Episode 15, blog 12'); insert into Episodes values (16, 12, '', '', 'Episode 16, blog 12'); insert into Episodes values (17, 12, '', '', 'Episode 17, blog 12'); insert into Episodes values (18, 34, '', '', 'Episode 18, blog 34'); insert into Episodes values (19, 34, '', '', 'Episode 19, blog 34'); insert into Episodes values (20, 34, '', '', 'Episode 20, blog 34'); insert into Episodes values (21, 34, '', '', 'Episode 21, blog 34'); insert into Episodes values (22, 35, '', '', 'Episode 22, blog 35'); insert into Episodes values (23, 35, '', '', 'Episode 23, blog 35'); insert into Episodes values (24, 35, '', '', 'Episode 24, blog 35'); insert into Episodes values (25, 35, '', '', 'Episode 25, blog 35'); insert into Episodes values (26, 67, '', '', 'Episode 26, blog 67'); insert into Episodes values (27, 67, '', '', 'Episode 27, blog 67'); insert into Episodes values (28, 67, '', '', 'Episode 28, blog 67'); insert into Episodes values (29, 67, '', '', 'Episode 29, blog 67'); insert into Episodes values (30, 67, '', '', 'Episode 30, blog 67');
insert into BlogAssociations values (3,'',1); insert into BlogAssociations values (12,'',1);
There is my above sql script, i'm currently using mysql at home to test things but will convert to work in mssql. I need to get associated BlogID for Certain ChannelID from the BlogAssociations table. Then using those BlogID's that it finds for let just say ChannelID "1" list off the 3 most recent EpisodeID's from the Episodes Table per each BlogID. So if ChannelID "1" has four BlogID's associated with it then in the Episodes table list off the recent 3 EpisodeIDs for BlogID "3", then the 3 most recent EpisodeIDs for BlogID "5" and so on and so on.
I have this which kind of does what I want:
Code:
select e.BlogID, e.EpisodeID, e.other FROM episodes e, BlogAssociations ba WHERE e.BlogID = ba.BlogID and ba.ChannelID = '1' ORDER BY e.EpisodeID DESC;
It returns:
Code:
+--------+-----------+---------------------+ | BlogID | EpisodeID | other | +--------+-----------+---------------------+ | 12 | 17 | Episode 17, blog 12 | | 12 | 16 | Episode 16, blog 12 | | 12 | 15 | Episode 15, blog 12 | | 12 | 14 | Episode 14, blog 12 | | 12 | 13 | Episode 13, blog 12 | | 3 | 4 | Episode 4, blog 3 | | 3 | 3 | Episode 3, blog 3 | | 3 | 2 | Episode 2, blog 3 | | 3 | 1 | Episode 1, blog 3 | +--------+-----------+---------------------+
But as you can see it lists off 5 of the Episodes for BlogID "12", I only want the most recent 3 as previously stated. It also lists off more than 3 Episodes for BlogID "3". How in the world do I go about doing this? I'm making this a stored procedure so I can't use php otherwise I wouldn't even be posting
I'm new to MSSQL 2005 and want to get a summary of a log table. I want to count all the rows for each date based on a DATETIME field called 'post_date' that holds the date and time of each record's creation.
this is the best I can come up with:
Code:
SELECT DISTINCT(LEFT(post_date,11)) AS post_date, COUNT(DISTINCT(LEFT(post_date,11))) AS total_posts FROM log_directory_contacts GROUP BY post_date
The results show each date but the count column ('total_posts') returns '1' for every row even when I know their are more than 1 record on that date.
I have an nt group login 'prod1ame'. How can I know who all are members of this group(list of all nt individual users) from SQL server? I need to know this info without taking NT admins help.Is that possible??thanks.
Hi, In sql 7.0 , i would like to create a database with the size of 10Gb, in my server couple of databases already exist. How do i know how much free space is there in File group. we are having only one file group i.e PRIMARY. Could anyone pls tell me about this. Thank u.
I have a need to show a row inside a table group to simulate a header row for the data rows inside the group. The table will not have a real header or footer. Thanks for the help.
Hi,I'm trying to build a query that get only one row from a group ofrows, but I need the values from that row and not the results of onefunction group.I need one row for each idRef, with column2=2 and the bigger column1id |idRef | column1 | column21 1 0 12 1 1 23 1 2 14 2 0 15 2 1 26 2 2 17 2 3 2For these, I will take the rows with id=2 and id=7.Thank you, and sory for my english.
Okay, I am sure this is an easy question, but for some reason I cannot wrap my brain around it. I have a table that has data similar to the following:
IPAddress VisitDate URLVisited
192.154.21.554 9/18/2007 http://www.microsoft.com
192.154.21.554 9/19/2007 http://www.google.com
164.21.124.23 9/19/2007 http://www.microsoft.com
192.154.21.554 9/20/2007 http://www.yahoo.com
What I am trying to do is use a select query to find the most recent visit per IP address (but I also want the other row data like URL visited). I have something like this that is finding the most recent visit per IP address:
SELECT IPAddress, MAX(VisitDate) AS MaxDateVisited FROM VisitorTable GROUP BY IPAddress
How do I also pull in the URL that is associated with the MAX date visited? Thank you!
1. The table statistic shows for example 67 rows in a table, select count(*) only returns 63 rows. 2. Table statistic shows 50 rows, select count(*) returns 55 rows.
In both cases if you do an insert the newly inserted row sometimes can be retrieved by a select statement sometimes not. Row statistics sometimes is updated correctly, sometimes not.
Integrity check for these databases says everything is fine. DBCC CHECKDB, DBREINDEX, UPDATESTATISTICS, ... does not help or says everything is fine.
Already opened a case at HP's Microsoft support and they involved Microsoft itself but all are a little bit clueless at the moment.
As this is our main DB cluster and several databases are affected we had to stop most of our applications since last Thursday and now we are getting a little bit in trouble so any hint is very welcome.
Thanks in advance
PS: Restore is not an option - as all tools say everything is fine all of our backups from the last months include the error. Don't ask why nobody recognized the lost data earlier, seems they were stored but not required for some time.
Hi, I am trying to get the first row of what might be a group of any size of rows within an id that may contain a specific field. For eg
Row A = NoteID 1, FK_JobID 1, UnRead
Row B = NoteID 2, FK_JobID 1, UnRead
Row C = NoteID 3, FK_JobID 1, UnRead
I need the sql to return to just one Job (row) even though the job as 3 UnRead fields. But its returning 3 because its only doing what I'm asking. What I need it to do is just get the one Job (row) where any of the notes = UnRead.
I tried using Top 1, but that will only ever return one row and since I need it to return more than one job (row) it won't work.
Heres my attempt
DECLARE @UserID INT
SET @UserID = 4
SELECT User_Notes.BeenRead, Master_Jobs.By_Who, Master_Jobs.Next_Action, Master_Jobs.Due_Time, Master_Jobs.Due_Date, Master_Jobs.Contact, Master_Jobs.Job_Title, Master_Jobs.JobID FROM User_Notes INNER JOIN Note ON User_Notes.FK_UN_NoteID = Note.NoteID INNER JOIN Master_Jobs ON Note.FK_JobID = Master_Jobs.JobID
WHERE Note.FK_UserID = User_Notes.FK_UN_UserID AND BeenRead = 'UnRead'
creating a query to group identical rows into one and placing corresponding data in appropriate columns for a table named items, what I mean is that I have a table structured as below
As you can see each room is assigned and id and once the room changes the id starts from 1 again. I don't want the 2nd row to appear because 9am - 1pm is in 8am - 5pm. How do I remove this row?
My first post on the forum, I wish I had found this place sooner, looks to be full of good advice and knowledgeable posters.
I have tried searching and looking at the FAQ's but couldn't find an answer. So here goes, all help greatly appreciated.
SQL Server 2005
Table (many columns left out for simplicity)
ID - unique. HdrID - key to the header record. PTtimestamp - date, date/time the row was written to DB. PType - integer, representing various states of the row. etc etc etc (another 15 columns of data)....
Typically there is approximately 250 records per HdrID.
I am trying to do an SQL UPDATE without using my usual solution of writing vb code!
I want to update the PType of latest row (as per timestamp) for each HdrID to -9999.
I built a report with one field as a group. I want to count the number of rows in each field so I can add it to the group field or somw where in the report.
How can I count how many rows do I have in each group?
I need to delete the duplicate rows from a table. How to do that in SQL server 7.0 ? If possible write an example, so that it will be much useful for me..
Hi i have a table value which contains value ----- a a a b b b c c c
Now i need to have the results as
a 1
b 1
c 1
I tried using distinct.But OLEDB returns error that invalid syntax.It doesn't support distinct keyword.Actually i read these table from a file thru OLEDB.Not from a database.Any idea ? Thanks in Advance
I have a report and its been populating from a sproc. and i have 2 text boxes called both of them are poplulated by Fields!Investment Names, but right i can display the data left to right but i want to display the Data starting top to bottom and then towards the right.
I tried grouping the data in this way for one text box = CountRows()/2 > 10 . and this shows all the records one below the other, so is there a way that i can display half the records in one text box and the other half in the other text box.
I am going kinda nuts over this. Can someone please help me.
Hi everyone: I guess this should be a simple question but have not been able to find the answer, does anyone know how to make a SQL Sentence to return at least one row when counting? SELECT COUNT(Id_Field), Field2 FROM Table1 WHERE Code_Field = 1 GROUP BY Field2 This will return 0 rows when the where criteria is not matched by any record on the Table1, but I would like to have one row counting 0 rows, in stead it returns no rows at all. Thanks for any help.
hi, i have a stored procedure SELECT UserName AS Visitor, COUNT(VisitID) AS TotalVisit FROM UserVisits WHERE (ProductID = @ProductID) AND (AnonimIP IS NULL) GROUP BY UserName UNION SELECT AnonimIP AS Visitor, COUNT(VisitID) AS TotalVisit FROM UserVisits AS UserVisits_1 WHERE (ProductID = @ProductID) AND (UserName IS NULL) GROUP BY AnonimIP this will return something like: zuperboy90 - 4 visits ANONIMOUS - 6 visits 85.104.103 - 2 visits etc how can i count the rows returned in both selections (4+6+2 = 12) ? thank you
I have posted this in the Experts Forum so I have put replies to questions in here as well (Hence the long post!!). Hope someone here can help!!
I am having trouble with a T-SQL query. I have three tables in a join and I need to limit the results returned by query using a group by.
All of the fields in the result group are the same except for two fields, a date field and a varchar field. What I want to achieve is to return the row that has the latest date, and I need the varchar field as that is the information I am after. The problem is if I include the varchar field in the group by, it is returned as a separate group, if I do not included it in the group by, Server: Msg 8120 (aggregate fn / group by) error occurs!
Any ideas how to get around this?
Sample code and results below:
Query:
select distinct s.id, [active-from], code, s.desc, [scheme], [market-id], [market-id]+'CODE' from [coded] c join sec s on s.id = c.id join [mkt-security] m on m.id = s.id where ([market-id] = [scheme] or [market-id]+'CODE' = [scheme]) Group by s.id, [active-from], code, s.desc, [scheme], [market-id], [market-id]+'CODE'
i.e. those with the latest [active-from] date, but I must have the corresponding code value.
I have tried a correlated sub query on the [active-from] field, but it is possible to have different id’s with the same [active-from] date so it did not work.
The primary key for the [coded] table is the combination of [active-from], code and [scheme]. The tables are truncated and then imported back into the SQL database from a Progress database daily, so restructuring the table(s) is not a possibility.
I have rewritten the query to "AND [ACTIVE_FROM] in (SELECT MAX([ACTIVE_FROM]) FROM CODED AND MAX([CODE]) = S.[CODE] AND MAX([SCHEME]) = S.[SCHEME]", (Which I think is the T-SQL for you query), but it returned the error "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."
Also I think the query need to use a Group by as the results I need returned are those with the latest [ACTIVE_FROM] date WITHIN the group of [ID]and [MARKET-ID]. I can't think of any conditions where I can filter the results in the where clause.
The [ ] in the T-SQL is to identify table names as some of the table names (from the Progress db) use characters (the "-") that are unsupported for table names in T-SQL.
I am using the group by as I want to return specific groups of results ( [ID] and [MARKET-ID] ), that have the latest active-from date. I have made a change to that posted before to illustrate:
I have dataset ready having sales rep ID  and other column like customer ID, company name, Email adress and transaction month as shown above how can i make a  row group by sales rep id and each tab (when we export report to excel)  I mean one tab for A sales repID , another tab for B...I did row group  parent group and group by sales repID but not working as expected.