473,782 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Empty recordsets and artificial records

I'm currently running the following statement that is used in a Crystal
Report. Basically, a record is returned when the T_PAYMENT.amoun t
has a record in the database based on the value of the T_MULTILIST.cod e
field. Currently, if there is no record returned, there is no listing
in the report for the given T_MULTILIST.cod e.
The user now wants a record to be displayed on the report when there is
no record in the database - she wants it to display a value of '$0'
for the given T_MULTILIST.cod e record. I tried to explain the fact that
is not possible the way things stand at the moment. Basically I need
some type
of case statement that says 'if there is no record returned, create a
single record and set T_PAYMENT.amoun t = 0' AFTER each query has been
executed.
Anyone have any idea how to accomplish this?

SELECT DISTINCT
'English Language Arts, Grade 1' as Rec_Type, 'English Language Arts
(Consumable)' as Super_Type, '' as Other_Type, 'Continuing Contracts'
as Proc_Type,
T_MULTILIST_GRA DE.grade, T_MULTILIST.des cription, T_MULTILIST.cod e,
T_PAYMENT.amoun t
FROM (T_MULTILIST T_MULTILIST INNER JOIN (T_PAYMENT T_PAYMENT
INNER JOIN T_SHIPPING_DETA IL
T_SHIPPING_DETA IL ON
T_PAYMENT.trans action_id=T_SHI PPING_DETAIL.tr ansaction_id)
ON T_MULTILIST.cod e=T_SHIPPING_DE TAIL.multilist_ code) INNER JOIN
T_MULTILIST_GRA DE T_MULTILIST_GRA DE ON
T_MULTILIST.cod e=T_MULTILIST_G RADE.multilist_ code,
T_ORDER, T_REQUISITION, T_REQUISITION_D ETAIL
WHERE
T_ORDER.id = T_SHIPPING_DETA IL.order_id AND
T_REQUISITION.i d = T_ORDER.requisi tion_id AND
T_REQUISITION_D ETAIL.requisiti on_id = T_REQUISITION.i d AND
T_REQUISITION_D ETAIL.latest_re cord_flag = 1 AND
T_REQUISITION.l atest_record_fl ag = 1 AND
T_ORDER.latest_ record_flag = 1
AND (T_MULTILIST.co de='1040')
AND (T_MULTILIST.ex piration_year >= '2006' )
AND (T_REQUISITION. requested_shipm ent_date >= '2006' + '0601'
AND T_REQUISITION.r equested_shipme nt_date < dateadd(YY, 1,
'2006' + '0601' ) )
UNION
SELECT DISTINCT
'English Language Arts, Kindergarten' as Rec_Type, 'English
Language Arts (Consumable)' as Super_Type,
'' as Other_Type, 'Continuing Contracts' as Proc_Type,
T_MULTILIST_GRA DE.grade, T_MULTILIST.des cription,
T_MULTILIST.cod e, T_PAYMENT.amoun t
FROM (T_MULTILIST T_MULTILIST INNER JOIN (T_PAYMENT T_PAYMENT
INNER JOIN T_SHIPPING_DETA IL T_SHIPPING_DETA IL
ON T_PAYMENT.trans action_id=T_SHI PPING_DETAIL.tr ansaction_id)
ON T_MULTILIST.cod e=T_SHIPPING_DE TAIL.multilist_ code) INNER JOIN
T_MULTILIST_GRA DE T_MULTILIST_GRA DE ON
T_MULTILIST.cod e=T_MULTILIST_G RADE.multilist_ code,
T_ORDER, T_REQUISITION, T_REQUISITION_D ETAIL
WHERE
T_ORDER.id = T_SHIPPING_DETA IL.order_id AND
T_REQUISITION.i d = T_ORDER.requisi tion_id AND
T_REQUISITION_D ETAIL.requisiti on_id = T_REQUISITION.i d AND
T_REQUISITION_D ETAIL.latest_re cord_flag = 1 AND
T_REQUISITION.l atest_record_fl ag = 1 AND
T_ORDER.latest_ record_flag = 1
AND (T_MULTILIST.co de='0040')
and (T_MULTILIST.ex piration_year >= '2006' )
AND (T_REQUISITION. requested_shipm ent_date >= '2006' + '0601'
AND T_REQUISITION.r equested_shipme nt_date < dateadd(YY, 1,
'2006' + '0601' ) )

Up to 40 more UNION statements follow the above 2.

Jul 5 '06 #1
3 1394
wg********@yaho o.com (wg********@yah oo.com) writes:
I'm currently running the following statement that is used in a Crystal
Report. Basically, a record is returned when the T_PAYMENT.amoun t
has a record in the database based on the value of the T_MULTILIST.cod e
field. Currently, if there is no record returned, there is no listing
in the report for the given T_MULTILIST.cod e.
The user now wants a record to be displayed on the report when there is
no record in the database - she wants it to display a value of '$0'
for the given T_MULTILIST.cod e record. I tried to explain the fact that
is not possible the way things stand at the moment. Basically I need
some type
of case statement that says 'if there is no record returned, create a
single record and set T_PAYMENT.amoun t = 0' AFTER each query has been
executed.
Sounds like you are looking for an outer join. Now, since I don't know
your tables, or what is being presented, the below may not be the
exact match, but you may be able to get the drift.
SELECT DISTINCT
'English Language Arts, Grade 1' as Rec_Type, 'English Language Arts
(Consumable)' as Super_Type, '' as Other_Type, 'Continuing Contracts'
as Proc_Type,
T_MULTILIST_GRA DE.grade, T_MULTILIST.des cription, T_MULTILIST.cod e,
amount = coalesce(T_PAYM ENT.amount, 0)
FROM T_MULTILIST T_MULTILIST
LEFT JOIN (T_PAYMENT T_PAYMENT
INNER JOIN T_SHIPPING_DETA IL T_SHIPPING_DETA IL
ON T_PAYMENT.trans action_id=T_SHI PPING_DETAIL.tr ansaction_id)
ON T_MULTILIST.cod e=T_SHIPPING_DE TAIL.multilist_ code)
INNER JOIN T_MULTILIST_GRA DE T_MULTILIST_GRA DE ON
T_MULTILIST.cod e=T_MULTILIST_G RADE.multilist_ code,
T_ORDER, T_REQUISITION, T_REQUISITION_D ETAIL
WHERE
T_ORDER.id = T_SHIPPING_DETA IL.order_id AND
T_REQUISITION.i d = T_ORDER.requisi tion_id AND
T_REQUISITION_D ETAIL.requisiti on_id = T_REQUISITION.i d AND
T_REQUISITION_D ETAIL.latest_re cord_flag = 1 AND
T_REQUISITION.l atest_record_fl ag = 1 AND
T_ORDER.latest_ record_flag = 1
AND (T_MULTILIST.co de='1040')
AND (T_MULTILIST.ex piration_year >= '2006' )
AND (T_REQUISITION. requested_shipm ent_date >= '2006' + '0601'
AND T_REQUISITION.r equested_shipme nt_date < dateadd(YY, 1,
'2006' + '0601' ) )

Permit me that the query is quite messy with it's mix of ANSI-join operators
and comma-style cross-join with the join conditions in the WHERE clause.
With outer joins in the mix, you should rewrite all to use ANSI joins.
Up to 40 more UNION statements follow the above 2.
40? Ouch! But why? From the two segments you posted, it appears to me
that all that differs is the condition on T_MULTILIST.cod e.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Jul 5 '06 #2
On 5 Jul 2006 09:41:41 -0700, wg********@yaho o.com wrote:
>I'm currently running the following statement that is used in a Crystal
Report. Basically, a record is returned when the T_PAYMENT.amoun t
has a record in the database based on the value of the T_MULTILIST.cod e
field. Currently, if there is no record returned, there is no listing
in the report for the given T_MULTILIST.cod e.
(snip)

Hi wgblackmon,

The statement you gave is horrible, and I'd be surprised if it shows
anything even resembling performance.

If I may assume that the other 40 UNION'ed SELECT statements all
resemble the first two, you have 42 almost equal SELECT statements with
only one code in the WHERE clause and some constants in the SELECT list
different. Why don't you include the 42 relevant codes and the
accompanying values for Rec_Type, Super_Type, Other_Type, and Proc_Type
in a table? Then, yoou can rewrite the complete monster with one single
query.

There are many other things at fault with your query too:
- Think about readability and maintainability : Why do you supply aliases
that are exactly equal to the table's name? Why do you mix "new style"
infixed joins with "old style" joins (using a comma-seperated list of
tables)? Why do you use nested INNER JOINs? Why don't you stick to one
style for using newlines and indentation to make your queries more
readable?
- Think about performance: Why do you use >'2006' + '0601' << to
denote a fixed date? If you use >'20060601' <<, it can be converted at
compile-time. Now, you're forcing string concatenation and conversion at
execution time. It gets even worse in the complicated datetime formula
that can be replaced with '20070601'. Also, why do you use "UNION"
instead of "UNION ALL", forcing SQL Server to search for duplicates if
the constants in the 42 SELECT lists are distinct anyway? And do you
really need a DISTINCT on the individual queries? It's often a token of
bad design.
>The user now wants a record to be displayed on the report when there is
no record in the database - she wants it to display a value of '$0'
for the given T_MULTILIST.cod e record. I tried to explain the fact that
is not possible the way things stand at the moment.
If you add the extra table suggested above, fulfilling this requirement
is as easy as changing an INNER JOIN to an OUTER JOIN and adding some
COALESCE functions in the SELECT list!

Here's a quick stab (retaining the DISTINCT for now, but do check if you
can leave it out). I have already added the user's wish.

First, set up and fill a table of codes and type descriptions:

CREATE TABLE dbo.CodeList
(Code char(4) NOT NULL PRIMARY KEY,
Rec_Type varchar(40) NOT NULL,
Super_Type varchar(40) NOT NULL,
Other_Type varchar(40) NOT NULL,
Proc_Type varchar(40) NOT NULL)
go
INSERT INTO CodeList (Code, Rec_Type, Super_Type, Other_Type, Proc_Type)
SELECT '1040', 'English Language Arts, Grade 1',
'English Language Arts (Consumable)', '', 'Continuing Contracts'
UNION ALL
SELECT '0040', 'English Language Arts, Kindergarten',
'English Language Arts (Consumable)', '', 'Continuing Contracts'
-- etc for the other codes
go

Now, attempt to rewrite your query.

SELECT DISTINCT c.Rec_Type, c.Super_Type, c.Other_Type, c.Proc_Type,
mg.grade, m.description, m.code, p.amount
FROM T_PAYMENT AS p
INNER JOIN T_SHIPPING_DETA IL AS sd
ON p.transaction_i d = sd.transaction_ id
INNER JOIN T_MULTILIST AS m
ON m.code = sd.multilist_co de
AND m.expiration_ye ar >= '2006'
INNER JOIN T_MULTILIST_GRA DE mg
ON m.code = mg.multilist_co de
INNER JOIN T_ORDER AS o
ON o.id = sd.order_id
AND o.latest_record _flag = 1
INNER JOIN T_REQUISITION AS r
ON r.id = o.requisition_i d
AND r.latest_record _flag = 1
AND r.requested_shi pment_date >= '20060601'
AND r.requested_shi pment_date < '20070601'
INNER JOIN T_REQUISITION_D ETAIL AS rd
ON rd.requisition_ id = r.id
AND rd.latest_recor d_flag = 1
RIGHT OUTER JOIN dbo.CodeList AS c
ON c.Code = m.code

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

--
Hugo Kornelis, SQL Server MVP
Jul 5 '06 #3
On Thu, 06 Jul 2006 00:26:27 +0200, Hugo Kornelis
<hu**@perFact.R EMOVETHIS.info. INVALIDwrote:
>The statement you gave is horrible, and I'd be surprised if it shows
anything even resembling performance.
From the original post, I would wonder if it was generated by Crystal
Reports. Code that bad does often come from such sources.

Roy Harvey
Beacon Falls, CT
Jul 6 '06 #4

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

Similar topics

4
5333
by: Eli Sidwell | last post by:
Trying to return a Recordset to an ASP and the Recordset is empty. The StorredProc works in the query analyzer and it even works from a quick VB app that I wrote to test it. The storedproc that I am using is fairly complex (creates some temporary tables and populates them with 'Insert Into Select ...', but the during testing the only Select statements that return visible rows is the final one that returns the finished table with an...
2
1769
by: Pieter Linden | last post by:
The answer to this one is probably "test it yourself and find out!", but I'll ask anyway. Pursuant to my previous question - sending separate recordsets to Word using the CreateTableFromRecordset code from ADH2000... I will be opening like 8 recordsets {one for each building, and there are 8). Would I be better off or is there anything to be gained by opening one recordset of the query with _all_ the records in it, filtering that, and...
7
8510
by: Jean | last post by:
Hello, I have a form that performs a search, according to criteria that a user enters into the text boxes. When the user clicks on „Search", a SQL string (say strSQL) is built up with the criteria. Then a list box RowSource property is set to this strSQL, to display the results of the search. StrSQL is a global variable within this Form's code module, i.e. in the class module.
34
10845
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have VBA code that wrote the results of that away to the db, either creating new records or updating existing records, whichever was relevant. This may also include deleting records. Now I generally do this by opening a recordset on the source data...
6
1299
by: aa | last post by:
Is there a limitation of the number of recordsets ASP is able to proceed? When my Access database exceeded 544 records, only first 544 records are shown
2
6338
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying tables are indexed. So the user will pull up either one record or none. Here is my problem: I pass the form name through a tmpvariable and ther other search parameters through unbound fields on an unbound form--this works fine unless the form opens...
24
8511
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a workaround for lack of a way to define the ordinal position of a field (incrementing a counter variable), but it feels so primitive: dim Fld as Field dim rst1 as new adodb.recordset
1
3742
by: sphinney | last post by:
As my Access form opens, I want it to find the names of the tables in the current Access database and populate a combobox with the table (recordset) names. Problem is, the CurrentDb.Recordsets object is empty even though I have two tables in the database. I'm using the code below, but the combobox remains empty. That's because CurrentDB.Recordsets is empty. Is this a bug with CurrentDb or have I done something dumb? Dim TBL As...
12
2242
convexcube
by: convexcube | last post by:
Hi Experts, I have a unbound form which records details of complaints. It also records what products are affected in the complaint. These are stored in a table named ComplaintProducts that has the fields: ComplaintProductsIndex (Autonumber), ComplaintIndex & ProductName. The product list is quite lengthy, so I have set up a separate form which opens when the Products Affected list box on the Complaints form is double clicked, containing a...
4
11061
by: MLH | last post by:
160 Dim DB As Database, Rst As Recordset, QD As QueryDef 170 Set DB = CurrentDb() 180 Set QD = DB.CreateQueryDef("", MySQL) 190 Set Rst = QD.OpenRecordset(dbOpenDynaset) HOW TO COUNT RECORDS IN Rst BEFORE NEXT LINE? 200 Rst.MoveFirst
0
9641
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10313
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...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3643
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.