Showing posts with label cross-tab. Show all posts
Showing posts with label cross-tab. Show all posts

Saturday, February 25, 2012

Cross-Tab vs SQL query

Hi,
I have a report like this...

Date Sam Tom Harry 2006 2005
02/01/2006 4 1 1 6 2
02/02/2006 3 1 1 4 5
02/03/2006 2 2 0 4 4
---
---

02/12/2006 1 1 1 3 1
-----------
Total -- -- -- -- --

Now I have all the data required for that in one table.

CREATE TABLE t1(
indate DATETIME
,agname VARCHAR(60)
,Polnumber VARCHAR(20)
)


So I have 2 options :
I have to do in in Cross tab or I have to create a query for this.
Is it possible to built a query like this,I mean assigning data as column?

Any help will be greatly appreciated.
Thanks!!Is it possible to built a query like this,I mean assigning data as column?Yep. If there are a fixed number of possible column values you can use CASE. If not then you need to use dynamic SQL (e.g. http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21820764.html#16500817,
http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608,
http://www.sqlteam.com/item.asp?ItemID=2955). I've not used any of the above - I just have them bookmarked in case I need them.

BTW - your better option might be the front end doing this if at all possible

HTH|||Thanks a lot pottie for those links.I will give it a try tonight...Thanks once again..:cool:

Cross-tab Row Title

CR XI posted a work-around/solution for inserting a row title in a cross tab report by creating a separate "fake" group, overlaying it and copying the cross tab to the fake group's section. It also discussed horizontal pages.

How do you repeat the label on subsequent/multiple pages though?See if you find answer here

http://support.businessobjects.com/

Friday, February 24, 2012

cross-tab reporting?

Hello:
Is it possible to create cross-tab reports in SQL Reporting Services,
similar to the cross-tab reports that can be created in Crystal Reports?
Thanks!
childofthe1980sAre you new to SSRS, it can be done very much, In SSRS its called as matrix
Reports.
Amarnath
"childofthe1980s" wrote:
> Hello:
> Is it possible to create cross-tab reports in SQL Reporting Services,
> similar to the cross-tab reports that can be created in Crystal Reports?
> Thanks!
> childofthe1980s

Cross-tab query in SQL Server

I have a table with results from several inspection tests. The Pass/Fail parameter in the cross-tab output would be Pass if all tests for that serial number are Pass, and Fail if any of the results for that serial number are Fail.

How would I create a cross-tab query in SQL Server?

Table for input to cross-tab query: [Inspection Data]

SerialNumber ParamName Result Pass/Fail

001 Test1 3.43 Pass

001 Test2 3.27 Pass

001 Test3 2.97 Pass

002 Test1 2.88 Pass

002 Test2 3.01 Pass

002 Test3 4.22 Fail

003 Test1 3.11 Pass

003 Test2 2.91 Pass

003 Test3 3.28 Pass

Cross-tab Query Output:

Test1 Test2 Test3 Pass/Fail

001 3.43 3.27 2.97 Pass

002 2.88 3.01 4.22 Fail

003 3.11 2.91 3.28 Pass

Thanks,

Sam

SELECT b.sno, b.test1, b.test2, b.test3, c.passfail FROM (select a.sNo
, min(case a.pName when 'test1' then result end) as [test1]
, min(case a.pName when 'test2' then result end) as [test2]
, min(case a.pName when 'test3' then result end) as [test3]
from dbo.pivotTable$ as a
group by a.sNo) b INNER JOIN (SELECT sNo, min(pOrf) AS PassFail
FROM pivotTable$ GROUP BY sNo) c ON c.sNo=b.sNo

For SQL Server 2005, you can:
SELECT p.sno, p.test1, p.test2, p.test3, b.PassFail
FROM (SELECT sno, pName, result
from pivotTable ) t
pivot (MAX(t.result) for t.pName in (test1,test2,test3)) AS p
INNER JOIN (SELECT sNo, min(pOrf) AS PassFail
FROM pivotTable$ GROUP BY sNo) b ON p.sNo=b.sNo|||

I am having trouble getting the syntax correct when I try to implement the real query. It tells me "Incorrect syntax near the keyword 'ON'

Do you have any idea what it might be?

select b.[Work Order No], b.[Serial No], b.[Apex], b.[Height]], b.[Radius], b.[Visual], b.[IL], b.[BR], c.[PF]

from ((select a.[Work Order No], a.[Serial No]

, min(case a.[Parameter Name] when 'Apex Offset' then [Value] end) as [Apex]

, min(case a.[Parameter Name] when 'Fiber Height ( Spherical Fit )' then [Value] end) as [Height]

, min(case a.[Parameter Name] when 'Radius of Curvature' then [Value] end) as [Radius]

, min(case a.[Parameter Name] when 'Visual Inspection' then [Value] end) as [Visual]

, min(case a.[Parameter Name] when 'Insertion Loss' then [Value] end) as [IL]

, min(case a.[Parameter Name] when 'Back Reflection' then [Value] end) as [BR]

from dbo.[Inspection Data] as a

group by a.[Work Order No],[Serial No] ) b inner join ((select [Work Order No], [Serial No], min([Pass/Fail]) as PF

from dbo.[Inspection Data] group by [Work Order No],[Serial No] ) c on c.[Work Order No]=b.[Work Order No] and c.[Serial No] = b.[Serial No]

Thank you,

Sam

|||

"...

group by a.[Work Order No],[Serial No] ) b inner join ((select [Work Order No], [Serial No], min([Pass/Fail]) as PF

from dbo.[Inspection Data] group by [Work Order No],[Serial No] ) c on c.[Work Order No]=b.[Work Order No] and c.[Serial No] = b.[Serial No]"

Is this an extra parathenses (in red)?

Cross-tab query definition problem

Hi there,
I have got this problem. I want to know which concrete combination of 7
variables gives the maximum value of some non-linear function. I have
created 7 tables (per 1 column) with all possible values of each
variable (e.g. for the first variable: 0.00,0.01,0.02,0.03,...,1.00)
and then made the cross-tab query based on these 7 tables: Select
Max(...) from dbo.var1 cross join dbo.var2 ... cross join dbo.var7.
This query finds the maximum value of the function but how should I
find out which concrete combination of the variables is connected with
this max.value?
I know that it is not clever to use relational DBS for this problem but
I want to find out how quickly SQL server calculates the result.
Thanks a lot.
MilanHello, Milan
You are talking about cross joins, not crosstabs.
You can get the values using something like this:
SELECT Col1, Col2, ...
FROM Table1, Table2, ...
WHERE YourExpression=(
SELECT MAX(YourExpression)
FROM Table1, Table2, ...
)
If you have 7 tables with 100 values each, that would be a total of
100000000000000 combinations (that is one hundred thousand billions). I
would not dare to think how many years it would take for SQL Server to
return the result of this query.
Razvan|||Hello Razvan,
Thanks for your answer. I am sorry for that mistake, of course I ment
cross joins :).
The problem with your solution is that expression for every
combinations have to be calculated two-times. Since the total number of
combinations is great (the number of rows in each table is: 100, 20,
10, 10, 10, 5, 20 resp., which gives 200 mil. combinations) there is a
serious problem with calculation. Is it somehow possible to obtain the
concrete combination of variables which yield the maximum value of that
function within one single query'
Thank you.
Milan|||Another way would be:
SELECT TOP 1 WITH TIES Col1, Col2, ...
FROM Table1, Table2, ...
ORDER BY YourExpression DESC
Razvan

cross-tab query

Hi all.

we have a department that has an access database with a bunch of queries in it. They want us to convert it to sql server. One of the queries is a cross-tab query. Is there an easy way to create this in sql? the column headings are the value of column from a table. This could change each month that they run it. How do I make the column heading a variable? I'm guessing a stored procedure would be best. Does anyone have any suggestions?

Thanks so much.
ODanielsWhat version of SQL Server?

If 2000, then no, there is no "easy" way. This is one of the more irritating omissions from SQL 2k.

If you have 2k5, then you're good to go.|||it's not too hard in sql 2k. there is even a section in BOL if you need a how to.|||The section in BOL requires you to know all of the "column" values in advance...|||It's sql2k.
That's what I saw also - need to know all the column values in advance.
I can build the table with a bunch of statements, I was just hopeing there was an easier way.|||I have a crosstab sproc tucked away somewhere. I'll dig it up in a bit here.

... and there she is:

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO



CREATE PROCEDURE crosstab @.XField varChar(50), @.XTable varChar(50),
@.XWhereString varChar(250), @.XFunction varChar(10), @.XFunctionField varChar(20), @.XRow varchar(40)
AS
Declare @.SqlStr nvarchar(4000)
Declare @.tempsql nvarchar(4000)
Declare @.SqlStrCur nvarchar(4000)
Declare @.col nvarchar(100)

set @.SqlStrCur = N'Select [' + @.XField + '] into ##temptbl_Cursor from [' + @.XTable + '] ' + @.XWhereString + ' Group By [' + @.XField + ']'

exec sp_executesql @.sqlstrcur

declare xcursor Cursor for Select * from ##temptbl_Cursor
open xcursor

Fetch next from xcursor
into @.Col

While @.@.Fetch_Status = 0
Begin
set @.Sqlstr = @.Sqlstr + ", "
set @.tempsql = isnull(@.sqlstr,'') + isnull(@.XFunction + '( Case When ' + @.XField + " = '" +@.Col +
"' then [" + @.XFunctionField + "] Else 0 End) As [" + @.Col + "]" ,'')
set @.Sqlstr = @.tempsql
Fetch next from xcursor into @.Col

End

set @.tempsql = 'Select ' + @.XRow + ', ' + @.Sqlstr + ' From ' + @.XTable +
@.XWhereString + ' Group by ' + @.XRow + ' Order By ' + @.XRow
set @.Sqlstr = @.tempsql
PRINT @.tempsql
Close xcursor
Deallocate xcursor

set @.tempsql = N'Drop Table ##temptbl_Cursor'
exec sp_executesql @.tempsql

exec sp_executesql @.Sqlstr


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

I should add that I shameless stole this from some random website, though I can't remember where it was.|||You are awsome Teddy.
I will give that a try. Thank you.

Cross-Tab Query

I need to produce a query in cross-tab format that displays the column data as the first day of the week for every week for a given date range.

i.e. for August
4 Aug 03 | 11 Aug 03 | 18 Aug 03 | 25 Aug 03

Row1
Row2
etc...

Any pointer in the right direction would be appreciated.
Thanks
TimThe basic idea is to make a query, which returns the value to be accumulated with the corresponding week indication, like:

SELECT YourGroupingField,
case datediff(wk, '2003-01-01', getdate()) < 30 THEN YourSumField ELSE 0 END AS Aug0,
case datediff(wk, '2003-01-01', getdate()) = 30 THEN YourSumField ELSE 0 END AS Aug1,
case datediff(wk, '2003-01-01', getdate()) = 31 THEN YourSumField ELSE 0 END AS Aug2,
case datediff(wk, '2003-01-01', getdate()) > 31 THEN YourSumField ELSE 0 END AS Aug3
FROM YourTable

You may use this query as a subquery or as a database view:

SELECT YourGroupingField, sum(Aug0), sum(Aug1), ...
FROM (YourQuery)
GROUP BY YourGroupingField

Let me know if this helps, or when you don't understand my outline.|||Thank you for your quick reply.
I think I did not explain myself that well. So here goes - the column dates should auto-generate based on a date range. This date range could potentially be several months or years. So if the date range was 1 March 03 - 30 June 03 this would produde 18 columns starting with 3 Mar 03.

I know how to write a normal cross-tab but it's the auto-column generation that's tricky for me.|||You want the heavy stuff? Look at this (www.sqlmag.com/Articles/Index.cfm?ArticleID=15608).

Crosstab Labels

Hi,

I have designed a Cross-Tab Report that goes more than 5 pages.
I have put Title and Row headings on to the Report but it is displayed only on the first page and does not repeat on to the Continuation pages.

Herewith i have attached the sample report on which the title and the row labels are displayed only on the first page and doesn't display on the second page.

How to acheive this.

Hlp Pls...

Ashok.I got the same problem as urs. please send me the solution if u get it. please reply even if there is no solution. my id: vijay@.synergies.com.sg

Thanks,

Vijay

crosstab and time values

Hello! I'm new to Reporting Services and cross-tab reporting in
general. I
need to create a report that has days (dynamic cols) and a wait time
col (static) and across the top and categories down the left.
Visually I need to see:
Day 1 Day 2 Total
Wait Time Col2 | Wait Time Col2 | Wait Time Col2
______________________________________________________
Cat1 00:05:00 10 | 00:10:00 5 | 00:15:00
15
Cat2 00:07:00 13 | 00:13:00 8 | 00:20:00
21
______________________________________________________
Total 00:13:00 23 | 00:23:00 13
Is it possible to apply an aggregate function to a time value? If yes,
how?
Can someone point me in the right direction?
Thanks in advance.I forgot to say: i=B4m using reporting services 2000 SP2. I really nedd
help!|||I forgot to say - I=B4m using reporting services 2000 SP2. I really need
help!|||You need to use a matrix to achieve this, instead of a table.
Then have categories as row groups and days as column groups.
Add subtotals to both groups to have the totals.
Add a static column to the data field to have your Col2s.
Matrixes can be a bit confusing at first, but this is definately the control
you need to use.
Kaisa M. Lindahl Lervik
"kaustika" <rosa.cunha@.gmail.com> wrote in message
news:1158867347.399372.184490@.i42g2000cwa.googlegroups.com...
> Hello! I'm new to Reporting Services and cross-tab reporting in
> general. I
> need to create a report that has days (dynamic cols) and a wait time
> col (static) and across the top and categories down the left.
> Visually I need to see:
> Day 1 Day 2 Total
> Wait Time Col2 | Wait Time Col2 | Wait Time Col2
> ______________________________________________________
> Cat1 00:05:00 10 | 00:10:00 5 | 00:15:00
> 15
> Cat2 00:07:00 13 | 00:13:00 8 | 00:20:00
> 21
> ______________________________________________________
> Total 00:13:00 23 | 00:23:00 13
> Is it possible to apply an aggregate function to a time value? If yes,
> how?
> Can someone point me in the right direction?
> Thanks in advance.
>|||I think I didn=B4t explain myself very well.
My doubts are: IN A CROSSTAB static column: (i) is it possible to put
time values? (ii) The sql query must return what? A double, an integer
or a datetime? (iii) And how to apply an aggregate function (SUM, AVG,
MAX, MIN) to a time value?
Thanks in advance.

Cross-Tab - How to add logic in summarized fields

Hi, I'm using Crystal Report Developper XI with Eclipse and I'd like to know if it's possible to put some logic in a Cross-table

Here's my problem:
I made a simple cross-table to track orders delivery

Example:
---| 20/06/07 21/06/07 22/06/07
-------------------
Order 1 | 44 55 66
Order 2 | 66 55 33
Order 3 | DELI 22 66

The problem is that when I display this cross-tab, I must checked whether or not this order has been delivered (it's a simple boolean flag), if yes, I want to display DELI, otherwise I display the quantity

I could I do that given that the quantities displayed are the result of a SUM({ORDER.QUANTITY}) ??

Thanks in advance !
Philgo to cross tab expert, you should see new formula near bottom right corner, create your formula and add to your cross tab.|||It seems like I don't have access to that cross-tab expert, i'm using Crystal Report XI Developper (plugin for eclipse) and i'm not using the Crystal report server.

Is there another of doing it... without using cross-tab expert ?!|||The problem is that when I display this cross-tab, I must checked whether or not this order has been delivered (it's a simple boolean flag), if yes, I want to display DELI, otherwise I display the quantity

I could I do that given that the quantities displayed are the result of a SUM({ORDER.QUANTITY}) ??
Phil

ig you have access to your query into DB, I suggest you create a view and you could write there the logic.

bit litle sample

create or replace view nameview as
select................
case when myflag=1 then
ORDER.QUANTITY
else
0 'in your report takes 0 when run (Sum)
end as ForCrossTabData
..
.....

Thursday, February 16, 2012

Cross Tab Query

How can I cross-tab the query it was simple in MS-Access but i don't know how
to do it in SQL. the thing i want is as follows:
Simple Query
Item Year Qty
A 1991 10
B 1991 15
C 1991 20
Cross-Tab Query (Items)
ITEMS
Year A B C
1991 10 15 20
Daud Akbar
select year ,
A = (select QTY from tbl t2 where t.year = t2.year and item = 'A') ,
B = (select QTY from tbl t2 where t.year = t2.year and item = 'B') ,
C = (select QTY from tbl t2 where t.year = t2.year and item = 'C')
from tbl t
group by year
order by year
|||Than u very much dear u were the only who replied my and understand the
issue. But unfortunately u have give the example for static entry I need
dynamic one in the form that whatever records are entered in the table in the
form they autometically comes in this way.
The table name is T_GCombDetail and the fields are:
Comb T3 Per
1 80286 50
1 37395 50
2 80286 100
3 37385 100
4 80286 30
4 37395 70
I need the result in this way after quering
Comb 80286 37395
1 50 50
2 100
3 100
4 30 70
Waiting for your response,
With Regards,
"Nigel Rivett" wrote:

> select year ,
> A = (select QTY from tbl t2 where t.year = t2.year and item = 'A') ,
> B = (select QTY from tbl t2 where t.year = t2.year and item = 'B') ,
> C = (select QTY from tbl t2 where t.year = t2.year and item = 'C')
> from tbl t
> group by year
> order by year
>
|||On Sun, 6 Feb 2005 22:29:02 -0800, Daud wrote:

>Than u very much dear u were the only who replied my and understand the
>issue. But unfortunately u have give the example for static entry I need
>dynamic one in the form that whatever records are entered in the table in the
>form they autometically comes in this way.
Hi Daud,
There is no easy way to do this in SQL Server. Since this is basically a
formatting issue, the obvious recommendation is to do this at the client,
which is the "proper" place for formatting in a tiered architecture.
If you really must do this at the server, you'll either have to use
dynamic SQL to generate a query like the one Nigel suggests, with the
"correct" values taken from your table, or you can use a third party
product, like RAC4SQL (www.rac4sql.com). Or you could wait for the release
of SQL Server 2005 - I've read that this version will provide crosstab
functionality.
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)