473,385 Members | 1,901 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,385 software developers and data experts.

Syntax on View/Cross tab join

6
Hoping someone can help me here!
I'm having some problems trying to get the right syntax for a view, and am wondering if someone could point me in the right
direction!

My code is currently:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT a.Department,
  2.         a.Section,
  3.             SUM(CASE b.Type WHEN 'T' THEN b.LYAmt ELSE 0 END) AS TLYAmt,
  4.             SUM(CASE b.Type WHEN 'I' THEN b.LYAmt ELSE 0 END) AS ILYAmt,
  5.             SUM(CASE b.Type WHEN 'A' THEN b.LYAmt ELSE 0 END) AS ALYAmt,
  6.             SUM(CASE b.Type WHEN 'T' THEN b.CYAmt ELSE 0 END) AS TCYAmt,
  7.             SUM(CASE b.Type WHEN 'I' THEN b.CYAmt ELSE 0 END) AS ICYAmt,
  8.             SUM(CASE b.Type WHEN 'A' THEN b.CYAmt ELSE 0 END) AS ACYAmt    
  9.         FROM tbl_Departments a
  10.         INNER JOIN tbl_AccountDetail b ON
  11.         a.DeptCode = b.DeptCode
  12.         GROUP BY a.section
So basically a cross-tab join. What I would like to do however, is have multiple values in Type making up 'TLYAmt'; so rather than having

Expand|Select|Wrap|Line Numbers
  1. SUM(CASE b.Type WHEN 'T' THEN b.LYAmt ELSE 0 END) AS TLYAmt
I would be like to have
Expand|Select|Wrap|Line Numbers
  1. SUM(CASE b.Type WHEN 'T' or 'P' or 'H' THEN b.LYAmt ELSE 0 END) AS TLYAmt 
Except of course that that syntax doesn't work!
(so for example, if LyAmt for T was 1, P was 2, and H was 3, TLYAmt would return 6. Just to make it a bit more complicated, I wouldn't have multiple values for each CASE statement.

I've googled around and can't seem to find a solution, so am wondering if anyone knows whether this can be done? If it can't (and I am fast beginning to believe I am barking up the wrong tree!), can someone suggest something that might work in it's place?

Many thanks!
Dec 2 '08 #1
2 2234
ck9663
2,878 Expert 2GB
OR does not work that way...

try:

Expand|Select|Wrap|Line Numbers
  1. SUM(CASE WHEN b.Type  = 'T' or b.Type  = 'P' or b.Type  = 'H' THEN b.LYAmt ELSE 0 END) AS TLYAmt 
  2.  
for cleaner code, you can also

Expand|Select|Wrap|Line Numbers
  1. SUM(CASE WHEN b.Type  in ('T', 'P','H') THEN b.LYAmt ELSE 0 END) AS TLYAmt 
  2.  

Happy coding!
Dec 2 '08 #2
klaul
6
That works fantastically - thank you very much!
Dec 5 '08 #3

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

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
8
by: xixi | last post by:
when i create a join view like this create view JV104FZ.APJTINM1 (APAM32, APNO20, APQY05, PONO01, PONO05, PONO19, POCD01, POCD13, systimestamp, loginname, id ) as select JV104FZ.APPTINM.APAM32,...
3
by: Ryan | last post by:
Hello, I am trying to create a view that shows the following Field1: Sum of Amounts from Table A Field2: Count of Amounts from Table A Field3: Sum of of Amounts from Table B Field4: Count...
3
by: Doug | last post by:
Hi, I'm more familiar with MSSQL than Access syntax and have run into a problem with correctly putting ( )'s around the joins in a 3 table query. I want to INNER JOIN lenders and accounts and...
0
by: Gabriele Bertolucci | last post by:
Before saying anything more, I'm sorry for cross-posting, but the topic should be relevant for every newsgroups I posted in. ================================================== Access XP. ADP....
4
by: Zvonko | last post by:
Hi all! I was wondering if this is possible: To make things simpler, I have a table: CREATE TABLE example (text VARCHAR(30), number INTEGER);
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.