Solved

SQL 2008 R2

Posted on 2013-01-21
9
280 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
 

Author Comment

by:HDM
ID: 38802638
Certificate number is TEXT and I need it to be numeric??
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now