2 Group By In A Single Query
Jul 20, 2005
Say the following are the columns of a table
A B C D E F
Can a aggregate function like sum be applied to A like sum(a) and then
order by b and c similarly aggregate function on d and group by e and
f using a single query...
Regards,
Jai
View 2 Replies
ADVERTISEMENT
Feb 12, 2015
I have a code like this:
SELECT Node_ID,Day,Operation, AA,BB
FROM
(SELECT
CASE
WHEN Operation LIKE 'NOTIFY' THEN SUM(Total_request) ELSE 0 END AS AA,
CASE WHEN OPERATION LIKE 'SEARCH' THEN SUM(Total_requests) ELSE 0 END AS BB,Node_ID,DAY,Operation
[code]....
So i want to make two columns by the name of operation. in the real code AA and BB are calculates with many counters. My code doesn't work, I have an error: "not a single-group group function" .....
View 1 Replies
View Related
May 19, 2015
partition with single file group or multiple file group which one best.
we have some report running from partition table, few reports don't have any partition Key and after creating 400 partition with 400 file group it is slow.what is best practices to crate 400 file group or single file group.
View 9 Replies
View Related
May 30, 2008
eg say.
i have a table emp
data
----
id name phone no
1 smith 423-422-5226
1 smith 414-255-5252
2 george 511-522-2525
2 george 524-522-2428
........
........
i need output as
1 smith 423-422-5226, 414-255-5252
2 george 511-522-2525, 524-522-2428
.....
can u any one help me
View 1 Replies
View Related
Aug 19, 2006
Hello everyone, I've got a bit of an SQL mystery that I'm not sure how to solve. For some reason I just cant get my head around it. Here's the scenario:
Table A:
_____________
BidID - Int identity
AuctionID - int
BiderName - varchar(50)
bidAmount - money
______________________
Now obviously each Bid will have a Unique ID using BidID but the other rows will contain multiple bids per user, on many different items possibly.
BidID AuctionID BiderName BidAmount
1 4005 joeblow 100.00
2 4005 janedoe 101.00
3 4005 joeblow 107.00
4 4006 joeblow 100.00
5 4006 janedoe 105.00
6 4006 joeblow 106.00
I need to find out which Auctions JoeBlow is bidding on, but I dont need a table with Rows for every single one of his bids, just a distinct auctionID for his top bid so in this case the only thing returned would be
3 4005 joeblow 107.00
6 4006 joeblow 106.00
Any clues? I've been through sub querys, and stored procedures, and I cant get anything to work quite right.
Thanks in advance for your help.
View 4 Replies
View Related
Oct 12, 2007
How to get
Code Block
a record value instead of aggregated value with GROUP BY?
Assume that I have a PRODUCT_COMMENT table defined as below. It logs
the multiple comments for products. A product may have multiple
comments logged at different time.
Code Block
CREATE TABLE [dbo].[PRODUCT_COMMENT](
[COMMENT_ID] [int] IDENTITY(1,1) NOT NULL,
[PRODUCT_ID] [int] NOT NULL,
[COMMENT] [nvarchar](50) NULL,
[UPDATED_ON] [datetime] NOT NULL,
CONSTRAINT [PK_PRODUCT_COMMENT] PRIMARY KEY CLUSTERED
(
[COMMENT_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PRODUCT_COMMENT] WITH CHECK ADD CONSTRAINT
[FK_PRODUCT_COMMENT_PRODUCT] FOREIGN KEY([PRODUCT_ID])
REFERENCES [dbo].[PRODUCT] ([PRODUCT_ID])
GO
ALTER TABLE [dbo].[PRODUCT_COMMENT] CHECK CONSTRAINT
[FK_PRODUCT_COMMENT_PRODUCT]
I would like to use the following SQL statement to get the latest
comments for all products.
Code Block
SELECT PRODUCT_ID, COMMENT, UPDATED_ON
FROM PRODUCT_COMMENT
GROUP BY PRODUCT_ID
HAVING UPDATED_ON = MAX(UPDATED_ON)
But this leads to the following error:
Code Block
Column 'PRODUCT_COMMENT.UPDATED_ON' is invalid in the HAVING clause
because it is not contained in either an aggregate function or the
GROUP BY clause.
Is there a way to do that?
Thanks!
View 5 Replies
View Related
Jun 4, 2015
I have a query that pulls back task and user assigned. Each task can have multiple users assigned. I want to pull back the single task and all the users assigned in one row.Â
Current Query:
select
t.Name 'Task',
d.FirstName + d.LastName 'User'
from [dbo].[Tasks_TemplateAssignTo] a join
Task_Template t on a.template_id = t.ID join
Doctor d on d.id = a.provider_id
Results from query above:
TaskUser
Call CustomerJohn Smith
Call CustomerBetty White
Call CustomerTammy Johnson
Order suppliesGreg Bullard
Order suppliesJosephine Gonzalez
Expected Results:
TaskUser
Call CustomerJohn Smith, Betty White, Tammy Johnson
Order SuppliesGreg Bullard, Jospehine Gonzalez
View 4 Replies
View Related
Jan 10, 2014
I'd like to ask how you would get the OUTPUT below from the TABLE below:
TABLE:
id category
1 A
2 C
3 A
4 A
5 B
6 C
7 B
OUTPUT:
category count id's
A 3 1,3,4
B 2 5,7
C 2 2,6
The code would go something like:
Select category, count(*), .... as id's
from TABLE
group by category
I just need to find that .... part.
View 3 Replies
View Related
Feb 26, 2008
I am trying to count a column field in a single table and return two count values as one record set using group by.
field1 = group by (department) nvarachar
field2 = count (closed) datetime
I have tried using derived tables with no luck getting the desired result.
field2 is a datetime field as indicated I want a count for two conditions
1. WHERE field2 is null
2. WHERE field2 is not null
End Results would like this
======
Department | OpenItems | ClosedItems
Department1 | 32 | 24
Departmnet2 | 87 | 46
Department3 | 42 | 76
=======
I got it *almost* working with derived tables, but the group by function was not putting the department as one single row. I was getting multiple rows for departments.
I realize this is probably a simple answer and I am making this a lot harder than it actually is....
Any suggestions?
View 3 Replies
View Related
Sep 18, 2014
I've 2 tables QuestionAnswers and ConditionalQuestions and fetching data from them using CTE join and I'm seeing repetitive rows (not duplicate) like, If you have multiple answers for 1 question, the output is like
where london
where paris
where toronto
why us
why japan
why indonesia
I want to eliminate the repetitive question and group them as parent child items.
with cte as (
select cq.ConditionalQuestionID from ConditionalQuestions cq
inner join QuestionAnswers qa on cq.QuestionID=qa.QuestionID where cq.QuestionID=5 and qa.IsConditional='Y')
select distinct q.Question, a.Answer from QuestionAnswers qa
inner join Answers a on a.AnswerID = qa.AnswerID
inner join Questions q on q.QuestionID = qa.QuestionID
inner join cte c on c.ConditionalQuestionID = qa.QuestionID;
View 4 Replies
View Related
Nov 1, 2001
Hi all,
I have to change the date format for one database in a group of databases in my sql server 2000,Can you please tell me how to change the date format.
thanks in advance
View 1 Replies
View Related
Sep 29, 2015
I have an SSRS 2012 table report with groups; each group is broken ie. one group for one page, and there are multiple groups in multiple pages.
'GroupName' column has multiple values - X,Y,Z,......
I need to group 'GroupName' with X,Y,Z,..... ie value X in page 1,value Y in page 2, value Z in page 3...
Now, I need to display another column (ABC) in this table report (outside the group column 'GroupName'); this outside column itself is another column header (not a group header) in the table (report) and it derives its name partly from the 'GroupName' Â values:
Example:
Value X for GroupName in page 1 will mean, in page 1, column Name of ABC column must be ABC-X Value Y for GroupName in page 2 will mean, in page 2, column Name of ABC column must be ABC-Y Value Z for GroupName in page 3 will mean, in page 3, column Name of
ABC column must be ABC-Z
ie the column name of ABC (Clm ABC) Â must be dynamic as per the GroupName values (X,Y,Z....)
Page1:
GroupName          Clm ABC-X
X
Page2:
GroupName          Clm ABC-Y
Y
Page3:
GroupName          Clm ABC-Z
Z
I have been able to use First(ReportItems!GroupName.Value) in the Page Header to get GroupNames displayed in each page; I get X in page 1, Y in page 2, Z in page 3.....
However, when I use ReportItems (that refers to a group name) in the Report Body outside the group,
I get the following error:
Report item expressions can only refer to other report items within the same grouping scope or a containing grouping scope
I need to get the X, Y, Z ... in each page for the column ABC.
I have been able to use this - First(Fields!GroupName.Value); however, I get ABC-X, ABC-X, ABC-X in each of the pages for the ABC column, instead of ABC-X in page 1, ABC-Y in page 2, ABC-Z in page 3, ...
View 4 Replies
View Related
Jun 14, 2007
Hi,
I have received a text file in the following format:
MthYear Customer Quantity Price Total
Apr2003 Allan 100 5 500
--------- Austin 25 2 50
--------- George 1500 1 1500
---------- Jessy 200 2 400
Apr2004 Jerry 600 3 1800
--------- Stella 250 2 500
June2005 XXXX 50 5 250
I am exporting this text file in Sql Server database table. After exporting I need to Update the MtnYear field marked as ------- to appropriate year. ie
The MthYear field for Austin, George,Jessy should be updated with Apr2003,
The MthYear field for Stella should be updated with Apr2004
iiily, for other records. Let me inform if this is possible with the help of a single query or any better way of doing it. The records that i need to update is more than 2 lakhs. I am sorry if u feel this questionis not apropriate.
View 3 Replies
View Related
Jan 7, 2007
Hello
I have a sql query that shows the top N numbers of records and also wanted to show the total numbers of this records.
Code:
.
.
.
rs_1.Open "SELECT top 5 msg_id, message, topic_id, last_post_by, id, sdate FROM forum_table",conn,1,3
rs_2.Open "SELECT msg_id, message, topic_id, last_post_by, id, sdate FROM forum_table",conn,1,3 “
Top number to show: <%=rs_1.recordcount%>
Total number of records in database: <%=rs_2.recordcount%>
.
.
.
As you can see, it is only on the same database that I am querying. How do I make a single sql query out of this?
View 3 Replies
View Related
Jul 6, 2004
i hav a table of the form
table EMP
(
EMPNAME varchar(50)
)
and the data in the table is
record #1 abc
record #2 abc
record #3 abc
record #4 abc
record #5 abc
record #6 abc
can i use a single query so as to retain a single record in the table and remove the rest ?
View 14 Replies
View Related
Mar 13, 2008
I have the following table:
id geounit parentgeounit
Here's some sample data:
1 Northeast null
2 Southeast null
3 New Jersey 1
4 Florida 2
Basically I need to return the parentgeounit's name, for instance for record #4 I'd want to return
4 Florida Southeast
Instead of:
4 Florida 2
Can this be accomplished?
View 3 Replies
View Related
Oct 29, 2006
So I have code to fill my repeater with a data: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim cnn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("db_ConnStr").ConnectionString) Dim cmd1 As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter("SELECT aspnet_Users.UserName, COUNT(forum_posts.post_id) AS IlePost, forum_posts_1.post_content, forum_posts_1.topic_id, forum_posts_1.post_id, forum_posts_1.post_date FROM aspnet_Users INNER JOIN forum_posts ON aspnet_Users.uID = forum_posts.user_id INNER JOIN forum_posts AS forum_posts_1 ON aspnet_Users.uID = forum_posts_1.user_id GROUP BY aspnet_Users.UserName, forum_posts_1.post_content, forum_posts_1.topic_id, forum_posts_1.post_id, forum_posts_1.post_date HAVING (forum_posts_1.topic_id = @topic_id) ORDER BY forum_posts_1.post_date ASC", cnn) cmd1.SelectCommand.Parameters.AddWithValue("@topic_id", Request.QueryString("ID")) Dim ds As Data.DataSet = New Data.DataSet cnn.Open() cmd1.Fill(ds, "posts") Repeater1.DataSource = ds.Tables("posts") Page.DataBind() cnn.Close() End Sub And I want to take the value from column which I bolded - IlePost. I want take this out at compare with some condition. For example:Dim label as label = Repeater1.Findcontrol("Label1")If COLUMN < 10 thenlabel.text = "Less than 10"elselabel.text = "More than 10"I hope you understand me :-)
View 1 Replies
View Related
Apr 29, 2008
Table Name:-emp
table structure:- Name Gender
A Male
B Male
C FeMale
D FeMale
I want only one update query to update this table like this
table structure:- Name Gender
A FeMale
B FeMale
C Male
D Male
please help me?
View 1 Replies
View Related
Sep 1, 2005
Hi Guys,This is my Problem.A table contain following desing for handling different level of categories, bu it is dynamic int_categoryid,int_parent_categoryid,int_categorylevel,str_categoryname,bit_activethatall.I want to list data from table as following ordercategorry_parent11 category_child12 category_child13 category_child23 category_child22categorry_parent21.............................................................................................like this..ie we can insert parent category and sub category to n level dynamically without adding a new tableplease mail me for another clarification....please help meregardsAbdul
View 2 Replies
View Related
Aug 30, 2005
Hi i am using Access as my database (i know its an MS SQL forum but that's the closest i could find, and maybe its more of an sql question).
I am trying to select all rows that contain i.e. the word "Art"
SELECT * FROM table1 WHERE column1 LIKE %Art%
as you can see column1 contain some words and not just "ART"
however i am getting also the following:
CART , ARTI, DARTS - since all has arts.
( i can use '=' since the column consist of other words)
The query should also load : Art. Art, Art! Art? and "ART"
Is there an easy solution or i need to create a huge condition ?
View 2 Replies
View Related
Jun 23, 2008
Hello All,
I have following type of table with data.
Year id value
01-01-2008 123 10
01-01-2008 131 11
01-01-2008 139 132
01-01-2007 153 112
01-01-2007 154 134
01-01-2007 155 164
I want to write query such that I should get value of particular year in single row as follows
Date Col 1 Col 2 Col 3
01-01-2008 10 11 132
01-01-2007 112 134 164
How can I write this type of query ? Please do let me know if anyone know how to write it ?
Thanks in advance.
--kneel
View 5 Replies
View Related
Feb 8, 2007
Hello Everyone,
Consider the below table
DocNbr Cheques
00001 101010
00002 101010
00002 102020
00003 103030
00004 103030
00004 104040
00005 105050
I just need the single select to result the docnbr which is repeating the values. In the above case I want my result like below where the DocNbr 00002 and 00004 repeated their values.
DocNbr Cheques
00002 101010
00002 102020
00004 103030
00004 104040
Thanks in advance,
Senthil .G
Note: I do not consider if the cheque contain repeated values
View 3 Replies
View Related
Jul 23, 2005
Hi groupI have a rather peculiar question, and I really don't know how to solvethis within an SQL statement:Given a view (v), that results in:IDX-----------------1a1b2a2c3aI'd like to query the view with something like:SELECT ID FROM v WHERE (X='a' AND X='b') which would result in:ID-----------------1or in another case:SELECT ID FROM v WHERE (X='a' OR X='c')would give:ID-----------------123how can this be done?TIAbernhard--www.daszeichen.chremove nixspam to reply
View 14 Replies
View Related
Oct 2, 2007
Hello,
I have a question which was given to me in a test. Question is :: print value from 1-20 in a single column using single select query without using any table.
If any one have solution, then please help me.
View 18 Replies
View Related
Oct 13, 2007
hello
i am a beginner
i it possible to get this result by single query ?
i got a int column and a datetime column
is it possible to get the sum(int column) for all the year 2007 and by the april2007 and by the month n year by the person name Tony
so the end result will look like this
year 2007 | current_month | tonys_group
-----------------------------------------------
$ 222 $22 $2
View 2 Replies
View Related
Jun 14, 2008
hi friends
in my sqlserver 2005 i want to update two tables in a single query.
is it possible? or is it possible by creating a view?
thanks in advance.
shweta
View 3 Replies
View Related
Feb 22, 2007
I have a query that I'm stumped on. The data has about 6000 records, of which about 460 of those have distinct dealer names and ids.
I am trying to condense this data so that there is only one record for each dealer, but my 3rd column has different values which is why some dealers have only 2 records, and others may have 10.
I know how to select distinct values, but what I want is to return a list of comma separated values for my 3rd column which consists of all the unique values from that 3rd column for that dealer.
For instance, say I have a dealer with 8 records. They have a unique ID and Dealer Name, but the 8 records appear because the dealer has 8 entries for its activity. The activities may or may not be unique.
So for example I have dealer ABC who has an id of 54, their name is ABC, but for their activites they have 8 entries (apple, orange, banana, pear, apple, banana, mango, peach).
I wnt to be able to select the distinct id, name, and distinct activities so that when my query is returned and displayed the record appears as such:
"54","ABC","apple, orange, banana, pear, mango, peach"
Does that make sense?
View 4 Replies
View Related
Oct 24, 2004
Hello,
I have a table with 1 column containing numeric values, and I need to create a query which displays the count of values for each set of conditions. For example, if the table contains the values 1,2,3,4,5 and 6, the output of the query should look like this:
L M H
-----------------------
3 2 1
(L are values below 4, M between 4 and 5, H is 6 and above)
Any suggestions?
TIA
View 1 Replies
View Related
May 5, 2004
I need to write a single query that will append the values from one table into another table but I need to update a single field in the table with a static value. What I'm working with is an Access .adp with an SQL 2000 backend. The database is used to track ticket sales, payments, and contact info for season ticket holders. Prior to upsizing, each year the database would be copied and then choice bits modified for the next year.
In other words, the history was in several databases, i.e. db2001, db2002, db2003 and for the current year dbcurrent. I decided to create a historic tables in the current DB rather than have umpteen DBs on my SQL server.
My problem is that I need to create a query that appends to my table the previous years data and then updates the season field to reflect the season that the data came from.
In short, say I have a table named accounts
with fields
account, customer, addr1, addr2, ..., ticket type
and I want it to end up in
account_hist with fields
account, customer, addr1, addr2, ..., ticket type, SEASON
and make the season equal say 8 which represents the 8th season the team has played.
I can get both queries to work seperately :D , but for end user ease, I want to perform both actions at the same time :confused: .
Can anybody point me in the right direction?
Thanks
View 2 Replies
View Related
May 13, 2004
SELECT 1 as id,COUNT(name) as count1
INTO #temp1
FROM emp
SELECT 1 as id,COUNT(name) as count2
INTO #temp2
FROM emp
WHERE name <>' ' AND name IS NOT NULL OR name <> NULL
SELECT (cast(b.count2 as float)/cast(a.count1 as float))*100 AS per_non_null_names
FROM #temp1 a INNER JOIN #temp2 ON a.id=b.id
View 9 Replies
View Related
Apr 15, 2008
Hi friends,
I have three table named as Eventsmgmt,blogmgmt,forummgmt..
Each table contain the common column named as CreatedDateTime..
I want to get the most recent CreationDateTime from these three table in single query..
Plzz help me its urgent
Thanks
View 20 Replies
View Related
Jun 17, 2014
I am using 3 tables: projects, project_manager and project_employee
projects
- project_id (int, PK)
- project_name
project_manager
- project_id (int, PK)
- manager_id (int, PK)
project_employee
- project_id (int, PK)
- employee_id (int, PK)
Assume that a manager is currently logged in (so we know what is his ID), what I trying to do is write a query that will display a grid-view showing:
project_id, project_name and number of employees in project.
I tried these quires to test:
SELECT project_id, count(emp_id) as NumberOfEmployees into #tempTable FROM project_employee WHERE project_id IN (SELECT project_id FrOM projects) GROUP BY project_id
SELECT projects.project_id, projects.project_name, NumberOfEmployees FROM projects, #tempTable WHERE projects.project_id = #tempTable.project_id
Problems are:
1 - I used two queries not one
2 - The result is for all projects of all managers, I just want to display projects for a specific manager (for example mag_id = 5)
3 - it created a temporary table - which I dont want.
Is there anyway to get what I want in a single query?
View 1 Replies
View Related
Oct 27, 2014
I have a huge database (20 columns and 56000 lines) and I need a query that can return each single unique character from all this database.
For example if character "a" appears at least one time in this database I need this query to show it to me.
View 2 Replies
View Related