Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Friday, March 30, 2012

Please Help me 'DrillthroughSourceQuery' parameter is missing a value in report builder

Can u help to to solve my problem . Many time this question arised in the forum no one answer to me to find out the solution for my problem. So please help

I am using report builder thru LocalHost\Report . I want a drill through report by using Report Builder and cube . I created the report but unfortunatly I cannot create a drill through report using parameters .How can I pass parameter in report1 to jump into report2 .

When I running the report after giving drill through properties in report the following error will occure.

'The 'DrillthroughSourceQuery' parameter is missing a value

How can I create a report using parameter for drill down in report builder

I am expecting one answer from u expertise

-Create a report with your needed entities and save it in Report builder as RDL file.

-Import it in a Reporting project

-Design your report as needed

-Deploy it on the Report Server

-Use SSMS to connect to the Reporting Server and navigate to the model

-Connect the Report with the single / multi instance of your entity within the model.

Jens K. Suessmeyer.


http://www.sqlserver2005.de

|||

Hi Jens,

Could you please elaborate on the steps.

In my scenario the report rdl file is on some other server and the folder is not shared, how do i impport the rdl file in reporting project. Could you please elaborate on "Use SSMS to connect to the Reporting Server and navigate to the model"

Regards

Neeraj

Please Help me ''DrillthroughSourceQuery'' parameter is missing a value in report builder

Can u help to to solve my problem . Many time this question arised in the forum no one answer to me to find out the solution for my problem. So please help

I am using report builder thru LocalHost\Report . I want a drill through report by using Report Builder and cube . I created the report but unfortunatly I cannot create a drill through report using parameters .How can I pass parameter in report1 to jump into report2 .

When I running the report after giving drill through properties in report the following error will occure.

'The 'DrillthroughSourceQuery' parameter is missing a value

How can I create a report using parameter for drill down in report builder

I am expecting one answer from u expertise

-Create a report with your needed entities and save it in Report builder as RDL file.

-Import it in a Reporting project

-Design your report as needed

-Deploy it on the Report Server

-Use SSMS to connect to the Reporting Server and navigate to the model

-Connect the Report with the single / multi instance of your entity within the model.

Jens K. Suessmeyer.


http://www.sqlserver2005.de

|||

Hi Jens,

Could you please elaborate on the steps.

In my scenario the report rdl file is on some other server and the folder is not shared, how do i impport the rdl file in reporting project. Could you please elaborate on "Use SSMS to connect to the Reporting Server and navigate to the model"

Regards

Neeraj

sql

Please help me debug Hex Dumps

Hello gurus,

Our SQL Servers is giving us a headache, after a certain period in time, either SQL Service automatically shuts down by itself or hangs. I've opened the logs and found hex dumps. Can you help me out with these?

2007-07-08 04:04:35.20 spid53 SqlDumpExceptionHandler: Process 1760 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
* ************************************************** *****************************
*
* BEGIN STACK DUMP:
* 07/08/07 04:04:35 spid 53
*
* Exception Address = 0042D46D
* Exception Code = c0000005 EXCEPTION_ACCESS_VIOLATION
* Access Violation occurred reading address 6AF1EDF0
* Input Buffer 4088 bytes -
* USE Document DBCC DBREINDEX (dtproperties) DBCC DBREINDEX (REfID_DocID
* ) DBCC DBREINDEX (RF_UNISYS_ErrorCodes) DBCC DBREINDEX (SYS_Document_F
* lat_Meta_Data) DBCC DBREINDEX (SYS_Document_Meta_Data) DBCC DBREINDEX
* (SYS_Document_Meta_Detail) DBCC DBREINDEX (SYS_Documents) DBCC DBREIND
* EX (SYS_ETFuelType) DBCC DBREINDEX (SYS_ETStatus) DBCC DBREINDEX (SYS_
* HF_Document_Meta_Data) DBCC DBREINDEX (SYS_HF_MI_Emission_Results) DBC
* C DBREINDEX (SYS_ITP_Failed) DBCC DBREINDEX (SYS_MI_Emission_Results)
* DBCC DBREINDEX (SYS_RF_Document_Meta_Data) DBCC DBREINDEX (SYS_RF_Docum
* ent_Status) DBCC DBREINDEX (SYS_TR_Document) DBCC DBREINDEX (SYS_TR_SM
* S_Document) USE Industry DBCC DBREINDEX (dtproperties) DBCC DBREIND
* EX (IND_MF_Industry) DBCC DBREINDEX (RF_ErrorCodes) DBCC DBREINDEX (RF
* _OperationCodes) DBCC DBREINDEX (RF_Unisys_ErrCodes) DBCC DBREINDEX (S
* YS_Admin_Has_Agents) DBCC DBREINDEX (SYS_Admin_Sharing) DBCC DBREINDEX
* (SYS_Companies) DBCC DBREINDEX (SYS_Company_Has_Admins) DBCC DBREINDE
* X (SYS_Company_Meta_Data) DBCC DBREINDEX (SYS_HF_Admin_Has_Agents) DBC
* C DBREINDEX (SYS_HF_Companies) DBCC DBREINDEX (SYS_HF_Company_Has_Admin
* s) DBCC DBREINDEX (SYS_HF_Company_Meta_Data) DBCC DBREINDEX (SYS_HF_Us
* er_Meta_Data) DBCC DBREINDEX (SYS_HF_Users) DBCC DBREINDEX (SYS_MF_App
* Variables) DBCC DBREINDEX (SYS_MI_Token) DBCC DBREINDEX (SYS_Page_Acce
* ss) DBCC DBREINDEX (SYS_Pages) DBCC DBREINDEX (SYS_Password_History)
* DBCC DBREINDEX (SYS_RF_Announcement) DBCC DBREINDEX (SYS_RF_BodyType)
* DBCC DBREINDEX (SYS_RF_Color) DBCC DBREINDEX (SYS_RF_Company_Meta_Data)
* DBCC DBREINDEX (SYS_RF_Company_Status) DBCC DBREINDEX (SYS_RF_DieselT
* ype) DBCC DBREINDEX (SYS_RF_EmissionFees) DBCC DBREINDEX (SYS_RF_Emiss
* ionRules) DBCC DBREINDEX (SYS_RF_Fuel_Type) DBCC DBREINDEX (SYS_RF_Hel
* pDetails) DBCC DBREINDEX (Sys_RF_Make) DBCC DBREINDEX (SYS_RF_Month)
* DBCC DBREINDEX (SYS_RF_MVClassification) DBCC DBREINDEX (SYS_RF_MVType)
* DBCC DBREINDEX (SYS_RF_MVType2) DBCC DBREINDEX (SYS_RF_Page_Groups)
* DBCC DBREINDEX (SYS_RF_Purpo
*
*
* MODULE BASE END SIZE
* sqlservr 00400000 00CBAFFF 008bb000
* Invalid Address 77F80000 77FFBFFF 0007c000
... <snipped>
* xpstar 09240000 09248FFF 00009000
* rsabase 092D0000 092F2FFF 00023000
* dbghelp 0AA80000 0AB7FFFF 00100000
*
* Edi: 0AA53937: 00000000 00000000 00000000 84004A00 98018901 C501B101
* Esi: 6AF1EDF0:
* Eax: 00000878:
* Ebx: FFFFE000:
* Ecx: 3FFFF800:
* Edx: FFFFE000:
* Eip: 0042D46D: CA8BA5F3 F303E183 DC7D8BA4 83D045FF 4D8B2CC7 E9D233E0
* Ebp: 0A1BFCC0: 0A1BFCE4 0042D5CD 71E428CC 71E40570 71E40988 0AA519A0
* SegCs: 0000001B:
* EFlags: 00010206: 00530053 0052004F 003D0053 00000034 0053004F 0057003D
* Esp: 0A1BFC28: 0AA519A0 71E4052C 00000000 00000010 71E408A0 00000010
* SegSs: 00000023:
* ************************************************** *****************************
* ------------------------
* Short Stack Dump
* 0042D46D Module(sqlservr+0002D46D)
* 0042D5CD Module(sqlservr+0002D5CD)
* 0042D6C7 Module(sqlservr+0002D6C7)
* 00508750 Module(sqlservr+00108750)
* 0051EB18 Module(sqlservr+0011EB18)
* 0051E9E4 Module(sqlservr+0011E9E4)
* 0085EACA Module(sqlservr+0045EACA) (GetIMallocForMsxml+0006A08A)
* 004229A7 Module(sqlservr+000229A7)
* 0087B87B Module(sqlservr+0047B87B) (GetIMallocForMsxml+00086E3B)
* 0087E3C9 Module(sqlservr+0047E3C9) (GetIMallocForMsxml+00089989)
* 0059A449 Module(sqlservr+0019A449)
* 41075309 Module(ums+00005309) (ProcessWorkRequests+000002D9 Line 456+00000000)
* 41074978 Module(ums+00004978) (ThreadStartRoutine+00000098 Line 263+00000007)
* 7C34940F Module(MSVCR71+0000940F) (endthread+000000AA)
* 7C57B3BC Module(KERNEL32+0000B3BC) (lstrcmpiW+000000B7)
* ------------------------
*Dump thread - spid = 53, PSS = 0x55a35280, EC = 0x55a355b0
*Stack Dump being sent to C:\Program Files\Microsoft SQL Server\MSSQL\log\SQLDump0020.txt

*** Problems occur intermittently, sometimes after our full backup which occurs every 0000h, sometimes 30 minutes after our transaction log dumps.

Please advice me on my next step. ThanksProbably need the latest service packs:
http://support.microsoft.com/kb/q293292/|||May also be a plain ol' memory or disk issue. Have you checked the hardware?|||hi blindmin: yes, we've installed the latest service packs both for our OS (windows 2000 server sp4) and rdbms (sql server 2000 sp4)

ReadySetStop: Memory for one, might serve as culprit. our box is running under DELL PowerEdge 1955 (blade server). memory is around 8GB (6gb goes to SQL, 2 GB goes to OS). There was a time when we ported from one blade to another blade server because of faulty OS. Before, when SQL encounters errors, the whole thing just freezes up. Now, SQL has the ability to shoot out hex dumps. That's why I need to have a basis before pointing it directly to a hardware fault.|||these were not the only hex dumps i've encountered. There were some hex dumps that has some variable declaration and value assigning like declare @.asdf datetime etc and lots of hex equivalents on the side. So, it might not only be DBCC stuffs. Just don't know where to start|||If the errors is not related only to DBCC, then I too would look at bad memory as the next possible culprit.|||just an update, we've managed to consult Microsoft for these errors. Initially, they told us that there's an issue having /PAE enabled. I'll keep you guys posted.|||just an update guys, got this from MS tech support.

Hi

I noticed the two SQL Servers have AWE enabled, while the platform is Windows 2000 (Build 2195: Service Pack 4). We have a known issue on such environment, with the similar dump call stacks. Please refer to the following URL.

Access violations when you use the /PAE switch in Windows 2000

http://support.microsoft.com/kb/838647

You may notice unpredictable behavior on a multiprocessor computer that is running SQL Server 2000 and has the Physical Addressing Extensions (PAE) specification enabled

http://support.microsoft.com/kb/838765/en-us

To avoid the issue, could you please upgrade your Windows 2000 to Rollup 1 for Microsoft Windows 2000 Service Pack 4? For more information about the Rollup, please refer to:



http://support.microsoft.com/kb/891861/en-us

And it can be downloaded from:

http://www.microsoft.com/downloads/details.aspx?FamilyId=B54730CF-8850-4531-B52B-BF28B324C662&displaylang=en

After applied it, please keep monitoring your SQL Server. If the issue reoccurs, please send me your SQL Server errorlog files with the new dump file generated.

Friday, March 23, 2012

Please guide - especially about Time Dimension and approach in general

Hi,

I have a table which contains all the transaction details for which I am trying to create a CUBE... The explanation below in brackets is only for clarity about each field. Kindly note that I am using the following table as my fact table. Let's call it tblFact

This table contains fields like transaction date, Product (which is sold), Product Family (to which the Product Belongs), Brand (of the product), Qty sold, Unit Price (at which each unit was sold), and some other fields.

I have created a Product dimension based on tblFact. I don't know if this is a good idea or not :confused: Is it okay and am I on the right track or should I base my Product Dimension on some other table (say tblProducts and then in the Cube editor link tblProducts with tblFact based on the ProductID field? Please guide.

Now coming to my last question:
Currently I am also creating my Time Dimension based on tblFact. Is this also a wrong approach?
1. Should I instead create the Time Dimension based on a different table (say tblTime) and again link up tblTime and tblFact in the Cube editor?

2. if yes, then how do I create tblTime from tblFact in a manner that it only contains all the transaction dates.

3. Assuming that I should take the tblTime approach, then should this table (tblTime) also contain the ProductID - representing the Product which was sold for each date in tblTime?

I realize that this is a lenghty post but reply and more importantly guidance from the experienced users will be greatly appreciated becuase I have recently started learning/playing around on the OLAP side of things and I know that this is the time that I get my foundations correct otherwise I'll end up getting used to some bad practice and will find it difficult to change my approach to cube designing later down the road.

So many thanks in advance and I eagerly look forward to reply from someone.No worries mate,

This is what the forum is for...

Ok - Down to what you need to do

When doing the design for a cube I always do a bit of anyalsis first. Looks like you have crack this bit. You know what your dimensions are - Time , product , brand etc This is what you should group on to build your fact table.

Your facts are going to be Qty Sold , Price. This is what you will be suming on with the SQL to build your fact table.

You said "should I base my Product Dimension on some other table (say tblProducts and then in the Cube editor link tblProducts with tblFact based on the ProductID field? Please guide."

This is exactly what a good cube design is based on mate.

I presume the basis of your fact table is a transactions type table.
First thing you need to do is build all your dimension tables.

A table for product dimension table should look something like :

create table tblProduct
(prod_id tinyint ,
prod_txt varchar (255)
)

Don't forget to put in a id for unknown product - just in case you get these in your base transaction table

Build the rest of your dimension tables like this and assign a tinyint composite key to each dimension. What you what is your fact table to be as small as possible in terms of datatypes.

Now once you have this you want to build your fact table.

What you do is take your fact table and join to each of you dimension tables (should be a left outer join) and sum on the qty and unit price and group up accross all your dimensions.

This now should be you fact table that you can reference in Anyalsis Manager. You will have to define all this with in here as well.

Whoo, that was an effort.

Any problems , questions give me a shout

Cheers|||Hi aldo_2003,

Many thanks for the reply. It answers a good number of my questions. Can you kindly advise regarding the remaining question i.e. the quoted portions below:

Build the rest of your dimension tables like this and assign a tinyint composite key to each dimension. What you what is your fact table to be as small as possible in terms of datatypes.
Question: By composite key do you mean define a Primary key in each table? I will do so but was planning to define the data type for my ProductID field (for example) in my tblProducts as int. However I'll follow your advise and instead use the datatype tinyint. Thanks for the tip :)

And my last question hopefully:
Now coming to my last question:
Currently I am also creating my Time Dimension based on tblFact. Is this also a wrong approach?
1. Should I instead create the Time Dimension based on a different table (say tblTime) and again link up tblTime and tblFact in the Cube editor?

2. if yes, then how do I create tblTime from tblFact in a manner that it only contains all the transaction dates.

3. Assuming that I should take the tblTime approach, then should this table (tblTime) also contain the ProductID - representing the Product which was sold for each date in tblTime?

I think Part 2 (above) is easy and all I have to do is make a copy of tblFact but this copy (say tblTime) will only contain the transaction date column (from tblFact). Kindly confirm my understanding.

However it's the answer to part 1 (above) and especially the part 3 above that is requested.

Looking forward to your reply.|||Your welcome,

forgot about the time question

what you want to do is create a tblTime dimension table

should basically have the grandularity that you want to use

you can find scripts on the net that will help you create and manage a time dimension table.

table should look like

create table tblTime
(date_time smalldatetime,
quarter tinyint,
month tinyint,
week tinyint ,
day int
)

populate this table with all the dates in your date range i.e

Jan 1999 12:00am to Jan 2009 12:00am

This is now your time dimension table.
Using anyalsis manager join back on to the fact table.
Anyalsis manager should guide you through the process of creating a time dimension.

Hope this helps

p.s the only reason I used a tinyint is that I assumed you would have no more than 255 products, if you have more then up the datatype to what you need

Cheers|||Hi again,

Don't mean to "over-flatter" but your replies REALLY have been of great help... Here I was trying to build everything (the fact as well as the dimensions) using only a single table and now I am quite clear regarding what's the right approach :)

3 last questions please :rolleyes:

you can find scripts on the net that will help you create and manage a time dimension table.
table should look like

create table tblTime
(date_time smalldatetime,
quarter tinyint,
month tinyint,
week tinyint ,
day int
)

That's another new tip :) Can u kindly guide where I can get these scripts from? I am assuming that these scripts will not only create the table (in a similar structure as you have suggested above) but will also populate the table with the all the desired date ranges e.g. Jan 1999 12:00am to Jan 2009 12:00am.

2nd last question: So my approach which I was assuming for creating tblTime (for the Time dimension was incorrect) i.e. I thought that this will simply contain the ALL the transaction dates from the transaction table (as I described in my previous post). But from your reply my understanding is that this approach is wrong.

and the last question: So the tblTime does not have to store the ProductID?

Sorry for all the botheration.|||No worries buddy

Glad to be of help

The time dimension table is stand alone and does not have to contain any other info other than time info.

I'll see if my collegue know and get back to you

Cheers|||http://www.databasejournal.com/features/mssql/article.php/10894_1466091_6

http://www.winnetmag.com/Article/ArticleID/23142/Windows_23142.html|||Originally posted by aldo_2003
The time dimension table is stand alone and does not have to contain any other info other than time info.

I'll see if my collegue know and get back to you

Cheers

Thanks aldo : for the reply, the link to articles, and also for the clarification about the time dimension's underlying table containing only time related information.

Kindly do let me know if you get a script which not only creates the table for time but also populates it ...

Bless you!|||Check this link http://www.winnetmag.com/Articles/ArticleID/41531/pg/4/4.html for any help.|||Originally posted by Satya
Check this link http://www.winnetmag.com/Articles/ArticleID/41531/pg/4/4.html for any help.

Hi Satya,

Many sincere thanks for the article. I went through it...

However at this stage I am learning the basics (as evident from this post and my questions to aldo) therefore I found he article to be real "HEVY STUFF" and very difficult to digest at the moment. I talks about the MDX world and that's something that I have yet to explore... I know I will have to get into MDX soon but have enough of the basic to get right first :)

I however would like to request if you can help me with another post from me in this forum (Subject" Need help to create my Time Dimension")... it's somewhat related to portion of the discussion in this post but I did not want to unnecessarily prolong this particular thread/post...

Looking forward to your help in my other post.

Thanks again and regards.|||Joozh
Since this has been answered so well i just wanted to chime in and suggest some reading for you

The Data Warehouse Toolkit by Ralph Kimball (http://www.bestwebbuys.com/books/compare/isbn/0471200247/isrc/b-home-search)

this is the second edition of this book revised in 2002 and it is a must have for every OLAP develper.sql

Wednesday, March 21, 2012

Please Assist... Report Templates to save time in designing

In RS 2000 I was able to create a template report and put it in the Project
Items directory, then when I would select New Report Item, I could choose my
template report instead of redesigning everything from scratch.
Where is this folder, or to better phrase this question:
Where do I put my template reports that I want to show up when I choose "Add
new report item" in the designer in SQL 2005 RS?
I have checked the folders - but obviously I am missing something.
Thanks in advance!
=-ChrisI had to search through my computer for *.rdl files. Finally I think I found
the templates at
C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\PrivateAssemblies\ProjectItems\ReportProject
Kaisa M. Lindahl Lervik
"Chris Conner" <Chris.Conner@.NOSPAMPolarisLibrary.com> wrote in message
news:uOtrsue%23GHA.1220@.TK2MSFTNGP05.phx.gbl...
> In RS 2000 I was able to create a template report and put it in the
> Project
> Items directory, then when I would select New Report Item, I could choose
> my
> template report instead of redesigning everything from scratch.
> Where is this folder, or to better phrase this question:
> Where do I put my template reports that I want to show up when I choose
> "Add
> new report item" in the designer in SQL 2005 RS?
> I have checked the folders - but obviously I am missing something.
> Thanks in advance!
> =-Chris
>

Tuesday, March 20, 2012

Plan not using Index

Hi all, I have been running into this same issue for some time now and can't
come up with a valid reason why SQL is doing what s is doing...
In a nut shell I have three tables
t1 is a fact table
t2 is a time dimension table
t3 is a parameter table that get loaded prior to the query being executed.
Simple code
Select count (t1.stuff)
From t1
Inner join t2
On
t1.date_key = t2.date_key
Inner join t3
on
t2.date_number between t3.start_date and t3.end_date
The optimizer performs a full table scan on the fact then applies the date
range filter... Though if I were to replace the start_date and end_date
values with hardcode values then the optimizer uses the index and pulls only
those records needed from the fact.
Really weird...
Any thoughts?
Thanks
EricStatistics for the tables/index are probably out of date. Update them
using UPDATE STATISTICS command. See if that helps. Or provide an index
hints to the query to specify which index you want it to use. Like:
Select count (t1.stuff)
>From t1
Inner join t2
On
t1.date_key = t2.date_key
Inner join t3
on
t2.date_number between t3.start_date and t3.end_date
WITH (INDEX(index_name))|||Statistics for the tables/index are probably out of date. Update them
using UPDATE STATISTICS command. See if that helps. Or provide an index
hints to the query to specify which index you want it to use. Like:
Select count (t1.stuff)
>From t1
Inner join t2
On
t1.date_key = t2.date_key
Inner join t3
on
t2.date_number between t3.start_date and t3.end_date
WITH (INDEX(index_name))|||Hello Green thank you for your quick reply.
Thats the odd thing... everything looking good. here are the stats: in
addtion supplying the index hint doesn't seem to change the plan result.
DBCC SHOW_STATISTICS
Statistics for FACT INDEX
Updated Rows Rows Sampled Steps
Density Average key length
-- -- -- --
-- --
Mar 6 2006 2:40PM 71680256 304424 197
160.55275 4.0
Statistics for TIME DIMENSION INDEX .
Updated Rows Rows Sampled Steps
Density Average key length
-- -- -- --
-- --
Feb 28 2006 6:46PM 7306 7306 4
1.368738E-4 4.0
DBCC SHOWCONTIG
DBCC SHOWCONTIG scanning Fact_TABLE' table...
Table: 'Pharmacy_Data_Fact_TABLE' (421576540); index ID: 4, database ID: 29
LEAF level scan performed.
- Pages Scanned........................: 155003
- Extents Scanned.......................: 24942
- Extent Switches.......................: 90108
- Avg. Pages per Extent..................: 6.2
- Scan Density [Best Count:Actual Count]......: 21.50% [19376:90109]
- Logical Scan Fragmentation ..............: 13.55%
- Extent Scan Fragmentation ...............: 11.16%
- Avg. Bytes Free per Page................: 1143.1
- Avg. Page Density (full)................: 85.88%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
DBCC SHOWCONTIG scanning 'Time_Dimension' table...
Table: 'Time_Dimension' (597577167); index ID: 27, database ID: 29
LEAF level scan performed.
- Pages Scanned........................: 14
- Extents Scanned.......................: 2
- Extent Switches.......................: 1
- Avg. Pages per Extent..................: 7.0
- Scan Density [Best Count:Actual Count]......: 100.00% [2:2]
- Logical Scan Fragmentation ..............: 0.00%
- Extent Scan Fragmentation ...............: 0.00%
- Avg. Bytes Free per Page................: 268.1
- Avg. Page Density (full)................: 96.69%
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
"Green" wrote:

> Statistics for the tables/index are probably out of date. Update them
> using UPDATE STATISTICS command. See if that helps. Or provide an index
> hints to the query to specify which index you want it to use. Like:
> Select count (t1.stuff)
> Inner join t2
> On
> t1.date_key = t2.date_key
> Inner join t3
> on
> t2.date_number between t3.start_date and t3.end_date
> WITH (INDEX(index_name))
>|||Just a guess, but I don't think the optimizer knows what is in table T3, so
it cannot make a decision based on those dates. As a result, it is unable
to use the index on the dates, since they are unknown. Again, this is just
a guess and could be totally off.
Also, I don't think the statistics on table T3 reflect the current data, but
rather the (completely unrelated) data that was present when the statistics
were generated. I am assuming that statistics on this table are either non
existent, or were generated when the table had no data in it. Try inserting
a row into the table and generating statistics on it, then removing that
row, and running your process again. It is possible that having statistics
with a single valid row in this table will cause the optimizer to choose the
correct path.
Just a couple of ideas.
"Eric" <Eric@.discussions.microsoft.com> wrote in message
news:C7CA8FC0-BD0A-4779-870E-9658E4C72084@.microsoft.com...
> Hi all, I have been running into this same issue for some time now and
can't
> come up with a valid reason why SQL is doing what s is doing...
> In a nut shell I have three tables
> t1 is a fact table
> t2 is a time dimension table
> t3 is a parameter table that get loaded prior to the query being executed.
> Simple code
> Select count (t1.stuff)
> From t1
> Inner join t2
> On
> t1.date_key = t2.date_key
> Inner join t3
> on
> t2.date_number between t3.start_date and t3.end_date
> The optimizer performs a full table scan on the fact then applies the date
> range filter... Though if I were to replace the start_date and end_date
> values with hardcode values then the optimizer uses the index and pulls
only
> those records needed from the fact.
> Really weird...
> Any thoughts?
> Thanks
> Eric
>|||Hi Jim,
I confirmed that the stats on t3 were up to date and truncated the table,
reloaded in it and once again updated the stats. Unfortunately the plan
result was the same.
I assume the optimizer know what is in a table based on the table stats
now taking what you had mentioned into count I think you may be on to
something... but the wrench is why is it if I use a SARG value or create an
additional year column in t3 and make the year column in t2 equal to year i
n
t3 the index get used.
Select count (t1.stuff)
From t1
Inner join t2
On
t1.date_key = t2.date_key
Inner join t3
on
t2.date_number between t3.start_date and t3.end_date and
t2.year_number = t3.year_number
Thanks|||Eric,
It's impossible for the optimizer to do anything but make generic rowcount
estimates for predicates that involve comparing columns from one table
with columns from another. Here, for example, the actual limiting values
of t2.date_number are unknown until run-time, so the histogram of
t2.date_number values is basically useless for plan costing.
By adding an extra superfluous condition to your join's ON clause,
you are reducing the generic rowcount estimate used by the optimizer.
With nothing else to go on, the optimizer will assume that fewer rows
match (<this condition> AND <that condition> ) than match (<this condition> ).
Assuming that fewer rows from t2 match the condition, the estimated cost
of using the index drops, while the cost of a table scan does not drop, and
the change is enough to make the plan that uses an index appear cheapter.
Steve Kass
Drew University
Eric wrote:

>Hi Jim,
>I confirmed that the stats on t3 were up to date and truncated the table,
>reloaded in it and once again updated the stats. Unfortunately the plan
>result was the same.
>I assume the optimizer know what is in a table based on the table stats
>now taking what you had mentioned into count I think you may be on to
>something... but the wrench is why is it if I use a SARG value or create an
>additional year column in t3 and make the year column in t2 equal to year
in
>t3 the index get used.
>Select count (t1.stuff)
>From t1
>Inner join t2
>On
>t1.date_key = t2.date_key
>Inner join t3
>on
>t2.date_number between t3.start_date and t3.end_date and
>t2.year_number = t3.year_number
>Thanks
>
>|||I would expect that equality is handled very different then between for
optimizational purposes. This makes perfect sense since a single value is
more exlicit and easier to determine ahead of time than a range of values.
However, you did not state whether or not this year_number column is indexed
or not. If there is an index on this field , then I would expect it to make
a difference simply because it is easier for the optimizer to know that it
is targeting a certain percentage of rows with the join. If there is no
index, then Steve Kass's explanation seems the only way to explain it.
Regardless, I find that the behavior of the optimizer never ceases to amaze
me.
"Eric" <Eric@.discussions.microsoft.com> wrote in message
news:2DC2E151-6AB0-472B-B385-3B9918F34F73@.microsoft.com...
> Hi Jim,
> I confirmed that the stats on t3 were up to date and truncated the table,
> reloaded in it and once again updated the stats. Unfortunately the plan
> result was the same.
> I assume the optimizer know what is in a table based on the table stats
> now taking what you had mentioned into count I think you may be on to
> something... but the wrench is why is it if I use a SARG value or create
an
> additional year column in t3 and make the year column in t2 equal to year
in
> t3 the index get used.
> Select count (t1.stuff)
> From t1
> Inner join t2
> On
> t1.date_key = t2.date_key
> Inner join t3
> on
> t2.date_number between t3.start_date and t3.end_date and
> t2.year_number = t3.year_number
> Thanks
>|||Try running the SQL through the SQL Optimizer for Visual Studio. It will
rewrite your statement in every possible fashion to get you the best
performing SQL. It has a free 30 day trial. You can find it at
http://www.extensibles.com/modules...=Products&op=NS
examnotes <Eric@.discussions.microsoft.com> wrote in
news:C7CA8FC0-BD0A-4779-870E-9658E4C72084@.microsoft.com:

>
The Relentless One
Debugging is a state of mind
http://www.extensibles.com/

Friday, March 9, 2012

pl/sql

I am doing pl/sql first time. I dont know that when i have script to run , i wrote that in simple note pad and now i am not able to run that procedure. please help me that where to write script and how to run it sucessfully.Originally posted by sam70
I am doing pl/sql first time. I dont know that when i have script to run , i wrote that in simple note pad and now i am not able to run that procedure. please help me that where to write script and how to run it sucessfully.

Either write it in SQL*PLUS, or open SQL*PLUS and do @.filename to run it.

Pk to be initiated every time?

i want primary key of table to be inserted automatically. i've set its in desgin table as Identity = Yes; Seed=1

i want my application all other attributes except primary key which i've set atutomatically inserted.

but by doing this; it doesn't insert its value. instead it enters 0

can u plz help me in doing so?

Hi,

You are probably not doing things in the right way. It's a good idea to try ceating the table using a create table script and see if the problem persists or not:

CREATE TABLE TableName( PrimaryKeyColumnNameINT IDENTITY PRIMARY KEYNOT NULL, Column1 DataType, ...)

Happy SQLing!

Mehrdad

Wednesday, March 7, 2012

PK

Hi,
only one question please, maybe it is a faq, but could be a boolean question
for
people that haven't time...
I have My_user table, here there's a field 'nick_name' not Null and unique,
is it good for primary Key or is better a field int. identity.
Thanks"VincEnzo" <TOGLIv-franco@.libero.it> wrote in message
news:KSLoc.51573$Qc.2092127@.twister1.libero.it...
> Hi,
> only one question please, maybe it is a faq, but could be a boolean
question
> for
> people that haven't time...
> I have My_user table, here there's a field 'nick_name' not Null and
unique,
> is it good for primary Key or is better a field int. identity.
> Thanks

If nickname must always be present and is a common way of identifying a user
then it seems on the information so far to be a good candidate for a primary
key.

However, as for the second question - (I assume you mean would an integer
identity be a better candidate for a primary key), and not (would nickname
be better stored as an intereger intentity) then this depends a bit on the
other tables in your database.

How many people do you have in your User-table ... If it is very high ( >
10,000 ) then you might find it very difficult to continue to find unique
nicknames ...

It also depends on the number of other related tables you have that will
refer back to this table.

If the number of related-records is very high ( > 100,000 ) , then you may
find that table-size is important and than you want narrow tables.

On the other hand, if you have a small database then you may fnid that
readability is more important than efficiency or table storage.

Choosing a primary key can depend on a lot more things than I've mentioned
(and the numbers I've quoted are just fictitious examples ) ...

The best answer to "what is the best Primary Key" is often "it depends"|||thanks Steven Wilmot
this answeresatisfy me.
I thought that numbers was more faster than string
but the true was more coplexity
ciao
Vincenzo
"Steven Wilmot" <steven-news@.wilmot.me.uk> ha scritto nel messaggio
news:40a3a8ce$0$58821$5a6aecb4@.news.aaisp.net.uk.. .
> "VincEnzo" <TOGLIv-franco@.libero.it> wrote in message
> news:KSLoc.51573$Qc.2092127@.twister1.libero.it...
> > Hi,
> > only one question please, maybe it is a faq, but could be a boolean
> question
> > for
> > people that haven't time...
> > I have My_user table, here there's a field 'nick_name' not Null and
> unique,
> > is it good for primary Key or is better a field int. identity.
> > Thanks
> If nickname must always be present and is a common way of identifying a
user
> then it seems on the information so far to be a good candidate for a
primary
> key.
> However, as for the second question - (I assume you mean would an integer
> identity be a better candidate for a primary key), and not (would nickname
> be better stored as an intereger intentity) then this depends a bit on the
> other tables in your database.
> How many people do you have in your User-table ... If it is very high ( >
> 10,000 ) then you might find it very difficult to continue to find unique
> nicknames ...
> It also depends on the number of other related tables you have that will
> refer back to this table.
> If the number of related-records is very high ( > 100,000 ) , then you may
> find that table-size is important and than you want narrow tables.
> On the other hand, if you have a small database then you may fnid that
> readability is more important than efficiency or table storage.
> Choosing a primary key can depend on a lot more things than I've mentioned
> (and the numbers I've quoted are just fictitious examples ) ...
> The best answer to "what is the best Primary Key" is often "it depends"|||>> I have My_user table, here there's a field [sic] "nickname
<datatype> NOT NULL UNIQUE" Is it good for PRIMARY KEY or is better a
field [sic] INTEGER IDENTITY. <<

Rows are not records; fields are not columns; tables are not files.
When you look for a key, ask yourself:

1) Is it unique?
2) Is it verifiable in the reality modeled by the database?
3) Can it validate itself in the front end? (Check digits? regular
expression?)
4) Is it familiar to the users?
5) Is it a superkey?

Clearly the IDENTITY property is not a consideration at all. It is a
proprietary non-relational, physical locator for the internal
representation of the data in storage and has absolutely nothing that
can be validated or verified.

But let's ask the same questions about "nickname" and see some
problems. I am assuming that "nickname" is a character string with a
nickname in the usual sense in it, sicne you did not bother with DDL
or specs.

1) Do you ever have two people with the same nickname? In the real
world, yes. Every redheaded guy gets the nickname "Red" or "Carrot
top", so you will have to assign these things and force them to be
unique in the database.

2) You can ask everyone what their nickname is, and hope they have
one. And that they give you the one in the database, not the one in
the real world.

3) No.

4) Not the one in the database.

5) No.

Therefore, I would suggest that you look for a better key. Perhaps a
government assigned identifier or something like that.|||thx
"--CELKO--"
ok my example, nickname, not was happy, but
if we have a table with a few rows and has two field
idgategory and nameCategory.
the value of nameCategory is not Null and unique.
Can i get nameCategory like PK and throw idgategory that it is identity...
Now I understand YES
right Steven? right Celko?
grazie
ciao

<jcelko212@.earthlink.net> ha scritto nel messaggio
news:18c7b3c2.0405131115.3e974df9@.posting.google.c om...
> >> I have My_user table, here there's a field [sic] "nickname
> <datatype> NOT NULL UNIQUE" Is it good for PRIMARY KEY or is better a
> field [sic] INTEGER IDENTITY. <<
> Rows are not records; fields are not columns; tables are not files.
> When you look for a key, ask yourself:
> 1) Is it unique?
> 2) Is it verifiable in the reality modeled by the database?
> 3) Can it validate itself in the front end? (Check digits? regular
> expression?)
> 4) Is it familiar to the users?
> 5) Is it a superkey?
> Clearly the IDENTITY property is not a consideration at all. It is a
> proprietary non-relational, physical locator for the internal
> representation of the data in storage and has absolutely nothing that
> can be validated or verified.
> But let's ask the same questions about "nickname" and see some
> problems. I am assuming that "nickname" is a character string with a
> nickname in the usual sense in it, sicne you did not bother with DDL
> or specs.
> 1) Do you ever have two people with the same nickname? In the real
> world, yes. Every redheaded guy gets the nickname "Red" or "Carrot
> top", so you will have to assign these things and force them to be
> unique in the database.
> 2) You can ask everyone what their nickname is, and hope they have
> one. And that they give you the one in the database, not the one in
> the real world.
> 3) No.
> 4) Not the one in the database.
> 5) No.
> Therefore, I would suggest that you look for a better key. Perhaps a
> government assigned identifier or something like that.|||>> ... two fields [sic]category_id IDENTITY and category_name NOT NULL
UNIQUE. <<

Rows are not records; fields are not columns; tables are not files;
there is no sequential access or ordering in an RDBMS, so "first",
"next" and "last" are totally meaningless.

If you want to build a look-up table with a code called "Categories",
then you need to design this code. Is it a vector code (ISO tire
sizes)? A hierarchical code (Dewey Decimal)? Concatenation code?
What?

The problem is that you want a single, simple magic answer. There is no
magic answer. Designing a database is hard work and takes years to
learn to do correctly.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||ok, thanks
i go to work
see you in two years
ciao
"Joe Celko" <jcelko212@.earthlink.net> ha scritto nel messaggio
news:40a3fd38$0$201$75868355@.news.frii.net...
> >> ... two fields [sic]category_id IDENTITY and category_name NOT NULL
> UNIQUE. <<
> Rows are not records; fields are not columns; tables are not files;
> there is no sequential access or ordering in an RDBMS, so "first",
> "next" and "last" are totally meaningless.
> If you want to build a look-up table with a code called "Categories",
> then you need to design this code. Is it a vector code (ISO tire
> sizes)? A hierarchical code (Dewey Decimal)? Concatenation code?
> What?
> The problem is that you want a single, simple magic answer. There is no
> magic answer. Designing a database is hard work and takes years to
> learn to do correctly.
> --CELKO--
> ===========================
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in your
> schema are.
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!|||"Steven Wilmot" <steven-news@.wilmot.me.uk> wrote in message news:<40a3a8ce$0$58821$5a6aecb4@.news.aaisp.net.uk>...
> "VincEnzo" <TOGLIv-franco@.libero.it> wrote in message
> news:KSLoc.51573$Qc.2092127@.twister1.libero.it...
> > Hi,
> > only one question please, maybe it is a faq, but could be a boolean
> question
> > for
> > people that haven't time...
> > I have My_user table, here there's a field 'nick_name' not Null and
> unique,
> > is it good for primary Key or is better a field int. identity.
> > Thanks
> If nickname must always be present and is a common way of identifying a user
> then it seems on the information so far to be a good candidate for a primary
> key.
> However, as for the second question - (I assume you mean would an integer
> identity be a better candidate for a primary key), and not (would nickname
> be better stored as an intereger intentity) then this depends a bit on the
> other tables in your database.
> How many people do you have in your User-table ... If it is very high ( >
> 10,000 ) then you might find it very difficult to continue to find unique
> nicknames ...
> It also depends on the number of other related tables you have that will
> refer back to this table.
> If the number of related-records is very high ( > 100,000 ) , then you may
> find that table-size is important and than you want narrow tables.
> On the other hand, if you have a small database then you may fnid that
> readability is more important than efficiency or table storage.
> Choosing a primary key can depend on a lot more things than I've mentioned
> (and the numbers I've quoted are just fictitious examples ) ...
> The best answer to "what is the best Primary Key" is often "it depends"

The only condition to define a primary key is that it has to be unique.|||>> The only condition to define a primary key is that it has to be
unique. <<

Necessary, but not sufficient. An invalid and/or unverifiable key is
worse than useless; it is dangerous.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||>Necessary, but not sufficient. An invalid and/or unverifiable key is
>worse than useless; it is dangerous.
>--CELKO--

I agree but would you care to elaborate a little bit on this ?

Randy
http://members.aol.com/rsmeiner|||>> I agree but would you care to elaborate a little bit on this [An
invalid and/or unverifiable key is worse than useless; it is dangerous]?
<<

Scenario 1: The part number is a simple sequential number. ANY integer
could be a part number and it has no syntax or check digit to tell me.
I cheerfully send the orphans rat poison instead of penicillin when the
input clerk makes a simple typo on the order form.

Scenario 2: There is a natural key in the data, but it is not declared
unique in the schema. Instead, the DB designer used a proprietary
locator (rowid, auto-increment, identity, GUID, etc.). The real key
changes, but the locator does not. Data integrity is destroyed. Then
the Sarbanes-Oxley auditors and the 60 MINUTES crew show up on the same
day.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||>Scenario 1: The part number is a simple sequential number. ANY integer
>could be a part number and it has no syntax or check digit to tell me.
>I cheerfully send the orphans rat poison instead of penicillin when the
>input clerk makes a simple typo on the order form.
>Scenario 2: There is a natural key in the data, but it is not declared
>unique in the schema. Instead, the DB designer used a proprietary
>locator (rowid, auto-increment, identity, GUID, etc.). The real key
>changes, but the locator does not. Data integrity is destroyed. Then
>the Sarbanes-Oxley auditors and the 60 MINUTES crew show up on the same
>day.
>--CELKO--

I have fought the Scenario 2 battle more then a few times.
Seems that the basic M$ classes are geared towards this.
They actually teach students to use Identity fields as the primary
key even if there is a valid and unique column that can be used.
I can always tell when someone has been to that 1st course or 2.

Even had 1 little snot tell me that my method was "old school".
Just because I used a valid column as the primary key.

Randy
http://members.aol.com/rsmeiner|||Joe Celko (jcelko212@.earthlink.net) writes:
> Scenario 1: The part number is a simple sequential number. ANY integer
> could be a part number and it has no syntax or check digit to tell me.
> I cheerfully send the orphans rat poison instead of penicillin when the
> input clerk makes a simple typo on the order form.

Only if your application is designed to display the primary key for
the user. Which in many cases it shouldn't

> Scenario 2: There is a natural key in the data, but it is not declared
> unique in the schema. Instead, the DB designer used a proprietary
> locator (rowid, auto-increment, identity, GUID, etc.). The real key
> changes, but the locator does not. Data integrity is destroyed. Then
> the Sarbanes-Oxley auditors and the 60 MINUTES crew show up on the same
> day.

The good thing is that the natural key changed, but the database was
left bascially untouched. It was a simple update.

The problem with natural keys is that they rarely live up to the
strong requirements for a primary key in a database. Typical cases
are that some items does for some reason not have the expected value
(so you have to invent one), or there are unexpected duplicates.

These keys are often good enough for data entry and searching, but the
application have to handle the situation that there are two persons
with the same SSN, or corresponding registration number.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

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

Monday, February 20, 2012

pivot information

Hi there,
I haven't used queries to put data in columns in quite some time, so I'm
slightly rusty... I would appreciate some help with the following please:
(SQL 2000)
Query:
select [Date], MachNo,
case when Shift = 'A' then sum(Quantity) else 0 end as ShiftAQty,
case when Shift = 'B' then sum(Quantity) else 0 end as ShiftBQty,
case when Shift = 'C' then sum(Quantity) else 0 end as ShiftCQty
from Prod_Knitting_Data_1
group by [Date], MachNo, Shift
Result (extract):
Date Mc A B C
2008-01-07 00:00:00.000 01 0 0 400
2008-01-07 00:00:00.000 01 0 200 0
2008-01-07 00:00:00.000 02 0 0 0
2008-01-07 00:00:00.000 02 0 120 0
2008-01-07 00:00:00.000 03 0 0 180
2008-01-07 00:00:00.000 03 0 180 0
2008-01-07 00:00:00.000 03 60 0 0
As can be seen I need the results to be in one row for each date, machine
I know I'm missing something... but cannot recall what...
Thank you in advance!
Change the query to take the SUM out of the CASE function:
SELECT [Date], MachNo,
SUM(CASE WHEN Shift = 'A' THEN Quantity ELSE 0 END) AS
ShiftAQty,
SUM(CASE WHEN Shift = 'B' THEN Quantity ELSE 0 END) AS
ShiftBQty,
SUM(CASE WHEN Shift = 'C' THEN Quantity ELSE 0 END) AS ShiftCQty
FROM Prod_Knitting_Data_1
GROUP BY [Date], MachNo
HTH,
Plamen Ratchev
E-Mail: Plamen@.Ratchev.com
|||select [Date], MachNo,
sum(case when Shift = 'A' then Quantity else 0 end) as
ShiftAQty,
sum(case when Shift = 'B' then Quantity else 0 end) as
ShiftBQty,
sum(case when Shift = 'C' then Quantity else 0 end) as
ShiftCQty,
from Prod_Knitting_Data_1
group by [Date], MachNo
"PsyberFox" <PsyberFox@.discussions.microsoft.com> wrote in message
news:21D07D5F-6113-403F-8E6D-FBB4F433DEFB@.microsoft.com...
> Hi there,
> I haven't used queries to put data in columns in quite some time, so I'm
> slightly rusty... I would appreciate some help with the following please:
> (SQL 2000)
> Query:
> select [Date], MachNo,
> case when Shift = 'A' then sum(Quantity) else 0 end as
> ShiftAQty,
> case when Shift = 'B' then sum(Quantity) else 0 end as
> ShiftBQty,
> case when Shift = 'C' then sum(Quantity) else 0 end as
> ShiftCQty
> from Prod_Knitting_Data_1
> group by [Date], MachNo, Shift
>
> Result (extract):
> Date Mc A B C
> 2008-01-07 00:00:00.000 01 0 0 400
> 2008-01-07 00:00:00.000 01 0 200 0
> 2008-01-07 00:00:00.000 02 0 0 0
> 2008-01-07 00:00:00.000 02 0 120 0
> 2008-01-07 00:00:00.000 03 0 0 180
> 2008-01-07 00:00:00.000 03 0 180 0
> 2008-01-07 00:00:00.000 03 60 0 0
>
> As can be seen I need the results to be in one row for each date, machine
> I know I'm missing something... but cannot recall what...
> Thank you in advance!

pivot information

Hi there,
I haven't used queries to put data in columns in quite some time, so I'm
slightly rusty... I would appreciate some help with the following please:
(SQL 2000)
Query:
select [Date], MachNo,
case when Shift = 'A' then sum(Quantity) else 0 end as ShiftAQty,
case when Shift = 'B' then sum(Quantity) else 0 end as ShiftBQty,
case when Shift = 'C' then sum(Quantity) else 0 end as ShiftCQty
from Prod_Knitting_Data_1
group by [Date], MachNo, Shift
Result (extract):
Date Mc A B C
2008-01-07 00:00:00.000 01 0 0 400
2008-01-07 00:00:00.000 01 0 200 0
2008-01-07 00:00:00.000 02 0 0 0
2008-01-07 00:00:00.000 02 0 120 0
2008-01-07 00:00:00.000 03 0 0 180
2008-01-07 00:00:00.000 03 0 180 0
2008-01-07 00:00:00.000 03 60 0 0
As can be seen I need the results to be in one row for each date, machine
I know I'm missing something... but cannot recall what...
Thank you in advance!Change the query to take the SUM out of the CASE function:
SELECT [Date], MachNo,
SUM(CASE WHEN Shift = 'A' THEN Quantity ELSE 0 END) AS
ShiftAQty,
SUM(CASE WHEN Shift = 'B' THEN Quantity ELSE 0 END) AS
ShiftBQty,
SUM(CASE WHEN Shift = 'C' THEN Quantity ELSE 0 END) AS ShiftCQty
FROM Prod_Knitting_Data_1
GROUP BY [Date], MachNo
HTH,
Plamen Ratchev
E-Mail: Plamen@.Ratchev.com|||select [Date], MachNo,
sum(case when Shift = 'A' then Quantity else 0 end) as
ShiftAQty,
sum(case when Shift = 'B' then Quantity else 0 end) as
ShiftBQty,
sum(case when Shift = 'C' then Quantity else 0 end) as
ShiftCQty,
from Prod_Knitting_Data_1
group by [Date], MachNo
"PsyberFox" <PsyberFox@.discussions.microsoft.com> wrote in message
news:21D07D5F-6113-403F-8E6D-FBB4F433DEFB@.microsoft.com...
> Hi there,
> I haven't used queries to put data in columns in quite some time, so I'm
> slightly rusty... I would appreciate some help with the following please:
> (SQL 2000)
> Query:
> select [Date], MachNo,
> case when Shift = 'A' then sum(Quantity) else 0 end as
> ShiftAQty,
> case when Shift = 'B' then sum(Quantity) else 0 end as
> ShiftBQty,
> case when Shift = 'C' then sum(Quantity) else 0 end as
> ShiftCQty
> from Prod_Knitting_Data_1
> group by [Date], MachNo, Shift
>
> Result (extract):
> Date Mc A B C
> 2008-01-07 00:00:00.000 01 0 0 400
> 2008-01-07 00:00:00.000 01 0 200 0
> 2008-01-07 00:00:00.000 02 0 0 0
> 2008-01-07 00:00:00.000 02 0 120 0
> 2008-01-07 00:00:00.000 03 0 0 180
> 2008-01-07 00:00:00.000 03 0 180 0
> 2008-01-07 00:00:00.000 03 60 0 0
>
> As can be seen I need the results to be in one row for each date, machine
> I know I'm missing something... but cannot recall what...
> Thank you in advance!

Pivot Data

I have data in this format

Product qty
ABC 4
DEF 5
:
:
XYZ 4

I'll never have more that 10 products at a time.
I need to deliver the data in a format like this:

prod_1 qty1 prod_2 qty2 ... prod_10 qty10
ABC 4 DEF 5 XYZ 4

If there are less than 10 products, the last columns will contain NULL.
What I'm doing is delivering a recordset to a report.
The report is designed to work with a single record, not multiple lines.
Of course, there's more data than this, this is just where I'm stuck.

Is there a way to pivot this data? I'm using SQL2000

Here's a ddl of what I've been trying to work with,
but all attempts at pivoting have been = FAIL

CREATE TABLE #TMP (
PKG_CODE VARCHAR(20),
QTY_ACT INTEGER)

INSERT INTO #TMP
SELECT 'PAD', 15
UNION ALL
SELECT 'PAD4436', 15
UNION ALL
SELECT 'SW', 15
UNION ALL
SELECT 'TS', 16

SELECT * FROM #TMP

DROP TABLE #TMPHow do you decide what product goes into which column?|||Hi george,

Ordering doesn't matter, the first piece of data goes in the first column.

I'm thinking I'll have to use a cursor here. It doesn't matter... there won't be any speed issues, I'm just wondering if it's possible with SQL.

Thanks
Mark|||Nah, I'm pretty sure that it can be done.CREATE TABLE #foo (
product VARCHAR(10)
PRIMARY KEY (product)
, qty INT
)

DECLARE @.j0 VARCHAR(10)
, @.j1 VARCHAR(10)

INSERT INTO #foo (
product, qty
) SELECT 'A01', 11
UNION ALL SELECT 'B02', 22
UNION ALL SELECT 'C03', 33
UNION ALL SELECT 'D04', 44
UNION ALL SELECT 'E05', 55
UNION ALL SELECT 'F06', 66
UNION ALL SELECT 'G07', 77
UNION ALL SELECT 'H08', 88
UNION ALL SELECT 'I09', 99
UNION ALL SELECT 'J10', 10

SELECT
z00.product, z00.qty, z01.product, z01.qty
, z02.product, z02.qty, z03.product, z03.qty
, z04.product, z04.qty, z05.product, z05.qty
, z06.product, z06.qty, z07.product, z07.qty
, z08.product, z08.qty, z09.product, z09.qty
FROM #foo AS z00, #foo AS z01, #foo AS z02, #foo AS z03, #foo AS z04
, #foo AS z05, #foo AS z06, #foo AS z07, #foo AS z08, #foo AS z09
WHERE z00.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 0)
AND z01.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 1)
AND z02.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 2)
AND z03.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 3)
AND z04.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 4)
AND z05.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 5)
AND z06.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 6)
AND z07.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 7)
AND z08.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 8)
AND z09.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 9)

DROP TABLE #foo-PatP|||sooo close...

That works great, except when I have less than 10 records - which I normally will. I get nothing if I comment out the last UNION statement.|||i expect if you used left outer joins instead of (implied) inner, it might work

:)|||Works perfectly:)

INSERT INTO #foo (
product, qty
) SELECT 'A01', 11
UNION ALL SELECT 'B02', 22
-- UNION ALL SELECT 'C03', 33
-- UNION ALL SELECT 'D04', 44
-- UNION ALL SELECT 'E05', 55
-- UNION ALL SELECT 'F06', 66
-- UNION ALL SELECT 'G07', 77
-- UNION ALL SELECT 'H08', 88
-- UNION ALL SELECT 'I09', 99
-- UNION ALL SELECT 'J10', 10
select z00.product, z00.qty,
z01.product, z01.qty,
z02.product, z02.qty,
z03.product, z03.qty
from #foo as z00 left join #foo as z01 on z00.product<z01.product
left join #foo as z02 on z01.product<z02.product
left join #foo as z03 on z02.product<z03.product
WHERE z00.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 0)
AND z01.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 1)
AND (z02.product is null or z02.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 2))
AND (z03.product is null or z03.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 3))

Thanks to both of you!|||actually, what i had in mind was this -- FROM #foo AS z00
left outer
join #foo AS z01
on z01.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 1)
left outer
join #foo AS z02
on z02.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 2)
left outer
join #foo AS z03
on z03.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 3)
left outer
join #foo AS z04
on z04.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 4)
left outer
join #foo AS z05
on z05.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 5)
left outer
join #foo AS z06
on z06.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 6)
left outer
join #foo AS z07
on z07.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 7)
left outer
join #foo AS z08
on z08.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 8)
left outer
join #foo AS z09
on z09.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 9)
WHERE z00.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 0)|||My bad! I don't often need to "think Oracle" anymore, but this is a case where SQL-89 is an elegant solution (if you do it right). A solution that would better match my original intent would be:CREATE TABLE #foo (
product VARCHAR(10)
PRIMARY KEY (product)
, qty INT
)

DECLARE @.j0 VARCHAR(10)
, @.j1 VARCHAR(10)

INSERT INTO #foo (
product, qty
) SELECT 'A01', 11
UNION ALL SELECT 'B02', 22
UNION ALL SELECT 'C03', 33
UNION ALL SELECT 'D04', 44
UNION ALL SELECT 'E05', 55
UNION ALL SELECT 'F06', 66
UNION ALL SELECT 'G07', 77
UNION ALL SELECT 'H08', 88
UNION ALL SELECT 'I09', 99
-- UNION ALL SELECT 'J10', 10

SELECT
z00.product, z00.qty, z01.product, z01.qty
, z02.product, z02.qty, z03.product, z03.qty
, z04.product, z04.qty, z05.product, z05.qty
, z06.product, z06.qty, z07.product, z07.qty
, z08.product, z08.qty, z09.product, z09.qty
FROM #foo AS z00, #foo AS z01, #foo AS z02, #foo AS z03, #foo AS z04
, #foo AS z05, #foo AS z06, #foo AS z07, #foo AS z08, #foo AS z09
WHERE z00.product = (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 0)
AND z01.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 1)
AND z02.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 2)
AND z03.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 3)
AND z04.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 4)
AND z05.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 5)
AND z06.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 6)
AND z07.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 7)
AND z08.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 8)
AND z09.product =* (SELECT product FROM #foo AS a WHERE (SELECT Count(*) FROM #foo AS b WHERE b.product < a.product) = 9)

DROP TABLE #foo-PatP|||"better match my original intent?"

isn't mine the same as your latest, only you're not using sql-89 and i am?

and what does oracle have to do with any of this?

forgive me for asking, but i guess i didn't understand your remarks at all|||I'm 99 5/8 percent sure that my example was using SQL-89 (doing pseudo-joins within the WHERE clause) and that yours was using SQL-92 (doing explicit joins within the FROM clause). The SQL-92 approach is usually more comfortable for me, but in this case it seems more verbose and the older syntax seems clearer to me.

Oracle uses a very row-oriented approach to data. This is why it depends on cursors for so much, and why its query optimizer works better for pseudo-joins expressed as criteria in the WHERE clause instead of set oriented joins expressed within the FROM clause. This is why Oracle is sometimes easier to code for instances like this because the criteria don't get applied until after the candidate result set has materialized, so you can use any valid SQL expression to specify what rows you want returned instead of the more efficient but more limited choices available before the candidate set is materialized.

The purpose behind pusing the join criteria out of the WHERE clause into the FROM clause was to make it possible for more advanced optimizers to process the join operations and specifications in whatever order they chose. This was to allow more advanced ways of processing JOIN operations that could drastically improve the efficency of the operation (and thereby improve the efficiency of the databse engine).

-PatP|||i might have missed something somewhere along the way, i could be wrong, i don't want to say you're mistaken, but that reprehensible "equals asterisk" syntax was never part of any sql standard

which is why your earlier statement confused me as to whose query you were talking about

"equals asterisk" is, i believe, a sybase/microsoft implementation

which, admittedly, is fair game for this particular forum

but it isn't sql-89|||I don't have a copy of the ANSI SQL-89 standard handy, but I don't think that it had a specification for outer joins at all. Only the vendor specific extensions allowed outer joins at that time.

Oracle had its =(+) syntax, DB2 had its =| syntax, and all of the other SQL vendors (Sybase, Gupta, Borland, etc) that I knew of supported the =* syntax. None of them that I knew of supported JOIN operations in the FROM clause, all of them either completely or partially materialized the candidate result set, then started to apply the WHERE clause against that result set.

-PatP|||well, as you yourself so eloquently said, "this is a case where SQL-89 is an elegant solution (if you do it right)"

the trick, of course, would be doing it right

which in my opinion would be to use sql-92 syntax

:)