Solved

Showing multiple group values in one row

Posted on 2016-09-30
7
41 Views
Last Modified: 2016-10-06
Hi how can I show multiple vaules in the same group row
(not sure if this is the right way to say it)

example
table( PalletNo, box weight, count)
Values
19,10,1
19,20,1
19,20,1
19,20,1

How do I querie this so that it will display
Pallet,   boxes,            sum(count)
19,        20x3  10x1,    4
0
Comment
Question by:staleb
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 41823315
Hi Staleb,
Please try the following:

SELECT PalletNo, [box weight], COUNT(*)
FROM T
GROUP BY GROUPING SETS ((PalletNo), ([box weight]))

Open in new window

0
 

Author Comment

by:staleb
ID: 41823336
This gave me 3 rows.
Can these three lines be combined into one?
0
 

Author Comment

by:staleb
ID: 41823659
think I may have solved it with stuff  function
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 40

Expert Comment

by:Sharath
ID: 41824267
What is your SQL Server version?
0
 

Author Comment

by:staleb
ID: 41825419
SQL 2012
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41825858
check this.
;with cte as (
select  PalletNo, convert(varchar,box_weight) + 'x' + convert(varchar,count(*)) boxes, count(*) cnt 
  from your_table group by PalletNo, box_weight),
  cte2 as (
select PalletNo,
      rtrim(substring(isnull((select ','+boxes from cte t1 where t1.PalletNo = t2.PalletNo for xml path('')),' '),2,2000)) boxes,
	  cnt
  from cte t2)
select PalletNo, boxes,sum(cnt) total from cte2 group by PalletNo,boxes

Open in new window

example
declare @table table (PalletNo int, box_weight int, cnt int)
insert @table values (19,10,1),(19,20,1),(19,20,1),(19,20,1)


;with cte as (
select  PalletNo, convert(varchar,box_weight) + 'x' + convert(varchar,count(*)) boxes, count(*) cnt 
  from @table group by PalletNo, box_weight),
  cte2 as (
select PalletNo,
      rtrim(substring(isnull((select ','+boxes from cte t1 where t1.PalletNo = t2.PalletNo for xml path('')),' '),2,2000)) boxes,
	  cnt
  from cte t2)
select PalletNo, boxes,sum(cnt) total from cte2 group by PalletNo,boxes
/*
PalletNo	boxes	total
19	10x1,20x3	4
*/

Open in new window

0
 

Author Closing Comment

by:staleb
ID: 41833078
Yes that worked perfect.
Also made it work with a stuff command

Tnx
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query execution hang 5 31
Tsql query 6 22
Flattening heirachies 3 31
SQL view 2 27
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question