Solved

SQL 2008 R2

Posted on 2013-01-21
9
281 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I get blanks instead of zero's to show up in my results? 2 29
SQL Transaction logs 8 26
Filtered index 5 55
testing sql16 on win10 vs OS16 2 32
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 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

11 Experts available now in Live!

Get 1:1 Help Now