473,806 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need one Query to obtain results I can only get with two queries

I'm trying to devise a query for use on SQL Server 2000 that will do
what was previously done with one query in MS Access. The MS Access
query was like this:

SELECT Count(*) as [Opened],
Abs(Sum([Status] Like 'Cancel*')) As [Cancelled]
FROM Detail_Dir_LocV
Where (Detail_Dir_Loc V.DateOpened > '2004-8-01') and
Status not like 'Deleted'
Group By Year(DateOpened ), Month(DateOpene d)
Order By Year(DateOpened ), Month(DateOpene d)

Here were I'm at with SQL Server, TSQL

Select Right(Convert(C har (11), Min(DateOpened) , 106), 8) as [Month
Opened],
Count(Status) as [Opened]
FROM Detail_Dir_LocV
Where (Detail_Dir_Loc V.DateOpened > '2004-8-01') and
Status not like 'Deleted'
Group By Year(DateOpened ), Month(DateOpene d) Order By
Year(DateOpened ), Month(DateOpene d)

Which yields

Month Opened
=============== =======
Aug 2004 503
Sep 2004 752
Oct 2004 828
Nov 2004 658
Dec 2004 533
Jan 2005 736
Feb 2005 707
Mar 2005 797
Apr 2005 412

And

Select Right(Convert(C har (11), Min(DateOpened) , 106), 8) as [Month
Opened],
Count(Status) as [Cancelled]
FROM Detail_Dir_LocV
Where (Detail_Dir_Loc V.DateOpened > '2004-8-01') and
Status like 'Cancelled%'
Group By Year(DateOpened ), Month(DateOpene d) Order By
Year(DateOpened ), Month(DateOpene d)

Which yields;

Month Cancelled
=============== ==========
Aug 2004 78
Sep 2004 105
Oct 2004 121
Nov 2004 106
Dec 2004 75
Jan 2005 82
Feb 2005 71
Mar 2005 94
Apr 2005 33

What is desired is

Month Opened Cancelled
=============== =============
Aug 2004 503 78
Sep 2004 752 105
Oct 2004 828 121
Nov 2004 658 106
Dec 2004 533 75
Jan 2005 736 82
Feb 2005 707 71
Mar 2005 797 94
Apr 2005 412 33

Any assistance would be appreciated.
Cheers;

Bill
Jul 23 '05 #1
3 1269
I think that you need something like this:

Select Right(Convert(C har (11), Min(DateOpened) , 106), 8) as [Month
Opened],
Count(Status) as [Opened],
Sum(CASE WHEN [Status] Like 'Cancel%' THEN 1 ELSE 0 END) as
[Cancelled]
FROM Detail_Dir_LocV
Where (Detail_Dir_Loc V.DateOpened > '2004-8-01') and Status not like
'Deleted'
Group By Year(DateOpened ), Month(DateOpene d)
Order By Year(DateOpened ), Month(DateOpene d)

Razvan

PS. It would have been useful if you would have provided the DDL (in
form of "CREATE TABLE..." statements) and some sample data (in form of
"INSERT INTO ... VALUES ..." statements).

Jul 23 '05 #2
"Razvan Socol" <rs****@gmail.c om> wrote in message news:<11******* *************** @o13g2000cwo.go oglegroups.com> ...
I think that you need something like this:

Select Right(Convert(C har (11), Min(DateOpened) , 106), 8) as [Month
Opened],
Count(Status) as [Opened],
Sum(CASE WHEN [Status] Like 'Cancel%' THEN 1 ELSE 0 END) as
[Cancelled]
FROM Detail_Dir_LocV
Where (Detail_Dir_Loc V.DateOpened > '2004-8-01') and Status not like
'Deleted'
Group By Year(DateOpened ), Month(DateOpene d)
Order By Year(DateOpened ), Month(DateOpene d)

Razvan

PS. It would have been useful if you would have provided the DDL (in
form of "CREATE TABLE..." statements) and some sample data (in form of
"INSERT INTO ... VALUES ..." statements).


Razvan;

Thank you for your assistance. It solved my problem.

Is the purpose of your request for a Create Table and Insert samples
so that you might build the table and populate it locally to test your
proposed solution? I think I know how to get the Create statement
since in fact the query is accessing a view and I can just grab it's
properties. The main table that the view is built upon was built
interactively. The table is also populated via an ASP html form so
building a set of insert into would be a manual process. Is there
anyway to have SQL Server build a sample for your intended purpose
based upon the existing data in the table or view?

Any way, you solved my problem and I appreciate your time.

Thank you.

Cheers;

Bill
Jul 23 '05 #3
Hi Bill,

Yes, that is the purpose: to enable us to test the solution easily, but
also to make us sure that we properly understood the scenario, to make
all of those who respond use the same column names.

For generating insert scripts from the existing data, see:
http://vyaskn.tripod.com/code.htm#inserts
http://www.databasejournal.com/scrip...le.php/1493101

For in-depth considerations about DDL and sample data, see:
http://www.aspfaq.com/etiquette.asp?id=5006

Razvan

Jul 23 '05 #4

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

Similar topics

2
3058
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
2
1661
by: Alexandre MELARD | last post by:
Hi, My name is alexandre, I am 4th year student at the Napier university of edinburgh. I am finishing my year and do a presentation of my honours project next wednesday (the 5th of May). I am doing a project of data mining, and I run a mysql database to store my data. the database structure:
5
1925
by: Alicia | last post by:
Hello everyone based on the data, I created a union query which produces this. SELECT ,,, 0 As ClosedCount FROM UNION SELECT ,, 0 AS OpenedCount, FROM ORDER BY , ;
4
1705
by: Luisa Lopes | last post by:
Dear colleagues: I read somewhere that you could obtain the results of a query as a form. I have design a database for my books and waht I would like is to obtain the results of my queries (which use parameters for language, author, genre, etc) in the form of a bunch of the original forms I used for creating the book entries. I don't know if I explained the problem correctly. As a result of a query I
1
1453
by: Raposa Velha | last post by:
Olá Luisa ;-) If I understood correctly, you would like to display the results of a query in a form. No problem, just go to the "Create form by using wizard" and at the first step just choose the query you want. Xau! RV -------------------------
2
1636
by: rockyptc | last post by:
greetings. first, i apologize for asking an old question. it appears that i'm looking for a solution that was already given but it don't seem to fit my scenario. so thanks for putting up with me. here's what i have. a table called "RESULTS" - this table has 4 columns i need to work with. CustID, Matrix, Test, Parameter. i have three select queries that perform a count on how many times a
6
17164
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how this can be done? I've tried setting the default value of the text fields on the form to be equal to ! using Access' expression
0
2460
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself). First off, english is a second language to me and sometimes my sentences might be a little awkward. Please forgive me. Mon...
2
1609
by: bips2005 | last post by:
i have got two queries, $sql1 = "select cust_id from customer where comp_name = '$compname'"; $result = $DB->query($sql1); $result->fetchInto($row); $sql = "select expirydate from customer l,domain s where l.cust_id = '$row' and s.domname = '$domname'"; the first query retrieves cust_id from the table customer and i use it in the second query to obtain expirydate.
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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 we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.