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

SQL statement for a report...

Hello, SQL gurus!

This message use HTML tables (in case you see a bunch of gobbledy-gook).

I need to modify an SQL statement:

SELECT DISTINCT Trim(InvtID)+', '+Trim(Descr) AS Item, month, Sum(QtyShip) AS Qty
FROM Sales_View
GROUP BY Trim(InvtID)+', '+Trim(Descr), month, CustID
HAVING (CustID)= @CustID )
ORDER BY Trim(InvtID)+', '+Trim(Descr), month;

Whose output is something like the following:

Item
month
Qty

26479F, Report Cover Sheets (100/pkg)
3
2

26479F, Report Cover Sheets (100/pkg)
8
1

26479F, Report Cover Sheets (100/pkg)
9
1

46217C, Manual- 5th Ed
6
1

46217C, Manual- 5th Ed
8
1

514560, Profile Report
8
1

51900C, Technical Manual
1
1

ADHAC0, Adults and Children (kit)
8
6

ADS04, Technical Manual 2nd Ed
7
15

And turn it into something like this:

Item
JanQty
FebQty
MarQty
AprQty
MayQty
JunQty
JulQty
AugQty
SepQty
OctQty
NovQty
DecQty

26479F, Report Cover Sheets (100/pkg)
0
0
2
0
0
0
0
1
1
0
0
0

46217C, Manual- 5th Ed
0
0
0
0
0
1
0
1
0
0
0
0

514560, Profile Report
0
0
0
0
0
0
0
1
0
0
0
0

51900C, Technical Manual
1
0
0
0
0
0
0
0
0
0
0
0

ADHAC0, Adults and Children (kit)
0
0
0
0
0
0
0
6
0
0
0
0

ADS04, Technical Manual 2nd Ed
0
0
0
0
0
0
15
0
0
0
0
0

Can anyone help?!

TIA...
Jul 20 '05 #1
4 1933
The Eeediot (ee*****@hotmail.com) writes:
Hello, SQL gurus!

This message use HTML tables (in case you see a bunch of gobbledy-gook).

I need to modify an SQL statement:

SELECT DISTINCT Trim(InvtID)+', '+Trim(Descr) AS Item, month, Sum(QtyShip)
AS Qty
FROM Sales_View
GROUP BY Trim(InvtID)+', '+Trim(Descr), month, CustID
HAVING (CustID)= @CustID )
ORDER BY Trim(InvtID)+', '+Trim(Descr), month;

Whose output is something like the following:

Item
month
Qty
...
And turn it into something like this:

Item
JanQty
FebQty
MarQty
AprQty
MayQty
JunQty
JulQty
AugQty
SepQty
OctQty
NovQty
DecQty


JanQty = SUM(CASE month WHEN 1 THEN QtyShip ELSE 0 END),
FebQty = SUM(CASE month WHEN 2 THEN QtyShip ELSE 0 END),
....
DecQty = SUM(CASE month WHEN 12 THEN QtyShip ELSE 0 END),

I'm not really sure why have the condition on CustID in a HAVING clause.
Why isn't in the WHERE clause?

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Thanks for the tip!

The HAVING claused is used in conjuction with GROUP BY. It is used to
organize column data with aggregate functions...so says the manual!

I used MS Access to create the SQL statement.

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn**********************@127.0.0.1...
The Eeediot (ee*****@hotmail.com) writes:
Hello, SQL gurus!

This message use HTML tables (in case you see a bunch of gobbledy-gook).

I need to modify an SQL statement:

SELECT DISTINCT Trim(InvtID)+', '+Trim(Descr) AS Item, month, Sum(QtyShip) AS Qty
FROM Sales_View
GROUP BY Trim(InvtID)+', '+Trim(Descr), month, CustID
HAVING (CustID)= @CustID )
ORDER BY Trim(InvtID)+', '+Trim(Descr), month;

Whose output is something like the following:

Item
month
Qty
...
And turn it into something like this:

Item
JanQty
FebQty
MarQty
AprQty
MayQty
JunQty
JulQty
AugQty
SepQty
OctQty
NovQty
DecQty


JanQty = SUM(CASE month WHEN 1 THEN QtyShip ELSE 0 END),
FebQty = SUM(CASE month WHEN 2 THEN QtyShip ELSE 0 END),
...
DecQty = SUM(CASE month WHEN 12 THEN QtyShip ELSE 0 END),

I'm not really sure why have the condition on CustID in a HAVING clause.
Why isn't in the WHERE clause?

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #3
Okay, I feel stupid.

I found a way to get a similar result by using (you guessed it) a Crosstab Query! And this was right in MS Access, too.

The resulting SQL statement being...

TRANSFORM Sum(Sales_View.QtyShip) AS [The Value]
SELECT Trim([InvtID])+', '+Trim([Descr]) AS Item
FROM Sales_View
WHERE (((Sales_View.CustID)='100070') AND ((Sales_View.Year)=2003))
GROUP BY Trim([InvtID])+', '+Trim([Descr])
PIVOT Sales_View.month;

"The Eeediot" <ee*****@hotmail.com> wrote in message news:es********************@giganews.com...
Hello, SQL gurus!

This message use HTML tables (in case you see a bunch of gobbledy-gook).

I need to modify an SQL statement:

SELECT DISTINCT Trim(InvtID)+', '+Trim(Descr) AS Item, month, Sum(QtyShip) AS Qty
FROM Sales_View
GROUP BY Trim(InvtID)+', '+Trim(Descr), month, CustID
HAVING (CustID)= @CustID )
ORDER BY Trim(InvtID)+', '+Trim(Descr), month;

Whose output is something like the following:

Item
month
Qty

26479F, Report Cover Sheets (100/pkg)
3
2

26479F, Report Cover Sheets (100/pkg)
8
1

26479F, Report Cover Sheets (100/pkg)
9
1

46217C, Manual- 5th Ed
6
1

46217C, Manual- 5th Ed
8
1

514560, Profile Report
8
1

51900C, Technical Manual
1
1

ADHAC0, Adults and Children (kit)
8
6

ADS04, Technical Manual 2nd Ed
7
15

And turn it into something like this:

Item
JanQty
FebQty
MarQty
AprQty
MayQty
JunQty
JulQty
AugQty
SepQty
OctQty
NovQty
DecQty

26479F, Report Cover Sheets (100/pkg)
0
0
2
0
0
0
0
1
1
0
0
0

46217C, Manual- 5th Ed
0
0
0
0
0
1
0
1
0
0
0
0

514560, Profile Report
0
0
0
0
0
0
0
1
0
0
0
0

51900C, Technical Manual
1
0
0
0
0
0
0
0
0
0
0
0

ADHAC0, Adults and Children (kit)
0
0
0
0
0
0
0
6
0
0
0
0

ADS04, Technical Manual 2nd Ed
0
0
0
0
0
0
15
0
0
0
0
0

Can anyone help?!

TIA...
Jul 20 '05 #4
Careful. This may be a performance nightmare. Sounds like you are using MS Access with linked tables. On small databases this may be acceptable but as the data grows, the functions that access performs on the backend usually become detrimental to performance. A cross tab in access is probably going to do one of two things. Either submit a cross join on the backend or pull multiple result sets down and do the joining on the front end. Both are very inefficient.

As you can tell I'm not a big fan of using MS Access against SQL Server unless you use SQL pass through and write custom SQL.

Danny
"The Eeediot" <ee*****@hotmail.com> wrote in message news:pI********************@giganews.com...
Okay, I feel stupid.

I found a way to get a similar result by using (you guessed it) a Crosstab Query! And this was right in MS Access, too.

The resulting SQL statement being...

TRANSFORM Sum(Sales_View.QtyShip) AS [The Value]
SELECT Trim([InvtID])+', '+Trim([Descr]) AS Item
FROM Sales_View
WHERE (((Sales_View.CustID)='100070') AND ((Sales_View.Year)=2003))
GROUP BY Trim([InvtID])+', '+Trim([Descr])
PIVOT Sales_View.month;

"The Eeediot" <ee*****@hotmail.com> wrote in message news:es********************@giganews.com...
Hello, SQL gurus!

This message use HTML tables (in case you see a bunch of gobbledy-gook).

I need to modify an SQL statement:

SELECT DISTINCT Trim(InvtID)+', '+Trim(Descr) AS Item, month, Sum(QtyShip) AS Qty
FROM Sales_View
GROUP BY Trim(InvtID)+', '+Trim(Descr), month, CustID
HAVING (CustID)= @CustID )
ORDER BY Trim(InvtID)+', '+Trim(Descr), month;

Whose output is something like the following:

Item
month
Qty

26479F, Report Cover Sheets (100/pkg)
3
2

26479F, Report Cover Sheets (100/pkg)
8
1

26479F, Report Cover Sheets (100/pkg)
9
1

46217C, Manual- 5th Ed
6
1

46217C, Manual- 5th Ed
8
1

514560, Profile Report
8
1

51900C, Technical Manual
1
1

ADHAC0, Adults and Children (kit)
8
6

ADS04, Technical Manual 2nd Ed
7
15

And turn it into something like this:

Item
JanQty
FebQty
MarQty
AprQty
MayQty
JunQty
JulQty
AugQty
SepQty
OctQty
NovQty
DecQty

26479F, Report Cover Sheets (100/pkg)
0
0
2
0
0
0
0
1
1
0
0
0

46217C, Manual- 5th Ed
0
0
0
0
0
1
0
1
0
0
0
0

514560, Profile Report
0
0
0
0
0
0
0
1
0
0
0
0

51900C, Technical Manual
1
0
0
0
0
0
0
0
0
0
0
0

ADHAC0, Adults and Children (kit)
0
0
0
0
0
0
0
6
0
0
0
0

ADS04, Technical Manual 2nd Ed
0
0
0
0
0
0
15
0
0
0
0
0

Can anyone help?!

TIA...
Jul 20 '05 #5

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

Similar topics

7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
2
by: Cathleen C via DotNetMonster.com | last post by:
I'm a semi-beginner with c# and am having a problem effectively implementing a switch statement. I've created an asp.net app that runs a report depending on which item was selected from a drop...
4
by: dkelly925 | last post by:
I have an Access Report that I am trying to enter an "If Statement" that when a certain field exceeds a certain number of lines or a certain number of characters that field is not visible and...
5
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
12
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select...
1
by: Ron | last post by:
Hi All, I've got a requirement to print an existing statement printing report to another printer that's set up to print on a wide-carriage, bottom-fed dot-matrix printer. That printer's sole...
1
by: anthonyjm | last post by:
I need to run a report for each record returned from a SQL Select statement. The report changes for each record, so they need to be run separately. So something like this (I have no idea what the...
9
by: ontherun | last post by:
Hi all, I am using MS Access 2002. i am building a database for a Finance Company I have 2 tables - tblCustomer (personal Details and PK is LoanNumber) tblLoan (PK is...
0
by: angi35 | last post by:
In Access 2000, I'm trying to create a couple reports but can't figure out the queries. 1. In a form, I have a control with a dollar amount, with the source field . Then there's an option group...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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: 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...

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.