473,403 Members | 2,284 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,403 software developers and data experts.

Insert columns from selected rows

112 100+
Hi all,
here my select command

SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) from itr
where itr_transdate between '2007-12-01' and '2007-12-31'
group by itr_glacctnbr
having itr_glacctnbr='121101'

This statement select only one glaccount but i have more than 15 glaccounts to select.

I can use this statement to select all glaccount but i want to select specific glaccounts

SELECT sUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) from itr
where itr_transdate between'2007-12-01' and '2007-12-31'
group by itr_glacctnbr


I can get result in rows that i want to inset into another table called 'testgl'

That table contains field of all different gl_accounts. fields like 121101, 121102,12103....etc


How to convert selected rows in column and insert on this table fields.

Thanks in advance for your answer.
Jan 22 '08 #1
10 1279
Delerna
1,134 Expert 1GB
I think I understand your question.
You want to insert more than the one row returned by the first query but not all the rows in the second ???

If so how about using more criteria in the first query
ie HAVING itr_glacctnbr='121101' or itr_glacctnbr='121105' or .......

Or maybe another table where you store the itr_glacctnbr numbers you want to insert and join it to the second query
Jan 22 '08 #2
imtmub
112 100+
Thanks for your reply.
I have tried using having still i am not getting the result. here my statement

SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) from itr
where itr_transdate between '2007-12-01' and '2007-12-31'
group by itr_glacctnbr
having itr_glacctnbr='121101'or itr_glacctnbr='121102'


This statement give only one result for 121101. not for 121102.

If i use below statement i can get the result for 121102.

SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) from itr
where itr_transdate between '2007-12-01' and '2007-12-31'
group by itr_glacctnbr
having itr_glacctnbr='121102'

But i want in one statement should give both result. because i want to filter 15 different itr_glaccounts in one statement.

I have more than 100 gl_accounts in my table but i want only few.
Jan 23 '08 #3
imtmub
112 100+
I think I understand your question.
You want to insert more than the one row returned by the first query but not all the rows in the second ???

If so how about using more criteria in the first query
ie HAVING itr_glacctnbr='121101' or itr_glacctnbr='121105' or .......

Or maybe another table where you store the itr_glacctnbr numbers you want to insert and join it to the second query
Thanks delerna,

Sorry for last reply. My table doesn't have any data with that date.

Now i have one more question.

I am getting the result like this for the select statement

45000
67786

First result for Itr_glacct 121101, 121102 respectively.


This result i need to insert in another table as one row.

Because in another table i have the fields like 121101, 121102..

First select command rows insert on the this table as single row.

121101, 121102
------------------------
45000, 67786
Jan 23 '08 #4
Delerna
1,134 Expert 1GB
is this a regular thing that you will need to do???
Will it be a different set of itr_glacctnbr each time
If so, may I suggest you create a table called something like tblGLAccsToInclude with 1 field called glacctnbr
Then you could add the GLAcc's you want to the table and change the query to
something like

Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) 
  2. from itr
  3. join tblGLAccsToInclude on itr_glacctnbr=glacctnbr
  4. where itr_transdate between '2007-12-01' and '2007-12-31'
  5. group by itr_glacctnbr
  6.  
if it's a one off thing then try changing it to
Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) 
  2. from itr
  3. where itr_transdate between '2007-12-01' and '2007-12-31'
  4.    and (itr_glacctnbr='100021' or itr_glacctnbr='100022' or itr_glacctnbr='100023' or ---)
  5. group by itr_glacctnbr
  6.  
Jan 23 '08 #5
Delerna
1,134 Expert 1GB
I take it that the GLAcc's are a fixed set of numbers.
Make your query a subquery by wrapping in brackets and add field itr_glacctnbr
then write a
Expand|Select|Wrap|Line Numbers
  1. Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as 121101,
  2.           sum(Case when itr_glacctnbr='121102' then Amt else 0 end) as 121102,
  3.           ......
  4. FROM
  5. (   SELECT itr_glacctnbr,
  6.     SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) as Amt from itr
  7.     where itr_transdate between '2007-12-01' and '2007-12-31'
  8.     group by itr_glacctnbr
  9.     having itr_glacctnbr='121101'  or  itr_glacctnbr='121102' ....
  10. )a
  11.  
Jan 23 '08 #6
imtmub
112 100+
is this a regular thing that you will need to do???
Will it be a different set of itr_glacctnbr each time
If so, may I suggest you create a table called something like tblGLAccsToInclude with 1 field called glacctnbr
Then you could add the GLAcc's you want to the table and change the query to
something like

Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) 
  2. from itr
  3. join tblGLAccsToInclude on itr_glacctnbr=glacctnbr
  4. where itr_transdate between '2007-12-01' and '2007-12-31'
  5. group by itr_glacctnbr
  6.  
if it's a one off thing then try changing it to
Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) 
  2. from itr
  3. where itr_transdate between '2007-12-01' and '2007-12-31'
  4.    and (itr_glacctnbr='100021' or itr_glacctnbr='100022' or itr_glacctnbr='100023' or ---)
  5. group by itr_glacctnbr
  6.  
Thanks Once again Delerna.

Both statements working fine. now i need to insert this result into another table. that table got fields of 121101, 121102.....

So i need to convert the results from rows to coloumn. so that i can use insert statement.
Jan 23 '08 #7
Delerna
1,134 Expert 1GB
I take it that the GLAcc's are a fixed set of numbers.
Make your query a subquery by wrapping in brackets and add field itr_glacctnbr
then write a
Expand|Select|Wrap|Line Numbers
  1. Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as 121101,
  2.           sum(Case when itr_glacctnbr='121102' then Amt else 0 end) as 121102,
  3.           ......
  4. FROM
  5. (   SELECT itr_glacctnbr,
  6.     SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) as Amt from itr
  7.     where itr_transdate between '2007-12-01' and '2007-12-31'
  8.     group by itr_glacctnbr
  9.     having itr_glacctnbr='121101'  or  itr_glacctnbr='121102' ....
  10. )a
  11.  
Does that help
Jan 23 '08 #8
imtmub
112 100+
I take it that the GLAcc's are a fixed set of numbers.
Make your query a subquery by wrapping in brackets and add field itr_glacctnbr
then write a
Expand|Select|Wrap|Line Numbers
  1. Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as 121101,
  2.           sum(Case when itr_glacctnbr='121102' then Amt else 0 end) as 121102,
  3.           ......
  4. FROM
  5. (   SELECT itr_glacctnbr,
  6.     SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) as Amt from itr
  7.     where itr_transdate between '2007-12-01' and '2007-12-31'
  8.     group by itr_glacctnbr
  9.     having itr_glacctnbr='121101'  or  itr_glacctnbr='121102' ....
  10. )a
  11.  
Does that help
I am getting error message like this

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '121101'.

Here is my query

Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as 121101,
sum(Case when itr_glacctnbr='121102' then Amt else 0 end) as 121102,
sum(Case when itr_glacctnbr='121103' then Amt else 0 end) as 121103,
sum(Case when itr_glacctnbr='122101' then Amt else 0 end) as 122101,
sum(Case when itr_glacctnbr='124101' then Amt else 0 end) as 124101,
sum(Case when itr_glacctnbr='124102' then Amt else 0 end) as 124102,
sum(Case when itr_glacctnbr='124104' then Amt else 0 end) as 124104,
sum(Case when itr_glacctnbr='124105' then Amt else 0 end) as 124105,
sum(Case when itr_glacctnbr='124107' then Amt else 0 end) as 124107,
sum(Case when itr_glacctnbr='124108' then Amt else 0 end) as 124108,
sum(Case when itr_glacctnbr='124109' then Amt else 0 end) as 124109,
sum(Case when itr_glacctnbr='124301' then Amt else 0 end) as 124301,
sum(Case when itr_glacctnbr='124302' then Amt else 0 end) as 124302,
sum(Case when itr_glacctnbr='124305' then Amt else 0 end) as 124305
FROM
( SELECT itr_glacctnbr,
SUM(dbo.ITR.ITR_TransQty * dbo.ITR.ITR_UnitCostAmt) as Amt from itr
where itr_transdate between '2007-12-01' and '2007-12-31'
group by itr_glacctnbr
Having itr_glacctnbr='121101'or
itr_glacctnbr='121102' or
itr_glacctnbr='121103' or
itr_glacctnbr='122101' or
itr_glacctnbr='124101' or
itr_glacctnbr='124102' or
itr_glacctnbr='124104' or
itr_glacctnbr='124105' or
itr_glacctnbr='124107' or
itr_glacctnbr='124108' or
itr_glacctnbr='124109' or
itr_glacctnbr='124301' or
itr_glacctnbr='124302' or
itr_glacctnbr='124305' )
Jan 23 '08 #9
Delerna
1,134 Expert 1GB
sorry, if you want a number as a field name it must be enclosed in [ ]
should be
Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as [121101],
etc
Jan 23 '08 #10
imtmub
112 100+
sorry, if you want a number as a field name it must be enclosed in [ ]
should be
Select sum(Case when itr_glacctnbr='121101' then Amt else 0 end) as [121101],
etc
Thanks a lot. It is working i have inserted the records to another table. thanks for your guidense.
Jan 23 '08 #11

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

Similar topics

8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
0
by: a | last post by:
I've read and split a delimited text file into a dataset. It looks fine in a datagrid (5 columns and 5,000 rows), but I've been trying, without success, to then insert the resulting dataset called...
4
by: Mel | last post by:
Hi, I created a form that would search a record from a table and showed the details in datagrid. I have another button that would insert the records showed in the datagrid to another table..How...
6
by: Rob | last post by:
I am trying to copy some data from one datagrid to another. The first datagrid containing data is called DocList. The blank Datagrid that I am trying to copy some data to is called DataGrid1. ...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.