473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Analyst seeks help with one table query

3 New Member
SQL Server 2003 - SQL Query Analyzer. We have a database that stores service tickets and a table called non_part_usage. In this table, we have one field called request_id, one field called line_code_type and one field called quantity (there are more, but these are all we need for this question). Each request_id represents a service ticket. We have two line code types: L for Labor and T for Travel. The quantity field is a number of hours. One service ticket can have multiple Labor entries and multiple Travel entries (if technician went back to site 3 times, we would have 3 labor entries and 3 travel entries).

The output I am trying to get would like like this:

Request ID...........To tal Labor Hours.......... ..Total Travel Hours

I can write a query to get either total labor hours or total travel hours but I cannot figure out how to create and output both columns in one query. The individual queries are as follows:

SELECT request_id, sum(quantity) as Total_Labor_Hou rs
FROM non_part_usage
WHERE line_code_type = 'L'

SELECT request_id, sum(quantity) as Total_Travel_Ho urs
FROM non_part_usage
WHERE line_code_type = 'T'

The bosses will ask for this information a dozen different ways, but they always want to see the request id, the total number of labor hours and the total number of travel hours - and they want to read them across the report.

Any help will be greatly appreciated as this is a request I receive daily and one I desperately want to be able to fulfill. Our programmers are all tied up and my query skills are limited to the fundamentals. (Right now I list every line with the line code type, output to Excel and then pivot by request and sum on the line code type).

With sincere appreciation,
Brian
Sep 27 '07 #1
5 1386
mabubakarpk
62 New Member
Dear Try this one



SELECT tmp.request_id, sum(tmp.Total_L abor_Hours) as Total_Labor_Hou rs
,sum(tmp.Total_ Travel_Hours) as Total_Travel_Ho urs

From (

Select request_id,quan tity as Total_Labor_Hou rs,0 as Total_Travel_Ho urs FROM non_part_usage WHERE line_code_type = 'L'

Union ALL

Select request_id,0 as Total_Labor_Hou rs,quantity as Total_Travel_Ho urs FROM non_part_usage WHERE line_code_type = 'T'
) as tmp

group by tmp.request_id

Regards,
Sep 27 '07 #2
azimmer
200 Recognized Expert New Member
SQL Server 2003 - SQL Query Analyzer. We have a database that stores service tickets and a table called non_part_usage. In this table, we have one field called request_id, one field called line_code_type and one field called quantity (there are more, but these are all we need for this question). Each request_id represents a service ticket. We have two line code types: L for Labor and T for Travel. The quantity field is a number of hours. One service ticket can have multiple Labor entries and multiple Travel entries (if technician went back to site 3 times, we would have 3 labor entries and 3 travel entries).

The output I am trying to get would like like this:

Request ID...........To tal Labor Hours.......... ..Total Travel Hours

I can write a query to get either total labor hours or total travel hours but I cannot figure out how to create and output both columns in one query. The individual queries are as follows:

SELECT request_id, sum(quantity) as Total_Labor_Hou rs
FROM non_part_usage
WHERE line_code_type = 'L'

SELECT request_id, sum(quantity) as Total_Travel_Ho urs
FROM non_part_usage
WHERE line_code_type = 'T'

The bosses will ask for this information a dozen different ways, but they always want to see the request id, the total number of labor hours and the total number of travel hours - and they want to read them across the report.

Any help will be greatly appreciated as this is a request I receive daily and one I desperately want to be able to fulfill. Our programmers are all tied up and my query skills are limited to the fundamentals. (Right now I list every line with the line code type, output to Excel and then pivot by request and sum on the line code type).

With sincere appreciation,
Brian
One way is to set up a UNION query:
Expand|Select|Wrap|Line Numbers
  1. SELECT X.request_id, max(X.Total_Labor_Hours), max(X.Total_Travel_Hours)
  2. FROM
  3. (
  4. SELECT request_id, sum(quantity) as Total_Labor_Hours, NULL as Total_Travel_Hours
  5. FROM non_part_usage
  6. WHERE line_code_type = 'L'
  7. GROUP BY request_id
  8.  
  9. UNION
  10.  
  11. SELECT request_id, NULL as Total_Labor_Hours, sum(quantity) as Total_Travel_Hours
  12. FROM non_part_usage
  13. WHERE line_code_type = 'T'
  14. GROUP BY request_id
  15. ) as X
  16. GROUP BY X.request_id
  17.  
Sep 27 '07 #3
azimmer
200 Recognized Expert New Member
We did it independently.. . :-)
Sep 27 '07 #4
bpbull
3 New Member
Awesome job! Greatly appreciated! Worked like a charm.

Thank You,
bpbull


Dear Try this one



SELECT tmp.request_id, sum(tmp.Total_L abor_Hours) as Total_Labor_Hou rs
,sum(tmp.Total_ Travel_Hours) as Total_Travel_Ho urs

From (

Select request_id,quan tity as Total_Labor_Hou rs,0 as Total_Travel_Ho urs FROM non_part_usage WHERE line_code_type = 'L'

Union ALL

Select request_id,0 as Total_Labor_Hou rs,quantity as Total_Travel_Ho urs FROM non_part_usage WHERE line_code_type = 'T'
) as tmp

group by tmp.request_id

Regards,
Sep 28 '07 #5
bpbull
3 New Member
You folks are awesome and your response time is phenomenal. Both solutions worked great and will be the foundations of many queries to come. I googled all over and could not find this solution...than ks for being here and for taking the time to help a novice!

Sincerely,
bpbull

One way is to set up a UNION query:
Expand|Select|Wrap|Line Numbers
  1. SELECT X.request_id, max(X.Total_Labor_Hours), max(X.Total_Travel_Hours)
  2. FROM
  3. (
  4. SELECT request_id, sum(quantity) as Total_Labor_Hours, NULL as Total_Travel_Hours
  5. FROM non_part_usage
  6. WHERE line_code_type = 'L'
  7. GROUP BY request_id
  8.  
  9. UNION
  10.  
  11. SELECT request_id, NULL as Total_Labor_Hours, sum(quantity) as Total_Travel_Hours
  12. FROM non_part_usage
  13. WHERE line_code_type = 'T'
  14. GROUP BY request_id
  15. ) as X
  16. GROUP BY X.request_id
  17.  
Sep 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1604
by: Sunil Chaudhary | last post by:
JOB SUMMARY: The Business Analyst will be responsible for much of the modeling, tracking, reporting, analysis, and forecasting. The successful candidate will work with Business Managers to develop relevant reporting and conduct critical data analysis pertient to the understanding and improvement of customer behavior. The Business Analyst will provide analytic support in determining the factors affecting website visit behavior,...
11
2022
by: Ian | last post by:
'm hoping someone can help me resolve a problem I am having with stylesheets. I've only just started usinbg them so it's more than likely something similar. Trouble is I don't seem to be able to fix it so would be grateful for any help. Basically I'm working with a web page I inherited which has a menu in which the layout/colours are something like this: Heading (always yellow) - Link (always white irrespective of visited or not...
4
2054
by: bhbgroup | last post by:
I have a query on one large table. I only add one condition, i.e. a date (the SQL reads like 'where date > parameterdate'. This query is rather quick if 'parameterdate' is either explicitly written into the query or if it is a 'normal' access parameter value that is entered during the query. If I however create a separate parameter table that contains nothing but the date I want to use in the query and then refer to this table (the query...
0
1316
by: James Johnson | last post by:
I have a file that I open a FileStream and a StreamReader for. I issue a SEEK and read a series of records. I then issue another SEEK, no errors, but when I issue the read it picks up where the first read stopped. I have to close the FileStream and the StreamReader, reopen them and then issue the second SEEK. I tried issuing a FLUSH before the second SEEK but the results were the same.
0
1273
by: googlegroups | last post by:
hi our company is recruiting several c++ analyst programmers for our singapore based client. we are looking for experienced c++ analyst programmers to work in both contract and permanent capacities, relocation is also offered. should you have investment or retail banking experience this will be beneficial. please feel free to contact me on 0044 (0) 870 720 1074 or james.tobin@cubicegg.com
0
1249
by: Greg Corradini | last post by:
Hello all, I'm having trouble inserting an SQL selection into a new MS Access table. I get a parameter error on my insert statement when I try this (see below for code and error msg). I'm not sure if 'insert' or 'update' is the route I should be taking. CODE: #Import Pythond Standard Library Modules import win32com.client, sys, os, string, copy, glob import mx.ODBC.Windows as odbc
0
2128
by: peejay750 | last post by:
I have an Access application that needs to open an Excel workbook which has Cognos links in it. When I open the Excel workbook with the Cognos links in it using Excel, the Cognos Analyst menu is there. However, when I open the Excel workbook from Access VBA by creating an instance of Excel using Set objXL = CreateObject("Excel.Application") and then using objXL.Open to open the Excel workbook, the Cognos Analyst menu is not there. I found...
0
1555
by: LK~ICT | last post by:
Sri Lanka rural e-learning project seeks corporate support Dec 04, 2007 (LBO) - A Sri Lankan e-learning initiative for rural students is seeking corporate sector support to expand and cover 400 computer centers around the island, its designers said. The Shilpa Sayura project initiated by eFusion, a local software company, is a learning tool for rural students who do not have the necessary number of teachers and lack resources to...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.