473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Having trouble with GROUP BY ...

SELECT TOP 25 *
FROM new_ticket
WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC)
ORDER BY group_num DESC


I am using the above method to get pagination with PHP Script.
It is running on MS SQL Server 2005 Express Edition.

After long been trying, I haven't found a way to get them grouped.
I modified very last line "GROUP BY group_num" and didn't work!

Is anyway this can be grouped and arrayed DESC order?
I need someone very Smart and save me!!
Sep 13 '07 #1
7 2362
ck9663
2,878 Expert 2GB
SELECT TOP 25 *
FROM new_ticket
WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC)
ORDER BY group_num DESC


I am using the above method to get pagination with PHP Script.
It is running on MS SQL Server 2005 Express Edition.

After long been trying, I haven't found a way to get them grouped.
I modified very last line "GROUP BY group_num" and didn't work!

Is anyway this can be grouped and arrayed DESC order?
I need someone very Smart and save me!!

am not a very smart but i can try to help you...

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 25 group_num
  2. FROM new_ticket 
  3. WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC) 
  4. GROUP BY group_num 
  5. ORDER BY group_num
  6.  
that is, if group_num is all you need. as a rule of thumb, you have to include in GROUP BY all non-aggreate functions in your select list
Sep 14 '07 #2
Thanks for your help. But, that's not exactly what I want.
There are more columns other than group_num and I'd like to display all other colunms based on the group_num displayed.

What I want to see is that other columns lined up according to the group numbers resulted from your SQL query.

Thanks



am not a very smart but i can try to help you...

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 25 group_num
  2. FROM new_ticket 
  3. WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC) 
  4. GROUP BY group_num 
  5. ORDER BY group_num
  6.  
that is, if group_num is all you need. as a rule of thumb, you have to include in GROUP BY all non-aggreate functions in your select list
Sep 14 '07 #3
ck9663
2,878 Expert 2GB
Thanks for your help. But, that's not exactly what I want.
There are more columns other than group_num and I'd like to display all other colunms based on the group_num displayed.

What I want to see is that other columns lined up according to the group numbers resulted from your SQL query.

Thanks

if you don't have duplicate group_num then this would be it

Expand|Select|Wrap|Line Numbers
  1.       SELECT TOP 25 *
  2.       FROM new_ticket
  3.       WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC)
  4.       ORDER BY group_num
Sep 14 '07 #4
Your worst guess was right. There are duplicate group_nums
I tried JOIN but didn't work.

id , group_num , issue
1 , 1 , A
------------------------------------------------------
2 , 1 , B
------------------------------------------------------
3 , 2 , C
------------------------------------------------------
4 , 2 , D
------------------------------------------------------



I want to see somthing like this.
id , group_num , issue
-------------------------------------
1 , 1 , A
--------------------------------------
3 , 2 , C
-------------------------------------



if you don't have duplicate group_num then this would be it

Expand|Select|Wrap|Line Numbers
  1.       SELECT TOP 25 *
  2.       FROM new_ticket
  3.       WHERE group_num NOT IN (SELECT TOP 75 no FROM new_ticket ORDER BY no DESC)
  4.       ORDER BY group_num
Sep 14 '07 #5
ck9663
2,878 Expert 2GB
Your worst guess was right. There are duplicate group_nums
I tried JOIN but didn't work.

id , group_num , issue
1 , 1 , A
------------------------------------------------------
2 , 1 , B
------------------------------------------------------
3 , 2 , C
------------------------------------------------------
4 , 2 , D
------------------------------------------------------



I want to see somthing like this.
id , group_num , issue
-------------------------------------
1 , 1 , A
--------------------------------------
3 , 2 , C
-------------------------------------

what's the factor to consider 1,1,A instead of 1,1,B ? that would affect your query. if this is a troubleshooting ticket table, are you planning to get the first record or the latest one?
Sep 14 '07 #6
I am migrating MYSQL to MS-SQL. MYSQL script works fine
when use * with GROUP BY group_num and it shows all fileds in each row.
but Mssql doesn't support SELECT * and GROUP BY one_column.

id , group_num , issue, customer_name
1 , [1] , Wiper , john
------------------------------------------------------
2 , [3], Transmission , bill
------------------------------------------------------
3 , [3] , Engine , mike
------------------------------------------------------
4 , [3] , Engine , david
------------------------------------------------------


I want to see somthing like this.
As long as group_num shows only unique number, that will eliminate my headaches. The 2nd row could be 2 ,[3] Engine, david. As long as only one group_num appears on the list page that will be okay.

1 , [1] , Wiper , john
------------------------------------------------------
2 , [3] , Transmission , bill
------------------------------------------------------



what's the factor to consider 1,1,A instead of 1,1,B ? that would affect your query. if this is a troubleshooting ticket table, are you planning to get the first record or the latest one?
Sep 15 '07 #7
ck9663
2,878 Expert 2GB
I am migrating MYSQL to MS-SQL. MYSQL script works fine
when use * with GROUP BY group_num and it shows all fileds in each row.
but Mssql doesn't support SELECT * and GROUP BY one_column.

id , group_num , issue, customer_name
1 , [1] , Wiper , john
------------------------------------------------------
2 , [3], Transmission , bill
------------------------------------------------------
3 , [3] , Engine , mike
------------------------------------------------------
4 , [3] , Engine , david
------------------------------------------------------


I want to see somthing like this.
As long as group_num shows only unique number, that will eliminate my headaches. The 2nd row could be 2 ,[3] Engine, david. As long as only one group_num appears on the list page that will be okay.

1 , [1] , Wiper , john
------------------------------------------------------
2 , [3] , Transmission , bill
------------------------------------------------------
how's this:

Expand|Select|Wrap|Line Numbers
  1. select * from new_ticket a inner join 
  2. (select group_num, max(id) as id from new_ticket group by group_num) b on a.group_num = b.group_num and a.id = b.id
  3. where <that condition you have to check for the top 75% or something>
  4.  
Sep 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
6
by: Jeremy Cowles | last post by:
Hi all, Disclaimer: This app is actually written in a different language, but I have much respect for this group & wanted to get your input so please ignore any minor syntactical errors. I...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
1
by: mr_burns | last post by:
hi there, Ive been having a few problems recently with this group. Im not sure if any of the other groups have been giving me trouble but recently this one has a couple of times, when attempting...
10
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets...
1
by: Hervé Piedvache | last post by:
Dear all, I have a trouble with the pam authentification for PostgreSQL. I have add in the pg_hba.conf the good line ... and I have create a /etc/pam.d/postresql file which contains : auth ...
1
by: Jozef | last post by:
Hello. I'm having trouble creating a blank solution (and ASP.net web application) from my laptop. I own the server (in fact it's sitting right next to me) and have added the URL to the trusted...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
6
by: Pep | last post by:
Firstly, I'm not sure if this is the right group for this query, so please forgive me if I am wrong. My problem is that most users I distribute my software to cannot install it on their systems...
24
by: GesterX | last post by:
First of all I'm new to this site but it certainly looks like a place that i will be visiting more often! Onto my problem. I am creating a Hotel Bussiness project in java using BlueJ The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.