473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting data out of table

parshupooja
159 100+
I have table with following columns

period (i have multiple records per period)
year
Employee
Type
expensecode
netpay

I want to extract information like this

period-----year------servicecharge------expense-------bonus-----request----total
89-----------2007--------912-------------------10-----------------20------------10--------952

service charge should be where type =4 or 3 or(expensecode= 70001)
expense will be where type = 2 and expensecode <> 70001 or 7000
bonus will be where type = 2 and expensecode = 7000
request will be where type = 1

i am able to get this values individiually eg: service charge but don't know how to make it all together

select period, year, sum(netpay) as servicecharge from payment table where employee = "jim"
where type between 4 and 3 and (expensecode= 70001) group by period, year

result

period year servicecharge
89 2007 912


Plz help
Oct 19 '07 #1
5 1286
iburyak
1,017 Expert 512MB
Try this:

Expand|Select|Wrap|Line Numbers
  1. Select period, year, ServiceCharge, expense, bonus, request, 
  2. ServiceCharge + expense + bonus + request Total
  3. From (
  4.     select period, year, Employee,
  5.     sum(Case when type in(3,4) and expensecode= 70001 then netpay else 0 end) ServiceCharge,
  6.     sum(Case when type = 2 and expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when type = 2 and expensecode = 7000 then netpay else 0 end) bonus,
  8.     sum(Case when type = 1 then netpay else 0 end) request,
  9.     from table 
  10.     group by period, year, Employee)
  11.  
  12. where Employee = 'Jim'
Good Luck.
Oct 19 '07 #2
parshupooja
159 100+
Hey thanks for replying but it throws this error

Incorrect syntax near ')'. if i am not including where and if I include where it throws where Employee = 'Jim' . I also removed , near request(bolded down)
but still no use

sum(Case when type = 1 then netpay else 0 end) request,
Help Plz



Try this:

Expand|Select|Wrap|Line Numbers
  1. Select period, year, ServiceCharge, expense, bonus, request, 
  2. ServiceCharge + expense + bonus + request Total
  3. From (
  4.     select period, year, Employee,
  5.     sum(Case when type in(3,4) and expensecode= 70001 then netpay else 0 end) ServiceCharge,
  6.     sum(Case when type = 2 and expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when type = 2 and expensecode = 7000 then netpay else 0 end) bonus,
  8.     sum(Case when type = 1 then netpay else 0 end) request,
  9.     from table 
  10.     group by period, year, Employee)
  11.  
  12. where Employee = 'Jim'
Good Luck.
Oct 19 '07 #3
iburyak
1,017 Expert 512MB
Oops, sorry try this:

Expand|Select|Wrap|Line Numbers
  1. Select period, year, ServiceCharge, expense, bonus, request, 
  2. ServiceCharge + expense + bonus + request Total
  3. From (
  4.     select period, year, Employee,
  5.     sum(Case when type in(3,4) and expensecode= 70001 then netpay else 0 end) ServiceCharge,
  6.     sum(Case when type = 2 and expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when type = 2 and expensecode = 7000 then netpay else 0 end) bonus,
  8.     sum(Case when type = 1 then netpay else 0 end) request,
  9.     from table 
  10.     group by period, year, Employee) a
  11.  
  12. where Employee = 'Jim'
Oct 20 '07 #4
parshupooja
159 100+
Thank You for helping me out

Now I have another issue

I want to add 4 more coulmns to my resultset. 3 columns(paymentdate, startdate, enddate) comes from table1 and 1 column(hrs) comes from table2
all the tables have common column period and its unique in table1 and table2 but not not unique in table thats why we did group by

This is what i tried but it thows error

Expand|Select|Wrap|Line Numbers
  1. Select a.payementdate, a.startdate, a.enddate, c.hrs, b.ServiceCharge, b.expense, b.bonus, b.request, 
  2. b.ServiceCharge + b.expense + b.bonus + b.request Total
  3. From (
  4.     select 
  5.     sum(Case when b.type in(3,4) and b.expensecode= 70001 then b.netpay else 0 end) ServiceCharge,
  6.     sum(Case when b.type = 2 and b.expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when b.type = 2 and b.expensecode = 7000 then b.netpay else 0 end) bonus,
  8.     sum(Case when b.type = 1 then b.netpay else 0 end) request
  9.     from table b, table1 a, table2 c
  10.     group by b.period) a
  11.  
  12. where b.Employee = 'Jim' a.period = b.period and c.period = b.period

Oops, sorry try this:

Expand|Select|Wrap|Line Numbers
  1. Select period, year, ServiceCharge, expense, bonus, request, 
  2. ServiceCharge + expense + bonus + request Total
  3. From (
  4.     select period, year, Employee,
  5.     sum(Case when type in(3,4) and expensecode= 70001 then netpay else 0 end) ServiceCharge,
  6.     sum(Case when type = 2 and expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when type = 2 and expensecode = 7000 then netpay else 0 end) bonus,
  8.     sum(Case when type = 1 then netpay else 0 end) request,
  9.     from table 
  10.     group by period, year, Employee) a
  11.  
  12. where Employee = 'Jim'
Oct 22 '07 #5
parshupooja
159 100+
Nevermind, I figured out. I wrote 4 scalar function which return Paymentdate, enddate,startdate and hrs based on period

Thank You

Thank You for helping me out

Now I have another issue

I want to add 4 more coulmns to my resultset. 3 columns(paymentdate, startdate, enddate) comes from table1 and 1 column(hrs) comes from table2
all the tables have common column period and its unique in table1 and table2 but not not unique in table thats why we did group by

This is what i tried but it thows error

Expand|Select|Wrap|Line Numbers
  1. Select a.payementdate, a.startdate, a.enddate, c.hrs, b.ServiceCharge, b.expense, b.bonus, b.request, 
  2. b.ServiceCharge + b.expense + b.bonus + b.request Total
  3. From (
  4.     select 
  5.     sum(Case when b.type in(3,4) and b.expensecode= 70001 then b.netpay else 0 end) ServiceCharge,
  6.     sum(Case when b.type = 2 and b.expensecode not in( 70001, 7000) then netpay else 0 end) expense,
  7.     sum(Case when b.type = 2 and b.expensecode = 7000 then b.netpay else 0 end) bonus,
  8.     sum(Case when b.type = 1 then b.netpay else 0 end) request
  9.     from table b, table1 a, table2 c
  10.     group by b.period) a
  11.  
  12. where b.Employee = 'Jim' a.period = b.period and c.period = b.period
Oct 22 '07 #6

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

Similar topics

0
by: Jim | last post by:
I need some help getting started with a .NET web project for a commercial site. I am new to .NET and my understanding of some (but not all) of its concepts is a little sparse. I apologize for the...
0
by: Jim | last post by:
This si a repost, I apologize but perhaps my original inquiry got buried under all the usenet spam... I need some help getting started with a .NET web project for a commercial site. I am new to...
2
by: SenseForAll | last post by:
First please note I am a novice at VBA and not even that experienced with DAO/ADO and MS-SQL. Any assistance is appreciated. That said... I have an application written in Access w/ VBA. I need to...
6
by: melanieab | last post by:
Hi, Easy question. It seems to me that I'm following the examples correctly, but apparently I'm not. I'm trying to retrieve the data from a row called "row" and a column called "File". This is...
6
by: Brett | last post by:
Not sure what the problem is here... Trying to update from a datagrid to an access database using vb.net... Its not updating the database but Im not getting any errors... Here is my code... ...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
3
by: BrianDP | last post by:
I have a database with a split front end/back end. There is a key table in the back end called Catalog, and it is sort of a central key table for all sorts of things. It's a list of all the jobs...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
1
imrosie
by: imrosie | last post by:
Please help with this one,,,,,I've been trying everything in my arsenal to fix this one. I'm stumped.... I"ve got a unbound combo box (customername) that has two events (on click); AfterUpdate and...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...

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.