T-SQL (SS2K8) :: Using Case Within PIVOT Portion Of Crosstab Query

Apr 13, 2015

I have created a crosstab query using the Pivot statement that returns the expected results. The results look similar to the sample below:

ItemKey Description Aflatoxin Coliform Bacteria E_Coli Fumonisin Melamine Moisture Mold Salmonella Vomitoxin (DON) Yeast

1000 Item1000 1 0 0 1 0 1 0 1 1 0
1024 Item1024 1 0 0 1 0 1 0 1 1 0
135 Item135 1 0 0 1 0 1 0 1 1 0
107 Item107 0 0 0 0 0 1 0 1 1 0
106 Item106 1 0 0 1 0 1 0 1 1 0

I'm using this statement to create the result set:

SELECT ItemKey, Description, Aflatoxin, [Coliform Bacteria], [E_Coli],[Fumonisin],
Melamine,Moisture, Mold, Salmonella, [Vomitoxin (DON)], Yeast
FROM
(SELECT tblInventory.ItemKey, tblInventory.Description,
jctProductClassificationRequiredTest.ProductTestClassID, tlbTestType.TestDescription

[Code] .....

Instead of doing a Count for the Pivot (the count will always be either 0 or 1 due to the design of the table being used), I would like to return an "X" for those records with a count of 1, and return a blank (otherwise null) for those records with a count of 0. So, the result set would look like:

ItemKey Description Aflatoxin Coliform Bacteria E_Coli Fumonisin Melamine Moisture Mold Salmonella Vomitoxin (DON) Yeast
1000 Item1000 X X X X X
1024 Item1024 X X X X X
135 Item135 X X X X X
107 Item107 X X X
106 Item106 X X X X X

I tried using a Case statement within the PIVOT portion, but I either did it incorrectly or it's not possible to do use a Case within the Pivot. Can I easily accomplish this?

View 6 Replies


ADVERTISEMENT

T-SQL (SS2K8) :: Using CASE Statement Within A PIVOT

Jun 17, 2014

I am using a PIVOT function to obtain the Invoice Values, but they appear in different currencies so need to perform a case function.

But am struggling with the syntax;

This fails a syntax check with
Msg 156, Level 15, State 1, Line 33
Incorrect syntax near the keyword 'Case'.

[Code]....

View 2 Replies View Related

T-SQL (SS2K8) :: Using Case Statement In Pivot?

Jun 23, 2015

Can we use case in pivot like below? I am getting an error. I want to do Pivot on condition basis.

select (
Column1
,Column2
,Column3
,Column4
,coloumn5
from Mytable
) x
pivot
(
case when Column1 = 6 then sum(Column3) else max(Column4) End
for coloumn5 in (' + @COLS + ')
)p

View 2 Replies View Related

Transact SQL :: Dynamic Query For Multi-Header CrossTab Pivot

May 7, 2015

Have a table with a structure like:

FIELD_A
FIELD_B
FIELD_C
FIELD_D
AMOUNT

DATA_A1
DATA_B1
DATA_C2
DATA_D2
37540

[Code] ....

In such a case, devising a dynamic cross-tab query, to cover all the items, and result like a pivot like the following to represent the data in a multi-header pivot like following:

DATA_C1
DATA_C1
DATA_C1
DATA_C2
DATA_C2
DATA_C2

[Code] ....

View 5 Replies View Related

Help With Pivot Crosstab Query (was Query Idea ??)

Feb 19, 2008

consider the following table:
name , TaskDate
john , 01/01/2006
john , 01/03/2005
steve , 01/05/2006
i want to build a select statement that gives result like the following:
name , JanuaryTotal , FebruaryTotal
john , 150 , 110
steve , 170 , 50
so the result will be total tasks in specific month, is it doable in one select or should i turn around using hash tables ???

View 6 Replies View Related

T-SQL (SS2K8) :: Pivot And Unpivot In Same Query?

Feb 15, 2014

I have below table and within same query i need pivot and unpivot.

create table #temp(name1 varchar(10),name2 varchar(10),name3 varchar(10),month date,emp1 int,emp2 int,emp3 int,emp4 int)
insert into #temp values ('a','b','c','1-1-2013',1,2,3,4)
insert into #temp values ('a','b','c','1-2-2013',11,20,30,40)
insert into #temp values ('a','c','c','1-1-2013',22,30,80,40)
insert into #temp values ('a','c','c','1-2-2013',28,34,39,30)
select * from #temp

Now i need output in below format

name1,name2,name3,Emp,jan-13,feb-13
a,b,c,emp1,1,11
a,b,c,emp2,2,20
a,b,c,emp3,3,30
a,b,c,emp4,4,40
a,c,c,emp1,22,28
a,c,c,emp2,30,34
a,c,c,emp3,80,39
a,c,c,emp4,40,30

View 4 Replies View Related

Access Crosstab To An SQL Express PIVOT

Jan 8, 2007

I need some help in converting this crosstab SQL from an Access query to a View in SQL Server Express:TRANSFORM First(tblPhones.PhoneNumber) AS FirstOfPhoneNumber
SELECT tblPhones.ClientID
FROM tblPhones
GROUP BY tblPhones.ClientID
PIVOT tblPhones.PhoneType; 

View 14 Replies View Related

T-SQL (SS2K8) :: Returning Only Portion Of String?

Jun 19, 2014

i need to cut my string on 3 portion, for exemple my string is :

a) 1.9999-Q1

b) 01.9999-Q11

I need to keep all values before "." (point), and all values after "-" , and also all values between "." and "-", like this:

a) 1
9999
Q1

b) 01
9999
Q11

View 9 Replies View Related

T-SQL (SS2K8) :: Case Query - Unpivot Function

May 6, 2015

I've this result from my 'case' query;

Jan Feb Mar April
1 2 3 4

I want ;

Month Value
JAN 1
Feb 2
Mar 3
April 4

View 3 Replies View Related

T-SQL (SS2K8) :: Pivot Query - Convert Data From Original Table To Reporting View

Apr 8, 2014

I want to convert the data from Original Table to Reporting View like below, I have tried but not get success yet.

Original Table:
================================================================
Id || Id1 || Id2 || MasterId || Obs ||Dec || Act || Status || InstanceId
================================================================
1 || 138 || 60 || 1 || Obs1 ||Dec1 || Act1 || 0|| 14
2 || 138 || 60 || 2 || Obs2 ||Dec2 || Act2 || 1|| 14
3 || 138 || 60 || 3 || Obs3 ||Dec3 || Act3 || 1|| 14
4 || 138 || 60 || 4 || Obs4 ||Dec4 || Act4 || 0|| 14
5 || 138 || 60 || 5 || Obs5 ||Dec5 || Act5 || 1|| 14

View For Reporting:

Row Header:
Id1 || Id2 || MasterId1 || Obs1 ||Desc1 ||Act1 ||StatusId1||MasterId ||Obs2 ||Desc2 ||Act2 ||StatusId2 ||MasterId3||Obs3 ||Desc3 ||Act3 ||StatusId3||MasterId4||Obs4||Desc4 ||Act4 ||StatusId4 ||MasterId5||Obs5 ||Desc5 ||Act5 ||StatusId5||InstanceId

Row Values:
138 || 60 || 1 || Obs1 ||Desc1 ||Act1 ||0 ||2 ||Obs2 ||Desc2||Act2 ||1 ||3 ||Obs3||Desc3 ||Act3 ||2 ||4||Obs4||Desc4 ||Act4 ||0 ||5 ||Obs5 ||Desc5 ||Act5 ||1 ||14

View 6 Replies View Related

Pivot With Case

Jul 21, 2013

I have a pivot script but I would like to use a case statement,See below script and result

select top 10 id,

[1421],
[1420],
[1419],
[1418]
from (

select Purchaseid,Purchaseid as id, occasionid, transactionid
from ticket_facts ) as query
pivot (count(transactionid) for purchaseid

in (
[1421],
[1420],
[1419],
[1418])) as piv

[code]....

I would like the count to be

case (count(transactionid) when >0 then 1 else 0) end

how would I use the case statement with the pivot.

View 17 Replies View Related

Transact SQL :: How To Get Time Portion Only Of A Date For LINQ Query

Aug 4, 2015

I'm trying to translate this portion of VFP code into LINQ query:

select COUNT(ID) as conflicts
from dbo.max4sale where <<thisform.cWhere>>
AND Start_Time >= <<VFP2SQL(m.ltBegin + m.lnStartTime)>>
and Start_time <= <<VFP2SQL(m.ltEnd)>>
AND CONVERT(varchar(5),Start_Time,108) <= <<VFP2SQL(m.lcEndTime)>>
AND CONVERT(varchar(5),End_Time,108) >= <<VFP2SQL(m.lcStartTime)>>
<<m.lcDays>>

Here is my non-working attempt:

var startTime = new DateTime(1900, 1, 1, beginDateTime.Hour, beginDateTime.Minute, 0);
var endTime = new DateTime(1900, 1, 1, endDateTime.Hour, endDateTime.Minute, 0);
var daysOfWeek = dailyLimits.Where(dl => dl.Selected == true).Select(ds => ds.WeekDay).ToList();
if (daysOfWeek.Count() < 7) // not all days of the week selected

[Code] .......

First of all, I see a bug in my logic now as the first part of the query I need to do all the time and only the second part if the count < 7. But that's not my problem - I can not figure out how to make times comparison only using LINQ. Ideally I think I'd like to have cast(start_time as time) >= @p1 as a result to be executed by LINQ.

BTW, I am only getting the error in run-time that Parse can not be interpreted. So, I need to figure out another way of making LINQ recognize my intent of checking time portion of the date only.

View 9 Replies View Related

PIVOT Is Not Case-sensitive

Oct 27, 2006

Hi there,

I am using a PIVOT to count the number of chunk for each block type:
ex.:
block_type, chunk
a, <data>
a, <data>
b, <data> ...

My problem is that the block_type is case-sensitive, 'a' should not be counted as a 'A'.
How can I take the case in consideration?

I've tried to plug a COLLATE SQL_Latin1_General_CP1_CS_AS statement but it doesn't seem to be supported... Something like:
SELECT *
FROM recv.test_Blocks
PIVOT (
COUNT(chunk)
FOR block_type COLLATE SQL_Latin1_General_CP1_CS_AS
IN ([9.], a, B, h, q)
) AS pvt

Also something like:
IN (a, A)
returns an error: The column 'A' was specified multiple times for 'pvt'.

Thanks

View 1 Replies View Related

Crosstab Query

Mar 14, 2002

Hello everyone,

I have a tabel which looks like the follows:

CODE Type Number
1 account 20
1 empl 3
2 account 15
2 empl 6
3 account 32
3 empl 7

I need to show the results like

CODE Account Empl
1 20 3
2 15 6
3 32 7

I've tried different attempts but so far I always get 2 rows for each code,where one column shows a NULL value.
How do I have to do this ?

View 1 Replies View Related

Crosstab Query

Apr 5, 2001

Is there a way to write Crosstab query in SQL 7.0.

I have a Table which has partner, usernames and the city they are from, apart from other fields. I have another table which tracks the user activity on a day by number of visits to the web site. What i want is like this :-

Date Total Visits Atlanta Connecticut .... .... ....

03/01/2001 5025 567 324 .... .... ....
03/02/2001 6789 423 146 .... .... ....
... ... ... ... .... .... ....

I.E. I wan't the city names to appear as columns. The main catch here is that this data is for a given partner. So the city names could be different for different users from different partners. i.e. city names have to be generated on the fly by looking at the partner table and then by getting all its users and seeing what all are the cities involved.

Any help would be appreciated.

Thanks
Sumit.

View 1 Replies View Related

CrossTab Query

Mar 8, 2006

Is there a SQL version of a Crosstab Query??

View 14 Replies View Related

Crosstab Query

Sep 2, 2006

Crosstab concepts

I m using Sql server 2000 version
I want to create a simple crosstab… as like the follows

My table is as like

Date warehouse (WH) Delivery No
7/7/2006 10 10-1
7/7/2006 20 20-1
7/7/2006 20 20-2
7/7/2006 30 30-1
8/7/2006 10 10-2
8/7/2006 10 10-3
11/7/2006 30 30-2
11/7/2006 20 20-3


I want to make a report using crosstab in iReport…..

I want report will be as like


Date WH10 WH20 WH30 total delivery
7/7/2006 1 2 1 4
8/7/2006 2 0 0 2
11/7/2006 0 1 1 2


my questions are:

i create a crosstab procedure and it is running but

01. I want to know the query.
02. How I count/sum the wh in the crosstab? (cause the WH is arrange as row in my database table )

I want step by step instructions
I have not clear concepts about crosstab, but I know crosstab will give the solution of this problem

Please help……

Thanks


shohan

View 1 Replies View Related

Crosstab Query

Sep 2, 2006

Crosstab concepts

I m using Sql server 2000 version
I want to create a simple crosstab… as like the follows

My table is as like

Date warehouse (WH) Delivery No
7/7/2006 10 10-1
7/7/2006 20 20-1
7/7/2006 20 20-2
7/7/2006 30 30-1
8/7/2006 10 10-2
8/7/2006 10 10-3
11/7/2006 30 30-2
11/7/2006 20 20-3


I want to make a report using crosstab in iReport…..

I want report will be as like


Date WH10 WH20 WH30 total delivery
7/7/2006 1 2 1 4
8/7/2006 2 0 0 2
11/7/2006 0 1 1 2


my questions are:

i create a crosstab procedure and it is running but

01. I want to know the query.
02. How I count/sum the wh in the crosstab? (cause the WH is arrange as row in my database table )

I want step by step instructions
I have not clear concepts about crosstab, but I know crosstab will give the solution of this problem

Please help……

Thanks


shohan

View 1 Replies View Related

Crosstab Query

Sep 2, 2006

Crosstab concepts

I m using Sql server 2000 version
I want to create a simple crosstab… as like the follows

My table is as like

Date warehouse (WH) Delivery No
7/7/2006 10 10-1
7/7/2006 20 20-1
7/7/2006 20 20-2
7/7/2006 30 30-1
8/7/2006 10 10-2
8/7/2006 10 10-3
11/7/2006 30 30-2
11/7/2006 20 20-3


I want to make a report using crosstab in iReport…..

I want report will be as like


Date WH10 WH20 WH30 total delivery
7/7/2006 1 2 1 4
8/7/2006 2 0 0 2
11/7/2006 0 1 1 2


my questions are:

i create a crosstab procedure and it is running but

01. I want to know the query.
02. How I count/sum the wh in the crosstab? (cause the WH is arrange as row in my database table )

I want step by step instructions
I have not clear concepts about crosstab, but I know crosstab will give the solution of this problem

Please help……

Thanks


shohan

View 1 Replies View Related

Crosstab Query

Sep 2, 2006

Crosstab concepts

I m using Sql server 2000 version
I want to create a simple crosstab… as like the follows

My table is as like

Date warehouse (WH) Delivery No
7/7/2006 10 10-1
7/7/2006 20 20-1
7/7/2006 20 20-2
7/7/2006 30 30-1
8/7/2006 10 10-2
8/7/2006 10 10-3
11/7/2006 30 30-2
11/7/2006 20 20-3


I want to make a report using crosstab in iReport…..

I want report will be as like


Date WH10 WH20 WH30 total delivery
7/7/2006 1 2 1 4
8/7/2006 2 0 0 2
11/7/2006 0 1 1 2


my questions are:

i create a crosstab procedure and it is running but

01. I want to know the query.
02. How I count/sum the wh in the crosstab? (cause the WH is arrange as row in my database table )

I want step by step instructions
I have not clear concepts about crosstab, but I know crosstab will give the solution of this problem

Please help……

Thanks


shohan

View 3 Replies View Related

Crosstab Query

Sep 2, 2006

Crosstab concepts

I m using Sql server 2000 version
I want to create a simple crosstab… as like the follows

My table is as like

Date warehouse (WH) Delivery No
7/7/2006 10 10-1
7/7/2006 20 20-1
7/7/2006 20 20-2
7/7/2006 30 30-1
8/7/2006 10 10-2
8/7/2006 10 10-3
11/7/2006 30 30-2
11/7/2006 20 20-3


I want to make a report using crosstab in iReport…..

I want report will be as like


Date WH10 WH20 WH30 total delivery
7/7/2006 1 2 1 4
8/7/2006 2 0 0 2
11/7/2006 0 1 1 2


my questions are:

i create a crosstab procedure and it is running but

01. I want to know the query.
02. How I count/sum the wh in the crosstab? (cause the WH is arrange as row in my database table )

I want step by step instructions
I have not clear concepts about crosstab, but I know crosstab will give the solution of this problem

Please help……

Thanks


shohan

View 1 Replies View Related

CrossTab Query

Sep 17, 2007

Hi,

Currently working on a Attendance System project .
Iam storing the data in table against the employee code, date and status (basically a rowwise data).

Would require a query which can generate a cross-tab display with Employee Codes (on X axis), Dates (on Y-Axis) and with the Attendance Status.

The sample data for the same is as follows:

EmpCode Att_Date Att_Status
------- -------- ----------
001 01/01/2007 P
001 01/02/2007 A
.. ... ..
001 01/31/2007 P

002 01/01/2007 P
. . .
. . .
. . .

Would require the output as..

EmpCode 01/01/2007 01/02/2007 ...... 01/31/2007
------- ---------- ---------- ----------
001 P A P
002 A P P
. . . .
. . . .
. . . .

Thanking you in anticipation.

Jabez.

View 10 Replies View Related

Crosstab Query

Jul 23, 2005

Can someone show me how to write a query to convert this:Year, Account, Qtr1, Qtr2, Qtr3, Qtr42004, 12345, 100, 200, 300, 4002005, 23456, 200, 300, 400, 500to this:Year, Account, Quarters, Amount2004, 12345, Qtr1, 1002004, 12345, Qtr2, 2002004, 12345, Qtr3, 3002004, 12345, Qtr4, 4002005, 23456, Qtr1, 2002005, 23456, Qtr2, 3002005, 23456, Qtr3, 4002005, 23456, Qtr4, 500So far I've only found examples which convert values to columns but notfor columns to values. TIA... AL

View 2 Replies View Related

Crosstab Query

Sep 7, 2005

I have two tables Bill and Location.Bill(location_id int,prod_period datetime,consumption float,demand float)Location(location_id int,location_name varchar(45))I want to create a stored procedure that takes a parameter of @year. Ibasically want the procedure to return results that show locationswhere consumption and/or demand IS NULL or 0 for each month throughouta given year. I would like my results to look something like this:location_id year Jan Feb Mar Apr May Jun Jul Aug Sept OctNov Dec1 2005 0 0 02 2005 0 0 0 03 2005 0 0If data does exist for consumption or demand, I would like to show it,however I am really interested in the locations that have no dataassociated with them.Any ideas of how I can accomplish this?

View 1 Replies View Related

Need Help With Crosstab Query

Mar 10, 2008

I cant figure out what is going wrong here. I created this query to be able to show values across periods , and put it inside of a table instead of a matrix, for easier totals and subtotals. but for some reason my subquery is not pullin up the right amounts for "Goal Amount". The fields i have labeled as Period_1 , period_2 and so on work just fine. But the goals are not coming up with the same number as in the Sub query, if i ran it by its self. I dont know if maybe its the join, or what. but for some reason the numbers are way high then they should be. am i doing something wrong. Please let me know if i need to give more information.

This is what the output looks like now:
0.0000 0.0000 0.0000
4516182.0000 5569957.8000 2559169.8000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
3915126.9500 3275922.5500 1598011.0000
4526861.2500 6880829.1000 3078265.6500
629010.4000 589697.2500 353818.3500
2218175.9000 1984683.7000 1517699.3000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000

this is what it looks like in the subquery:
150539.4000 2008 1 80 12
150539.4000 2008 2 80 12
150539.4000 2008 3 80 12
39313.1500 2008 1 81 12
39313.1500 2008 2 81 12
39313.1500 2008 3 81 12
181074.4500 2008 1 82 12
181074.4500 2008 2 82 12
181074.4500 2008 3 82 12
116746.1000 2008 1 86 12
116746.1000 2008 2 86 12
116746.1000 2008 3 86 12
79900.5500 2008 1 87 12
79900.5500 2008 2 87 12
79900.5500 2008 3 87 12
the two numbers highlighted should have the same amounts.





Code Snippet








SELECT cusSales.Report_Level_Id AS Report_Level, cusSales.Customer_Sales_Summary_Year AS Year, Goals.Region_Key,
Qry_Report_Level_Brand.Report_Level_Description, RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Region_Name,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 1 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_1,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 2 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_2,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 3 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_3,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 4 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_4,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 5 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_5,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 6 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_6,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 7 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_7,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 8 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_8,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 9 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_9,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 10 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_10,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 11 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_11,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 12 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_12,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 13 THEN cusSales.Customer_Sales_Summary_Amount ELSE 0 END) AS Period_13,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 1 THEN Goal_Amount ELSE 0 END) AS GOAL_1,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 2 THEN Goal_Amount ELSE 0 END) AS GOAL_2,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 3 THEN Goal_Amount ELSE 0 END) AS GOAL_3,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 4 THEN Goal_Amount ELSE 0 END) AS GOAL_4,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 5 THEN Goal_Amount ELSE 0 END) AS GOAL_5,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 6 THEN Goal_Amount ELSE 0 END) AS GOAL_6,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 7 THEN Goal_Amount ELSE 0 END) AS GOAL_7,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 8 THEN Goal_Amount ELSE 0 END) AS GOAL_8,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 9 THEN Goal_Amount ELSE 0 END) AS GOAL_9,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 10 THEN Goal_Amount ELSE 0 END) AS GOAL_10,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 11 THEN Goal_Amount ELSE 0 END) AS GOAL_11,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 12 THEN Goal_Amount ELSE 0 END) AS GOAL_12,
SUM(CASE cusSales.Customer_Sales_Summary_Period WHEN 13 THEN Goal_Amount ELSE 0 END) AS GOAL_13
FROM RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Customer INNER JOIN
RC_DWDB_INSTANCE_1.dbo.Tbl_Customer_Sales_Summary_Fiscal AS cusSales INNER JOIN
Qry_Report_Level_Brand ON cusSales.Sub_Brand_Id = Qry_Report_Level_Brand.Sub_Brand_Id ON
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Customer.Customer_Code = cusSales.Customer_Code INNER JOIN
RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension ON
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Customer.Territory_Code = RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Territory_Code FULL
OUTER JOIN
(SELECT SUM(cusSales.Customer_Sales_Summary_Amount) AS Goal_Amount, cusSales.Customer_Sales_Summary_Year AS Year,
cusSales.Customer_Sales_Summary_Period AS Period, cusSales.Report_Level_Id,
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Region.Region_Key
FROM RC_DWDB_INSTANCE_1.dbo.Tbl_Customer_Sales_Summary_Fiscal AS cusSales INNER JOIN
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Customer AS Tbl_Territory_In_Customer_1 ON
cusSales.Customer_Code = Tbl_Territory_In_Customer_1.Customer_Code INNER JOIN
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Region ON
Tbl_Territory_In_Customer_1.Territory_Code = RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Region.Territory_Code
WHERE (cusSales.Customer_Sales_Summary_Year = 2008) AND (cusSales.Consolidated_Sales_Tables_Id = 8) and region_key=12
GROUP BY cusSales.Customer_Sales_Summary_Year, cusSales.Customer_Sales_Summary_Period, cusSales.Report_Level_Id,
RC_DWDB_INSTANCE_1.dbo.Tbl_Territory_In_Region.Region_Key) AS Goals ON
RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Region_Key = Goals.Region_Key AND
cusSales.Report_Level_Id = Goals.Report_Level_Id AND cusSales.Customer_Sales_Summary_Year = Goals.Year AND
cusSales.Customer_Sales_Summary_Period = Goals.Period
WHERE (cusSales.Customer_Sales_Summary_Year IN (2007, 2008)) AND (cusSales.Consolidated_Sales_Tables_Id = 6) AND
(NOT (cusSales.Bill_Customer_Code IN ('11825', '11990', '11971'))) AND (cusSales.Report_Level_Id IN (78, 80, 81, 82, 86, 87)) AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Group_Name <> 'BREEDER') AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Group_Name <> 'OTHER') AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Group_Name <> 'Vet - Corporate') AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Group_Name <> 'export') AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Group_Name <> 'Vet - Other') AND
(RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Region_Key = 12)
GROUP BY cusSales.Report_Level_Id, cusSales.Customer_Sales_Summary_Year, Goals.Region_Key, Qry_Report_Level_Brand.Report_Level_Description,
RC_DWDB_INSTANCE_1.dbo.Qry_Sales_Group_Dimension.Region_Name

View 1 Replies View Related

Crosstab SQL Query

Mar 18, 2008



I am trıed to do crosstab in sql server 2005 but I failed. I look all document on the net but they are all static means the column that are crosed are avaliable. but ın my query I do not know the count of the columns... Please help me...



This the my query...
Yağmur Duman Türk Masajı 11
Mehtap . Türk Masajı 10
Mehtap . Japon Masajı 3
Yağmur Duman TENİS KORTU1 SAAT 1
Mehtap . TENİS KORTU 1 SAAT 4
Yağmur Duman Kortu Kirası 1 saat 1
Mehtap . Kortu Kirası 1 saat 2
Administrator . BALI MASAJI 1
Yağmur Duman BALI MASAJI 10
Mehtap . BALI MASAJI 11
Mehtap . 6 EL MASAJI 1
Mehtap . COUPLE MASAJ 1
Yağmur Duman PILATES SINGLE 1
Yağmur Duman CİLT BAKIMI 3
Mehtap . CİLT BAKIMI 4
Mehtap . Çikolata Masajı 2
Yağmur Duman BAL BAKIMI 1
Mehtap . BAL BAKIMI 4
Yağmur Duman 4 EL MASAJI 1
Mehtap . 4 EL MASAJI 2
Yağmur Duman YOSUN BAKIMI 1
Mehtap . MASA TENİSİ 1
Mehtap . HAMAM 1
Mehtap . KESE & KÖPÜK 1
Yağmur Duman TRIO 1
Mehtap . TRIO 1
Özge Öztürk TRIO 1
Özge Öztürk 4 EL MASAJI 16
Ozan Çicek ISVEC MASAJI 1
Özge Öztürk ISVEC MASAJI 3
Ozan Çicek 4 EL MASAJI 5
Ozan Çicek BAL BAKIMI 3
Özge Öztürk BAL BAKIMI 10
Özge Öztürk THAI MASAJI 1
Ozan Çicek Çikolata Masajı 24
Özge Öztürk Çikolata Masajı 12
Ozan Çicek SICAK TAŞ 5
Özge Öztürk SICAK TAŞ 14
Özge Öztürk CİLT BAKIMI 17
Ozan Çicek SHATSU MASAJ 1
Özge Öztürk PILATES SINGLE 16
Özge Öztürk SHATSU MASAJ 4
Ozan Çicek CİLT BAKIMI 3
Ozan Çicek PILATES SINGLE 2
Özge Öztürk COUPLE MASAJ 10
Ozan Çicek COUPLE MASAJ 6
Özge Öztürk 6 EL MASAJI 16
Ozan Çicek 6 EL MASAJI 18
Ozan Çicek BALI MASAJI 44
Özge Öztürk BALI MASAJI 59
Ozan Çicek Kortu Kirası 1 saat 5
Özge Öztürk Kortu Kirası 1 saat 5
Ozan Çicek TENİS KORTU 1 SAAT 12
Özge Öztürk TENİS KORTU 1 SAAT 5
Ozan Çicek Japon Masajı 41
Özge Öztürk Japon Masajı 26
Ozan Çicek Türk Masajı 38
Özge Öztürk Türk Masajı 17
Yeşim Yıldırım Türk Masajı 32
Yeşim Yıldırım Japon Masajı 35
Yeşim Yıldırım TENİS KORTU 1 SAAT 3
Yeşim Yıldırım Kortu Kirası 1 saat 4
Yeşim Yıldırım BALI MASAJI 101
Yeşim Yıldırım 6 EL MASAJI 20
Yeşim Yıldırım COUPLE MASAJ 11
Yeşim Yıldırım SHATSU MASAJ 4
Yeşim Yıldırım PILATES SINGLE 3
Yeşim Yıldırım CİLT BAKIMI 10
Yeşim Yıldırım THAI MASAJI 1
Yeşim Yıldırım Çikolata Masajı 18
Yeşim Yıldırım SICAK TAŞ 14
Yeşim Yıldırım BAL BAKIMI 4
Yeşim Yıldırım 4 EL MASAJI 9
Yeşim Yıldırım HAMAM 1
Yeşim Yıldırım KESE & KÖPÜK 1
Yeşim Yıldırım SAUNA TEK GİRİŞ 3
Yeşim Yıldırım YOSUN BAKIMI 1
Yeşim Yıldırım TRIO 2
Funda Güngörür TRIO 3
Funda Güngörür 4 EL MASAJI 5
Funda Güngörür BAL BAKIMI 8
Funda Güngörür THAI MASAJI 4
Funda Güngörür Çikolata Masajı 10
Funda Güngörür SICAK TAŞ 14
Funda Güngörür SHATSU MASAJ 5
Funda Güngörür CİLT BAKIMI 18
Funda Güngörür PILATES SINGLE 9
Funda Güngörür COUPLE MASAJ 11
Funda Güngörür 6 EL MASAJI 15
Funda Güngörür BALI MASAJI 60
Funda Güngörür Kortu Kirası 1 saat 4
Funda Güngörür TENİS KORTU 1 SAAT 6
Funda Güngörür Japon Masajı 41
Funda Güngörür Türk Masajı 58
Ayşegül Duman Türk Masajı 22
Ayşegül Duman TENİS KORTU 1 SAAT 1
Ayşegül Duman Japon Masajı 17
Ayşegül Duman Kortu Kirası 1 saat 1
Ayşegül Duman BALI MASAJI 53
Ayşegül Duman 6 EL MASAJI 2
Ayşegül Duman COUPLE MASAJ 7
Ayşegül Duman SHATSU MASAJ 7
Ayşegül Duman PILATES SINGLE 9
Ayşegül Duman SICAK TAŞ 7
Ayşegül Duman CİLT BAKIMI 10
Ayşegül Duman Çikolata Masajı 5
Ayşegül Duman THAI MASAJI 1
Ayşegül Duman BAL BAKIMI 6
Ayşegül Duman ISVEC MASAJI 2
Ayşegül Duman TRIO 3
Ayşegül Duman KESE & KÖPÜK 1
Ayşegül Duman 4 EL MASAJI 15
Mert Ilgın HAMAM 2
Mert Ilgın YOSUN BAKIMI 2
Mert Ilgın SAUNA TEK GİRİŞ 1
Mert Ilgın MASA TENİSİ 1
Mert Ilgın 4 EL MASAJI 11
Mert Ilgın KESE & KÖPÜK 1
Mert Ilgın TRIO 2
Mert Ilgın ISVEC MASAJI 1
Mert Ilgın BAL BAKIMI 1
Mert Ilgın Çikolata Masajı 6
Mert Ilgın THAI MASAJI 1
Mert Ilgın SICAK TAŞ 11
Mert Ilgın CİLT BAKIMI 7
Mert Ilgın SHATSU MASAJ 3
Mert Ilgın PILATES SINGLE 6
Mert Ilgın 6 EL MASAJI 9
Mert Ilgın COUPLE MASAJ 11
Mert Ilgın BALI MASAJI 28
Mert Ilgın Kortu Kirası 1 saat 2
Mert Ilgın TENİS KORTU 1 SAAT 5
Mert Ilgın Japon Masajı 6
Mert Ilgın Türk Masajı 11
Aslı Öztürk Türk Masajı 14
Aslı Öztürk Japon Masajı 2
Aslı Öztürk TENİS KORTU 1 SAAT 1
Aslı Öztürk BALI MASAJI 31
Aslı Öztürk 6 EL MASAJI 2
Aslı Öztürk COUPLE MASAJ 1
Aslı Öztürk PILATES SINGLE 10
Aslı Öztürk CİLT BAKIMI 5
Aslı Öztürk SICAK TAŞ 4
Aslı Öztürk Çikolata Masajı 1
Aslı Öztürk BAL BAKIMI 2
Aslı Öztürk ISVEC MASAJI 1
Aslı Öztürk TRIO 3
Aslı Öztürk 4 EL MASAJI 2
Aslı Öztürk KESE & KÖPÜK 2
Ahmet Yılmaz TRIO 2
Ahmet Yılmaz Çikolata Masajı 2
Ahmet Yılmaz CİLT BAKIMI 5
Ahmet Yılmaz SICAK TAŞ 4
Ahmet Yılmaz PILATES SINGLE 20
Ahmet Yılmaz SHATSU MASAJ 1
Ahmet Yılmaz COUPLE MASAJ 2
Ahmet Yılmaz BALI MASAJI 22
Ahmet Yılmaz Japon Masajı 2
Ahmet Yılmaz Kortu Kirası 1 saat 1
Ahmet Yılmaz Türk Masajı 10
Ceren Yıldızdoğan Türk Masajı 4
Ceren Yıldızdoğan Japon Masajı 3
Ceren Yıldızdoğan BALI MASAJI 26
Ceren Yıldızdoğan 6 EL MASAJI 4
Ceren Yıldızdoğan SHATSU MASAJ 5
Ceren Yıldızdoğan PILATES SINGLE 12
Ceren Yıldızdoğan SICAK TAŞ 11
Ceren Yıldızdoğan CİLT BAKIMI 3
Ceren Yıldızdoğan Çikolata Masajı 3
Ceren Yıldızdoğan THAI MASAJI 1


this the query result but I want to do this
Türk Masajı TENİS KORTU1 SAAT Kortu Kirası 1 saat ............

Yağmur Duman 11 1 1 ......


can you any idea about my problem

View 13 Replies View Related

Ordering Pivot Table Using Case Else

Aug 27, 2007

I have created a Pivot table using Case Else with a combination of Row_Number function. What I'm looking for is to try to Order it in a specific way. Manivannan.D.Sekaran, helped me with another Pivot table that I had and it worked great. So I decided to learn how to do a Pivot table using Case Else. Sample Data is the following without the Case Else


UserID LastName FirstName DocumentDesc docFileName
1 Smith Paul Resume PSmithResume.pdf
1 Smith Paul PhdStatistics phdstatstranscript.pdf
1 Smith Paul MS Applied Statistics MsAStats.pdf
1 Smith Paul MS Operation Research MsOpResearch.pdf
2 Jackson Jane MS Information Systems MsInforSystems.pdf
2 Jackson Jane Resume JaneJacksonResume.pdf

This is my query for my Pivot using Case Else:




Code Snippet
Select UserID, LastName, FirstName,
MAX(Case When RecID=1 Then DocumentDesc Else '' End)As Document1,
Max(Case When RecID=1 Then docFileName Else '' End) As DocumentFileName,
Max(Case When RecID=2 Then DocumentDesc Else '' End)As Document2,
Max(Case When RecID=2 Then docFileName Else '' End) As DocumentFileName,
Max(Case When RecID=3 Then DocumentDesc Else '' End)As Document3,
MAX(Case When RecID=3 Then docFileName Else '' End) As DocumentFileName,
Max(Case When RecID=4 Then DocumentDesc Else '' End)As Document4,
Max(Case When RecID=4 Then docFileName Else '' End) As DocumentFileName,
Max(Case When RecID=5 Then DocumentDesc Else '' End)As Document5,
Max(Case When RecID=5 Then docFileName Else '' End) As DocumentFileName,
Max(Case When RecID=6 Then DocumentDesc Else '' End)As Document6,
Max(Case When RecID=6 Then docFileName Else '' End)As DocumentFileName
From (
Select a.UserID, a.LastName, a.FirstName, b.FileName, b.DocumentDesc, b.DocumentTypeID, ROW_NUMBER() OVER(PARTITION BY a.UserID ORDER BY a.UserID) AS RecID
FROM Person a JOIN
Documents b ON
a.UserID = b.UserID
Where b.DocumentTypeID = '126d2beb-f7a1-4bf1-b9c0-dded37d3a6bc' Or b.DocumentTypeID = '9087956e-1fb0-4f3d-ba33-ef31d79141af'
) X
Group by UserID, LastName, Firstname
Order by LastName







Code Snippet
The Output is the following with the Pivot applied: (I'm excluding UserID, LastName, FirstName for space purposes)

The column headings are:

Document1 DocumentFileName Document2 DocumentFileName Document3 DocumentFileName Document4 DocFilename

The Data for document and filename columns:

Ms Information Systems MsInforSystems.pdf Resume JaneJacksonResume.pdf

Resume PSmithResume.pdf PhdStatistics Phdstatstranscript.pdf Ms Applied Statistics MsAstats.pdf






What I want is to have the column called Document1 to have only Resumes and the rest of the other columns to have the other files.

The following data places the data into one table.

Create Table #data (
[UserID] int ,
[LastName] Varchar(100) ,
[FirstName] Varchar(100) ,
[DocumentDesc] Varchar(100) ,
[docFileName] Varchar(100)
);

Insert Into #data Values('1','Smith','Paul','Resume','PSmithResume.pdf');
Insert Into #data Values('1','Smith','Paul','PhdStatistics','phdstatstranscript.pdf');
Insert Into #data Values('1','Smith','Paul','MSAppliedStatistics','MsAstats.pdf');
Insert Into #data Values('1','Smith','Paul','MSOperationResearch', 'MsOpResearch.pdf');
Insert Into #data Values('2','Jackson','Jane','MsInformationSystems', 'MsInforSystems.pdf');
Insert Into #data Values('2','Jackson','Jane','Resume', 'JaneJacksonResume.pdf');

View 4 Replies View Related

T-SQL (SS2K8) :: PIVOT Without Aggregation

Jun 28, 2015

I have a table imported from a legacy Oracle database that stores values vertically in name/value pairs. I store it in table-type variable that is an exact copy of the structure:

DECLARE @OracleEngData TABLE
( DataSourceCHAR(8)
, [OMNI_NUMBER] INTEGER
, [TIMESTAMP] INTEGER
, [DATA_TYPE] NVARCHAR(24)
, [PARAMETER] NVARCHAR(32)
, [PARAMETER_VALUE] NVARCHAR(132));

If this information were pivoted horizontally: OMNI_NUMBER would be the primary key.

TIMESTAMP is a 10-digit integer that represents the number of seconds since 1/1/1970 UTC that requires additional conversion. DATA_TYPE is not the data type. It is a general categorization of the next two columns.PARAMTER would be the column headings if it were horizontal..PARAMETER_VALUE would be the data value in that column.

I would like to try to use PIVOT to list the PARAMETER column values as column headers. This seems to work fine. What's confusing me is that I'd like it to list the PARAMETER_VALUE column values as raw data, just as it is in the source version, without having to apply some sort of aggregate function to it. Here's a CSV sample of the data you can paste into Excel. I'm trying to transform this:

OMNI_NUMBER,TIMESTAMP,DATA_TYPE,PARAMETER,PARAMETE_VALUE
506026,1413240436,test_data,cnr,211250000,54.8
506026,1413244259,test_data,cnr,211250000,53.2
506026,1413244679,test_data,cnr,211250000,53.1
506026,1413309646,test_data,cnr,211250000,53.4
506026,1413315987,test_data,cnr,211250000,53

[code]...

But I don't want the sum of the values or the average of the values, just the values. The PIVOT syntax seems to require an aggregate operation.

View 2 Replies View Related

Help With Crosstab (was Query Help Needed!)

Aug 9, 2007

Hey,

i have a table which has the foll data:

employeecode Amount AmountDescription
1 100 x
2 200 y
3 150 x
4 300 z

now i need to fetch this data such that i can display the output as :

empcode x y z
1 100
2 200
3 150
4 300

any suggestions?????????

platform: SQL Server 2000


thanx!

View 8 Replies View Related

Crosstab Query Problem

Oct 7, 2006

month wise production

Format that I want

item codenameJulyAugSepOctNovDecjan

1002pvc resin 3020115060140
3501SWR pipe566045801002020




I create crosstab procedure as follows

create procedure up_CrossTab (@SelectStatement varchar(1000),
@PivotColumn varchar(100),
@Summary varchar(100),
@GroupbyField varchar(100),
@OtherColumns varchar(100) = Null)
AS
/*
Inputs are any 1000 character or less valid SELECT sql statement,
the name of the column to pivot (transform to rows), the instructions to summarize the data, the field you want to group on, and other fields returned as output. 1
*/
set nocount on
set ansi_warnings off

declare @Values varchar(8000);
set @Values = '';

set @OtherColumns= isNull(', ' + @OtherColumns,'')
/*
An 8000 varchar variable called @values is created to hold the [potentially filtered] values in the pivot column. @Values is initiated to an empty string. Then, a temporary table is created to hold each unique value. After the table is created, its rows are loaded into the variable @values. It's usefullness completed, the temporary table is destroyed. 2
*/
create table #temp (Tempfield varchar(100))

insert into #temp
exec ('select distinct convert(varchar(100),' + @PivotColumn + ') as Tempfield FROM (' + @SelectStatement + ') A')

select @Values = @Values + ', ' +
replace(replace(@Summary,'(','(CASE WHEN ' + @PivotColumn + '=''' +
Tempfield + ''' THEN '),')[', ' END) as [' + Tempfield )
from #Temp
order by Tempfield

drop table #Temp
/*
Finally, a dynamic sql select statement is executed which takes the GroupByField, and OtherColumns, passed into the procedure, and each of the Values from the Pivot Column from the passed in SELECT statement . 3
*/
exec ( 'select ' + @GroupbyField + @OtherColumns + @Values +
' from (' + @SelectStatement + ') A GROUP BY ' + @GroupbyField)

set nocount off
set ansi_warnings on
GO


And then my sql query is as like


EXEC up_CrossTab 'SELECT ProdId, GrnDate,Quantity FROM inteacc..IcGrnD IcGrnD
INNER JOIN inteacc..IcProduct IcProduct ON (IcGrnD.ProdId=IcProduct.ProdId) ',
'Year(GrnDate)', 'sum(Quantity)[]', 'ProdId'




error occurring


ambiguous column name ‘ProdId’




But when I compile this query

EXEC up_CrossTab 'SELECT grnNo,GrnDate,Quantity FROM inteacc..IcGrnD IcGrnD
INNER JOIN inteacc..IcProduct IcProduct ON (IcGrnD.ProdId=IcProduct.ProdId) ',
'Month(GrnDate)', 'sum(Quantity)[]','GrnNo'


Output


GrnNo12249
1220NullNull20
2Null20Null10
3NullNull300Null
4NullNull10Null



I could not understand the error.

What will I do to get the format I want?


shohan

View 1 Replies View Related

Subquery, Select Case, Pivot Help. Is There An Easier Way?

Apr 12, 2008



I'm trying to select from a table with three columns. I want these columns to be spread out among multiple columns based on the values. I hope someone can shed some light on this. I might be able to use pivot, but don't know how the syntax would roll for this.

Here is the example of dummy values and the output I am trying to obtain.




drop table table1

create table table1

(Category int, Place int, Value int)

insert into table1 values

(1, 1, 20)

insert into table1 values

(1,2, 12)

insert into table1 values

(1,3, 30)

insert into table1 values

(2,1, 34)

insert into table1 values

(2,2, 15)

insert into table1 values

(2,3, 78)



select Category,

(select top 1 value from table1 where place = 1 and Category = t1.Category) as place1,

(select top 1 value from table1 where place = 2 and Category = t1.Category) as place2,

(select top 1 value from table1 where place = 3 and Category = t1.Category) as place3

from Table1 t1

group by Category




Thanks for the help.

View 5 Replies View Related

Matrix / Crosstab Query In SQL Server

Jan 16, 2007

How can I produce a Matrix/Crosstab table using SQL Query Analyzer/SQL Server?

I have the following tables:

people(unique_identifier, employee_number, known_as_and_surname ... )
exitin(unique_identifier, parent_identifier, category, score ... )

Relationship: exitint.parent_identifier = people.unique_identifier which is a one-many relationship (one employee can have many records in exitint)

example data:
please see attachment (cant make a pretty table appear in the post...)

The zip file has an excel spreadsheet with what I have and want to produce as an example.

I want to make this in SQL (SQL Server 2000 I think)


I have been looking all over the internet for a solution but I just cant get one to work! :eek:

Thank you all in advance! <3

View 13 Replies View Related

Classification Crosstab Query - 2000

Jul 23, 2005

Hello --I think this is the term for what I want (something that could be generatedin ACCESS using a pivot table, or, maybe Yukon).We have data for sales by sales people in sales regions. More than oneperson sells in a region.We want to display data as follows:salesperson's names----------- ----------- ----------- ----------- ----------- -----------region 1region 2 row/column values are sales amounts for person inthat regionregion 3We will add a WHERE clause for the period of time covered.I don't want to have to change the query if a new salesperson or new regionis added.Can this be done in SQL Server 2000, Analysis Services, OLAP, anywhere?Can someone direct me to examples of how to do this?Thanks for any direction.Larry Mehl

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved