473,468 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL QUERY HELP

I have SQL records as follows

REGION PRODUCT Amount Week
IMDS A 100 Week1
IMDS A 250 Week2
IMDS A 150 Week3

IMDS B 525 Week1
IMDS B 400 Week2

IMDS C 500 Week2
IMDS C 40 Week3

IHYD A 50 Week1
IHYD A 52 Week2
IHYD A 100 Week3
IHYD D 20 Week2

I want to display these items in ASP as
REGION PRODUCT Week1 Week2 Week3

IMDS A 100 250 150
IMDS B 525 400 0
IMDS C 0 500 40
IHYD A 50 52 100
IHYD D 0 20 0
Is there any "SQL query" which helps me to bring the above output
without using
any front end calculation?

Jan 13 '06 #1
2 1169
(ve**********@gmail.com) writes:
REGION PRODUCT Amount Week
IMDS A 100 Week1
IMDS A 250 Week2
IMDS A 150 Week3
....
I want to display these items in ASP as
REGION PRODUCT Week1 Week2 Week3

IMDS A 100 250 150
IMDS B 525 400 0
IMDS C 0 500 40
IHYD A 50 52 100
IHYD D 0 20 0
Is there any "SQL query" which helps me to bring the above output
without using any front end calculation?


If your week colunms are fixed, then it is relatively simple:

SELECT region, product,
week1 = SUM(CASE Week WHEN 'Week1' THEN Amount ELSE 0 END),
week2 = SUM(CASE Week WHEN 'Week2' THEN Amount ELSE 0 END),
week3 = SUM(CASE Week WHEN 'Week3' THEN Amount ELSE 0 END),
FROM tbl
GROUP BY region, product

If you can have any number of weeks, you would have to build dynamic
SQL that generated something like the above.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 13 '06 #2
On 12 Jan 2006 23:05:32 -0800, ve**********@gmail.com wrote:
I have SQL records as follows

REGION PRODUCT Amount Week
IMDS A 100 Week1
IMDS A 250 Week2
IMDS A 150 Week3

IMDS B 525 Week1
IMDS B 400 Week2

IMDS C 500 Week2
IMDS C 40 Week3

IHYD A 50 Week1
IHYD A 52 Week2
IHYD A 100 Week3
IHYD D 20 Week2

I want to display these items in ASP as
REGION PRODUCT Week1 Week2 Week3

IMDS A 100 250 150
IMDS B 525 400 0
IMDS C 0 500 40
IHYD A 50 52 100
IHYD D 0 20 0
Is there any "SQL query" which helps me to bring the above output
without using
any front end calculation?


Hi vedavyas.rao,

Actually, doing it client side is much preferred. Presentation should be
handled in the presentation tier, not in the data tier.

However, if you must do it server side, try something like the query
below:

SELECT Region, Product,
SUM(CASE WHEN Week = 'Week1' THEN Amount ELSE 0 END) AS Week1,
SUM(CASE WHEN Week = 'Week2' THEN Amount ELSE 0 END) AS Week2,
SUM(CASE WHEN Week = 'Week3' THEN Amount ELSE 0 END) AS Week3
FROM YourTable
GROUP BY Region, Product

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP
Jan 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
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...
1
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...
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.