Advertisement

04.08.2008 at 06:49PM PDT, ID: 23306917
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Can this be done in SQL? Query to Group by Value

Tags: Microsoft, SQL Server, 2000, SQL, IE
I have a MSSQL View that outputs this:
        INVNUMBER              ITEM                     DATE        COMPANY   QTYSHIPPED INVFISYR
      INV73267                    500LAPACK              20020701        COMP1    1                2002
      INV73354                    KASU70                    20020703   COMP1    1                2002
      INV73356                    CAADLT2VM              20020703        COMP2    1                2002
      INV73356                    KASU70                    20020703   COMP2    1                2002
      INV73357                    KASU408                  20020703   COMP2    1                2002
      INV73362                    KASU70                    20020703   COMP2    1                     2002
      INV73363                    CAADLT2VM              20020703        COMP3    1                2002
      INV73366                    KASU408                  20020703   COMP4    1                2002

And this query (is actually stored procedure):

SELECT SUM(QTYSHIPPED), ITEM from [system-specific-product]
WHERE INVDATE > @StartDte AND INVDATE < @EndDte
GROUP BY ITEM

Which gives me a listing.

What I am after is to group certain items into groups...

So, the KASU70 and KASU408 are to be grouped together and totaled, however for every KASU70, it counts as 5..

I imagined a long case statement specifying each item in its area, or maybe a nested SELECT. But I would be happy for a stored procedure using User Definable functions if necessary.

I currently get:

Column1      ITEM
1.0000      OAS100SPVMBP
2.0000      KASU70
5.0000      OAS12PSTN
4.0000      KASU408

but would like this:

Totals        ITEM
1.0000      OAS100SPVMBP
6.0000      KASU_TTL
5.0000      OAS12PSTN

Thanks for looking.

Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: debuggerau
Solution Provided By: mark_wills
Participating Experts: 4
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
04.08.2008 at 08:02PM PDT, ID: 21311536

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 08:04PM PDT, ID: 21311542

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 08:07PM PDT, ID: 21311550

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 08:09PM PDT, ID: 21311559

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 08:09PM PDT, ID: 21311562

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 10:23PM PDT, ID: 21311962

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.08.2008 at 11:12PM PDT, ID: 21312136

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.09.2008 at 12:30AM PDT, ID: 21312527

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.09.2008 at 04:14AM PDT, ID: 21313587

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.09.2008 at 04:38PM PDT, ID: 21320197

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.08.2008 at 08:02PM PDT, ID: 21311536

Rank: Wizard

Will you be only grouping 1 type of item (KAS*** to KAS_TTL)? Can you give more samples?
 
04.08.2008 at 08:04PM PDT, ID: 21311542

Rank: Guru

Try:
1:
2:
3:
4:
5:
6:
7:
8:
9:
SELECT 
	SUM(QTYSHIPPED), 
	left(ITEM,4)	as ITEM
FROM
	[system-specific-product]
WHERE 
	INVDATE > @StartDte AND INVDATE < @EndDte
GROUP BY 
	left(ITEM,4)
Open in New Window
 
04.08.2008 at 08:07PM PDT, ID: 21311550
I don't see the reason why KASU70 and KASU408 should be group together as KASU_TTL. How to determine for this?

For short, you have to define a new table which has relation between KASU_TTL and KASU70 & KASU408.
 
04.08.2008 at 08:09PM PDT, ID: 21311559
>How to determine for this?
AHA, I just see. U70 U408 ... TTL.
 
04.08.2008 at 08:09PM PDT, ID: 21311562

Rank: Guru

And try to make sense of that grouping rule. You cant get 2 different values and group them into a third value, as your example shows . Actually the two data samples lists values that have nothing in common.

You need precise rules of grouping that apply to any value in ITEM column. Something like in my example.
 
04.08.2008 at 10:23PM PDT, ID: 21311962

Rank: Wizard

Hi, think I understand, though the data doesn't seem to add up to 6 in your example, and not exactly sure what you mean by every kasu070 counts as 5 - might need to explain that a bit. In the meantime, have a look at the code below - complete with sample table...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
--step 1 create a test table
create table tmp_invoices(invnumber varchar(20),item varchar(20),invdate datetime,company varchar(20),qtyshipped int,invfisyr int)
 
-- step 2 insert some test data
insert tmp_invoices values ('INV73267','500LAPACK','20020701','COMP1',1,2002)
insert tmp_invoices values ('INV73354','KASU70','20020703','COMP1',1,2002)
insert tmp_invoices values ('INV73356','CAADLT2VM','20020703','COMP2',1,2002)
insert tmp_invoices values ('INV73356','KASU70','20020703','COMP2',1,2002)
insert tmp_invoices values ('INV73357','KASU408','20020703','COMP2',1,2002)
insert tmp_invoices values ('INV73362','KASU70','20020703','COMP2',1,2002)
insert tmp_invoices values ('INV73363','CAADLT2VM','20020703','COMP3',1,2002)
insert tmp_invoices values ('INV73366','KASU408','20020703','COMP4',1,2002)
 
--step 3 pretend we are a SP and create the needed date variables (must run this lot as complete set - ie highlight to end and F5)
 
declare @startdte datetime
declare @enddte datetime
set @startdte = '01 Jan 2000'
set @enddte = '01 Jan 2009'
 
SELECT SUM(QTYSHIPPED), ITEM 
FROM (select case when item like 'KASU%' then 'KASU_TTL' else Item end as item, qtyshipped from tmp_invoices
      WHERE INVDATE > @StartDte AND INVDATE < @EndDte) a
GROUP BY ITEM
Open in New Window
 
04.08.2008 at 11:12PM PDT, ID: 21312136
Thanks Champs looks like we are getting somewhere, appreciate your help.

ok,
ee_rlee, no, I will be grouping about a hundred items down to about 10 groups.
They are arbitrarily defined by the sales manager (need I say more..)

Zberteoc, so your grouping according to the left fields, looks like my example has successfully shot me in the foot..

gnoon, another table is not a problem, if only I could find the syntax to draw upon. I'll need to think about this tonight.

mark_wills, your entry - "select case when item like 'KASU%' then 'KASU_TTL' else Item end" looks the most promising, however I cant see how to multiply 5 on a item except for seperating each item on a separate line. Since its all arbitrarily defined, I have no issue with this. Let me try a few experiments and update tomorrow.

Thanks to everyone. Due to my poor quality question, each of you will receive some points according to your ESP powers...


 
04.09.2008 at 12:30AM PDT, ID: 21312527

Rank: Wizard

That's simple... hence my question about the "counts as 5"

SELECT SUM(QTYSHIPPED), ITEM
FROM (select case when item like 'KASU%' then 'KASU_TTL' else Item end as item, case when item = 'KASU70' then qtyshipped * 5 else qtyshipped end as qtyshipped from tmp_invoices
      WHERE INVDATE > @StartDte AND INVDATE < @EndDte) a
GROUP BY ITEM
Accepted Solution
 
04.09.2008 at 04:14AM PDT, ID: 21313587

Rank: Guru

<<Thanks to everyone. Due to my poor quality question, each of you will receive some points according to your ESP powers...>>

You don't have to do that. What you should do is to reformulate your question so that we understand what you need exactly and then you'll get a clear answer, most likely.
 
04.09.2008 at 04:38PM PDT, ID: 21320197
Thanks Zberteoc,
I'll take that advice.

Thanks Mark_wills, worked a treat, thanks.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628