Solved

SQL 2008 R2

Posted on 2013-01-21
9
283 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 500 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…

726 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