?
Solved

SQL 2008 R2

Posted on 2013-01-21
9
Medium Priority
?
286 Views
Last Modified: 2013-01-21
I have a table called INSPECTION.

In that table I have the following columns:
      ,[APPLICANT]
      ,[LOCATION]
      ,[REMARKS]
      ,[QUANTITY]
      ,[COUNT]
      ,[CONTAINER]
      ,[GRADE]
      ,[CERTIFICATE NUMBER]
      ,[CAR NUMBER]
      ,[QUANTITY THOUSAND WEIGHT]
      ,[TIME INSPECTION BEGUN]
      ,[TIME INSPECTION COMPLETED]

I would like to create a column or view or table that would add the numbers together from the QUANTITY THOUSAND WEIGHT column if the number in the CERTIFICATE NUMBER are the same.

Example:

73496            144550
73496            6300
73496            4250
73496            58800
73496            68700
73496            6500
73496            5952
73496            40000
73496            1600
73496            6420
73496            347800
73496            53500
73498            90850
73498            25000
73498            20000
73498            7760
73498            57750
73498            70950
73498            10850
73498            348500
73498            5600
73498            12600

I would now have a column called CERTIFICATE NUMBER.

In that column I would only see the certificate number 73496 once with the total of 744372, and certificate number 73498 once with a total of 649860.

Is this doable??
0
Comment
Question by:HDM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 38802462
SELECT [CERTIFICATE NUMBER]
      ,SUM([QUANTITY THOUSAND WEIGHT]) as SUM_QTY_THOUS_WT
FROM INSPECTION
GROUP BY [CERTIFICATE NUMBER]
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38802512
>I would now have a column called CERTIFICATE NUMBER
Is this another table , otherwise
Why do you want to have this column in the table ? You already have  data and you can get this info using the above query 'virtuadept' provided; or else you can create a view with same query; I dont really recommend storing the
0
 

Author Comment

by:HDM
ID: 38802633
When I try to run that query I get the error:
Executed SQL statement:  SELECT [CERTIFICATE NUMBER]
      ,SUM([QUANTITY THOUSAND WEIGHT]) as SUM_QTY_THOUS_WT
FROM INSPECTION
GROUP BY [CERTIFICATE NUMBER]
Error source:  .Net SQL Client Data Provider
Error message: The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:HDM
ID: 38802638
Certificate number is TEXT and I need it to be numeric??
0
 

Author Comment

by:HDM
ID: 38802661
Yes, I found this to be true.

Thanks for your help ALL!!  :-)
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 38802688
Yes. You can either change the table definition to not be TEXT or you could also do this:


SELECT CONVERT(VARCHAR(MAX),[CERTIFICATE NUMBER]) as Certificate_Number
,SUM([QUANTITY THOUSAND WEIGHT]) as SUM_QTY_THOUS_WT
FROM INSPECTION
GROUP BY CONVERT(VARCHAR(MAX),[CERTIFICATE NUMBER])
0
 

Author Comment

by:HDM
ID: 38802732
For this view, if I wanted to add the column for the applicant and the date associated, how could I accomplish this??

Or should I post a second question for it??

Thanks!!
0
 
LVL 8

Accepted Solution

by:
virtuadept earned 2000 total points
ID: 38802870
Assuming there is only one applicant per certificate number, you could do:

SELECT
      ,[APPLICANT]
      ,[CERTIFICATE NUMBER]
      ,SUM([QUANTITY THOUSAND WEIGHT]) as Sum_Qty_Thous_Wt
      ,MAX([TIME INSPECTION COMPLETED]) as Last_Inspected_Time
FROM INSPECTION
GROUP BY [APPLICANT],[CERTIFICATE NUMBER]

Your SELECT has to have the same columns as GROUP BY except for any columns that you use an aggregate function on such as SUM or MAX.
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 38802889
Also if you have TEXT columns for those you can get around with CONVERT(VARCHAR(MAX),column_name) in your SELECT and GROUP BY clauses.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

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