Please Help Unpivoted Pivot Check Each Column Succession-and Add Result Row At END
Apr 9, 2008
nedd help from the from the wonderful people here
i am stuck in !
i need to check each column succession
the chacking is for 3 columns (day1+day11+day111) (day2+day22+day222) (day3+day33+day333) (day4+day44+day444) (day5+day55+day555)
and if i don't have the value
than add to a new result row
if not show the missing value in the result row at END
if i have in the fields columns A B C than
ok-A
ok-B
ok-C
if i don't have the value A B C than than show it like this
no-A
no-B
no-Cif i don't have the value c than than show it like this no-C (on the left )
if i don't have the value a than than show it like this no-A (on middle)
if i don't have the value b than than show it like this no-B (on right)
IF have duplicity a or b or c show in the result row at END 2-C=(thre is tow time the C)
hi i need help problem common character "X"or"Z" the problem is that i must use the character "X"or"Z" i explain i need to do this if i put the day1 = "X"or"Z" instead of day1 = 'A' in day1 it be calculated as day1 = 'A' if i put the day11 ="X"or"Z" instead of day11 = 'B' in day11 it be calculated as day11 = 'B' if i put the day1 = "X"or"Z" instead of day111 = 'C' in day111 it be calculated as day111 = 'C' ..... i put example below
Iain J wrote:
Here's a query that will get you the result row you desire
Code Snippet -- Set up sample data declare @data table (id int, fname nvarchar(10), val int, day1 nvarchar(1), day11 nvarchar(1), day111 nvarchar(1), day2 nvarchar(1), day22 nvarchar(1), day222 nvarchar(1), day3 nvarchar(1), day33 nvarchar(1), day333 nvarchar(1), day4 nvarchar(1), day44 nvarchar(1), day444 nvarchar(1), day5 nvarchar(1), day55 nvarchar(1), day555 nvarchar(1))
-- Query starts here select null as id, null as fname, 0 as val, case day1A when 0 then 'NO' when 1 then 'ok' else cast(day1A as nvarchar) end + '-A' as day1, case day1B when 0 then 'NO' when 1 then 'ok' else cast(day1B as nvarchar) end + '-B' as day11, case day1C when 0 then 'NO' when 1 then 'ok' else cast(day1C as nvarchar) end + '-C' as day111, case day2A when 0 then 'NO' when 1 then 'ok' else cast(day2A as nvarchar) end + '-A' as day2, case day2B when 0 then 'NO' when 1 then 'ok' else cast(day2B as nvarchar) end + '-B' as day22, case day2C when 0 then 'NO' when 1 then 'ok' else cast(day2C as nvarchar) end + '-C' as day222, case day3A when 0 then 'NO' when 1 then 'ok' else cast(day3A as nvarchar) end + '-A' as day3, case day3B when 0 then 'NO' when 1 then 'ok' else cast(day3B as nvarchar) end + '-B' as day33, case day3C when 0 then 'NO' when 1 then 'ok' else cast(day3C as nvarchar) end + '-C' as day333, case day4A when 0 then 'NO' when 1 then 'ok' else cast(day4A as nvarchar) end + '-A' as day4, case day4B when 0 then 'NO' when 1 then 'ok' else cast(day4B as nvarchar) end + '-B' as day44, case day4C when 0 then 'NO' when 1 then 'ok' else cast(day4C as nvarchar) end + '-C' as day444, case day5A when 0 then 'NO' when 1 then 'ok' else cast(day5A as nvarchar) end + '-A' as day5, case day5B when 0 then 'NO' when 1 then 'ok' else cast(day5B as nvarchar) end + '-B' as day55, case day5C when 0 then 'NO' when 1 then 'ok' else cast(day5C as nvarchar) end + '-C' as day555 from (select sum(case when character = 'A' then 1 else 0 end) + sum(case when day11 = 'A' then 1 else 0 end) + sum(case when day111 = 'A' then 1 else 0 end) as day1A, sum(case when day1 = 'B' then 1 else 0 end) + sum(case when day11 = 'B' then 1 else 0 end) + sum(case when day111 = 'B' then 1 else 0 end) as day1B, sum(case when day1 = 'C' then 1 else 0 end) + sum(case when day11 = 'C' then 1 else 0 end) + sum(case when day111 = 'C' then 1 else 0 end) as day1C, sum(case when day2 = 'A' then 1 else 0 end) + sum(case when day22 = 'A' then 1 else 0 end) + sum(case when day222 = 'A' then 1 else 0 end) as day2A, sum(case when day2 = 'B' then 1 else 0 end) + sum(case when day22 = 'B' then 1 else 0 end) + sum(case when day222 = 'B' then 1 else 0 end) as day2B, sum(case when day2 = 'C' then 1 else 0 end) + sum(case when day22 = 'C' then 1 else 0 end) + sum(case when day222 = 'C' then 1 else 0 end) as day2C, sum(case when day3 = 'A' then 1 else 0 end) + sum(case when day33 = 'A' then 1 else 0 end) + sum(case when day333 = 'A' then 1 else 0 end) as day3A, sum(case when day3 = 'B' then 1 else 0 end) + sum(case when day33 = 'B' then 1 else 0 end) + sum(case when day333 = 'B' then 1 else 0 end) as day3B, sum(case when day3 = 'C' then 1 else 0 end) + sum(case when day33 = 'C' then 1 else 0 end) + sum(case when day333 = 'C' then 1 else 0 end) as day3C, sum(case when day4 = 'A' then 1 else 0 end) + sum(case when day44 = 'A' then 1 else 0 end) + sum(case when day444 = 'A' then 1 else 0 end) as day4A, sum(case when day4 = 'B' then 1 else 0 end) + sum(case when day44 = 'B' then 1 else 0 end) + sum(case when day444 = 'B' then 1 else 0 end) as day4B, sum(case when day4 = 'C' then 1 else 0 end) + sum(case when day44 = 'C' then 1 else 0 end) + sum(case when day444 = 'C' then 1 else 0 end) as day4C, sum(case when day5 = 'A' then 1 else 0 end) + sum(case when day55 = 'A' then 1 else 0 end) + sum(case when day555 = 'A' then 1 else 0 end) as day5A, sum(case when day5 = 'B' then 1 else 0 end) + sum(case when day55 = 'B' then 1 else 0 end) + sum(case when day555 = 'B' then 1 else 0 end) as day5B, sum(case when day5 = 'C' then 1 else 0 end) + sum(case when day55 = 'C' then 1 else 0 end) + sum(case when day555 = 'C' then 1 else 0 end) as day5C from @data) x
how count column in pivot table- and add result row i need to calculate each column for example day1 day2 day3 day4 day5 ------------------------------------------------------------------------- 1 2 1 2 3 1 2 3 2 2 2 3 2 1 2 2 3 0 0 0 -----------------------------------------------------------new result row ok ok 1|2|3 1 3
i need to check each column if i have twice each number if not show the missing number TNX
Code Block DECLARE @Employee TABLE (ID INT, Date SMALLDATETIME, ShiftID TINYINT) DECLARE @WantedDate SMALLDATETIME, -- Should be a parameter for SP @BaseDate SMALLDATETIME, @NumDays TINYINT SELECT @WantedDate = '20080301', -- User supplied parameter value @BaseDate = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', @WantedDate), '19000101'), @NumDays = DATEDIFF(DAY, @BaseDate, DATEADD(MONTH, 1, @BaseDate))
IF @Numdays > 28 BEGIN SELECT p.ID, p.[1] , p.[2],p.[3], p.[4], p.[5], p.[6], p.[7], p.[8], p.[9], p.[10], p.[11], p.[12], p.[13], p.[14], p.[15], p.[16], p.[17], p.[18], p.[19], p.[20], p.[21], p.[22], p.[23], p.[24], p.[25], p.[26], p.[27], p.[28], p.[29], p.[30], p.[31] FROM ( SELECT ID, DATEPART(DAY, Date) AS theDay, ShiftID FROM v_Employee WHERE Date >= @BaseDate AND Date < DATEADD(MONTH, 1, @BaseDate) ) AS y PIVOT ( min(y.ShiftID) FOR y.theDay IN ([1], [2], [3], [4], [5], [6], [7],[8] , [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]) ) AS p END
the number of results per CaseID are varable some have 1 record others up to 36 records..I need to pivot the results based off of the competed date Name has 20 distinct values but we can have diplicates...so something like
Case Name Date 1 This 2015-01-01 1 That 2015-01-03 2 That 2013-01-01 2 Then 2014-12-03 2 This 2015-01-01 2 There 2015-04-03 3 This 1999-01-01 3 That 2001-01-02 3 This 2006-01-01
looks like this
1 This That 2 That Then This There 3 This That This
help i need to check each column and if i don't have the value than add to a new result row
if not show the missing value
if i have in the field A B C than OK (like day1 + day2)
if i don't have the value A B C than than show it like this a|b|c (like day3)
if i don't have the value c than than show it like this -|-|c (like day4)
if i don't have the value a than than show it like this a|-|- (like day5)
if i don't have the value b than than show it like this -|b|- (like day6)
day1 day2 day3 day4 day5 day6 ------------------------------------------------------------------------- * c * a c a * a * a c * a b # b b c b * $ Q c s c # 0 0 0 * ---------------------------------------------------------------------new result row ok ok a|b|c -|-|c a|-|- -|b|- TNX
I have information on clothes in a table that I want to select out to a result set in a different structure - I suspect that this will include some kind of pivot (or cross-join?) but as I've never done this before I'd appreciate any kind of help possible.
Current structure is:
Colour Size Quantity ----------------------- Red 10 100 Red 12 200 Red 14 300 Blue 10 400 Blue 12 500 Blue 14 600 Green 10 700 Green 12 800 Green 14 900 Green 16 1000
I want to produce this result set:
Colour Size10 Size12 Size14 Size16 ------------------------------------- Red 100 200 300 0 Blue 400 500 600 0 Green 700 800 900 1000
There could be any number of sizes or colours.
Is this possible? Can anyone give me any pointers?
hi need help from the wonderful people here i am working on project and convert a OLD DOS program to SQL server on the web this is the resalt i need to show
I have some data which is vertical...I want to create a pivot query in SQL that will give me a result that is horizontal like this. I cannot find a way of doing it without lots of IF or CASE statements?
in table Databackup company keepmonth ----------------- ------------------- 001 12002 12003 6005 607917 609747 6
I run this query select Max(keepmonth) as keep from Databackup why the result is 6 not 12? I think the max value should 12 , have no idea why it return 6 does my query error? thank you
I have a query to check the records of job has received after 4pm Monday to Friday and it has completed before 9am next day and also weekend it should be Friday after 4pm and before Monday 10am for particular financial year period. I have my job table which has full date but it doesn't show the date exactly Monday to Friday it shows only as 12-12-2014 like that.
Is there a way we can get Table and Column name in separate column using PIVOT or something?Right now what i have is:
Text QueryPlan Plan_handle Name Value
select id,name,Address from person <showPlznXML... 010101 Table Person select id,name,Address from person <showPlznXML... 010101 column id select id,name,Address from person <showPlznXML... 010101 Table Person
Hi there, I am a new member of this site and I am not very much aware of T-sql's working. My question is what if I need to get one column's data to be the heading of another column. To be very exact I have a school's database. The table I am talking about is of the results of students. The table contains Student ID, Subject ID, Total marks of the subject, Marks obtained in the subject. Now I want to print a report by generating data from this table. Right now the data is something like this StuID - - - SubID - - - -Tot - - -Obt 1 - - - - - - -1 - - - - - - -50 - - - 38 1 - - - - - - -2 - - - - - - -50 - - - 41 1 - - - - - - -3 - - - - - - -50 - - - 42 1 - - - - - - -4 - - - - - - -50 - - - 40 2 - - - - - - -1 - - - - - - -50 - - - 35 2 - - - - - - -2 - - - - - - -50 - - - 40 2 - - - - - - -3 - - - - - - -50 - - - 42 2 - - - - - - -4 - - - - - - -50 - - - 41
StudentID and SubjectID fields are related to other tables so I can get the names from there but when I need the report I need the data in the form of StuID - Sub 1 - - - Sub 2 - - - Sub 3 - - - -Sub4 1 - - - - 38 - - - - - - 41 - - - - - - 42 - - - - - - 40 2 - - - - 35 - - - - - - 40 - - - - - - 42 - - - - - - 41
The Subjects can be different for different students so the query should be dynamic instead of hard coding the names of the subjects. I hope I am clear with my question. The subjectIDs or their names will become the headings and they will contain the obtained marks for that subjects in their columns just for the reports. I have also checked the PIVOT function but was not able to do what I wanted. Thanks.
Hello,I am trying to follow along with the Data Access tutorial under the the "Learn->Videos" section of this website, however I am running into an error when I try to use the "Edit -> Update" function of the Details View form: I'll post the error below. Any clues on how to fix this? Thanks in advance!!! ~DerrickColumn name 'Assigned_To' appears more than once in the result column list. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Column name 'Assigned_To' appears more than once in the result column list.Source Error: Line 1444: } Line 1445: try { Line 1446: int returnValue = this.Adapter.UpdateCommand.ExecuteNonQuery(); Line 1447: return returnValue; Line 1448: }
I have two columns. policyNumber contains a 12-13 varchar string AllPolicyNumbersIncluded contains one or more 12-13 varchar strings (policy nums) seperated by commas
I want to check if policyNumber is contained in AllPolicyNumbersIncluded?
I have policyNumber LIKE AllPolicyNumbersIncluded which works when only one policy number is in AllPolicyNumbersIncluded and incidently works switched around AllPolicyNumbersIncluded LIKE policyNumber I assume because they are equal.
Can anyone tell me how to check if one column's value is a substring of another - without going through every possible substring of the second
This SQL Select below doesn't show the total value. Always shows NULL. Why?
SELECT Ano, FORMAT((QPivot.[1]),'##,##0.00','pt-BR') As trimestre1, FORMAT((QPivot.[2]),'##,##0.00','pt-BR') As trimestre2, FORMAT((QPivot.[3]),'##,##0.00','pt-BR') As trimestre3, FORMAT((QPivot.[4]),'##,##0.00','pt-BR') As trimestre4, Municipio,
I'm wondering how it's possible to have a select statement resultant rows concatenated into one row and column. For example: select letter from alphabet_table a b c d e ... 26 rows returned.
Other than a cursor, how would I write a query to return the following: row1: abcdefghijkl...
I'm trying to Pivot and I keep getting an "Invalid Column Name" error, which I can't figure out since, if I run the query and exclude the Pivot statement, the query runs fine.
Columns ItemNmbr Char(31) not null SetupTime_I Numeric(19,5) not null WCID_I Char(11) not null select ItemNmbr,SetupTime_I, WCID_I from RT010130 Results
Now run select ItemNmbr,SetupTime_I, WCID_I from RT010130 pivot (sum(SetupTime_I) for WCID_I in ([BLA01],[URE02])) PVT
And I get an Invalid Column Name error for both SetupTime_I and WCID_I - which, as far as I can tell, is demonstrably incorrect.
Hi All, I have never used PIVOT before but looks exactly what I want for this scenario: I have rows of dates associated with ID of Hotels and Room avalability for each Hotel/Date..... I want to show the sum of the rooms per date as columns I am using something like this:SELECT dbHotelID ,[09/20/2007]as [Today],[09/21/2007]as [Today+1],[09/22/2007]as [Today+2] FROM vwRoomAvailable PIVOT (SUM(dbRoomNumber) FOR AvailableDate IN ([09/20/2007], [09/21/2007], [09/22/2007])) AS pAs you can see I know how may days I want in advance so know how many columsn so its not dynamic.. I just dont know what the dates are:I would like to do something like: DECLARE @todayDate varchar(255), DECLARE @todayPlusOne varchar(255), DECLARE @todayPlusTwo varchar(255) SET @todayDate = CONVERT(CHAR, GETDATE(),101)SET @todayPlusOne = CONVERT(CHAR, DATEADD(d, 1, GETDATE(),101)SET @todayPlusTwo = CONVERT(CHAR, DATEADD(d, 2, GETDATE(),101) SELECT dbHotelID,@todayDate as Today,@todayPlusOne as [Today+1],@todayPlusTwo as [Today+2] FROM vwRoomAvailable PIVOT (SUM(dbHotelRoomAvailabilityNumber) FOR AvailableDate IN ([@todayDate], [@todayPlusOne], [@todayPlusTwo])) AS pBut I can’t seem to put the variable in the PIVOT value list or GETDATE() Anyone got any ideas or do I just try and do this another way and forgot PIVOT. I am using sql server 2005 express. Thanks in advance. Lee
I have the following SQL which i want to convert to a stored procedure having dynamic SQL to generate column numbers (1 to 52) for Sale_Week.Also, I want to call this stored procedure from Excel using VBA, passing 2 parameters to stored procedure in SQL Server e.g,
DECLARE @KPI nvarchar(MAX) = 'Sales Value with Innovation' DECLARE @Country nvarchar(MAX) = 'UK'
I want to grab the resultant pivoted table back into excel. how to do it?
USE [Database_ABC] GO DECLARE @KPI nvarchar(MAX) = 'Sales Value with Innovation' DECLARE @Country nvarchar(MAX) = 'UK'
I am trying to work on a database with 3 tables. To make it easier I have created a couple of temp tables to work out the syntax.
CREATE TABLE #owner ( [NameId] tinyint IDENTITY(1,1) NOT NULL, [Name] varchar(50) NOT NULL )
INSERT INTO #owner VALUES ('ME'); INSERT INTO #owner VALUES ('Other');
CREATE TABLE #propertyType ( [TypeId] tinyint IDENTITY(1,1) NOT NULL, [Name] varchar(50) NOT NULL )
INSERT INTO #propertyType VALUES ('Home'); INSERT INTO #propertyType VALUES ('Car');
CREATE TABLE #property ( [NameId] tinyint NOT NULL, [TypeId] tinyint NOT NULL, [Value] varchar(50) NOT NULL )
INSERT INTO #property VALUES (1,1, 'Blue'); INSERT INTO #property VALUES (1,2, 'Black'); INSERT INTO #property VALUES (2,1, 'Red'); INSERT INTO #property VALUES (2,2, 'Black');
DROP TABLE #owner; DROP TABLE #propertyType; DROP TABLE #property
| NameId | Name | | 1 | ME| | 2 | other |
| TypeId | Name | | 1 | Home | | 2 | Car |
| NameId | TypeId | Value | | 1 | 1 | Blue | | 1 | 2 | Black | | 2 | 1 | Red | | 2 | 2 | Black |
Where property value is some arbitrary detail. The real propertyType has 50 or 60 rows and not every property has all of the values. I am trying to create a pivot table that would look like so that I can present the data in an easier to understand format:
[Owner | Home | Car ] [ME | Blue | Black ] [Other| Red | Black ]
The propertyTypes are added often, and I don't really have the ability to change them. There is a unique constrant on property on nameid and typeid so there will never be two of the same property with the same owner. Any help would be very helpful.
I have written a Query to Pivot this data like below:
SELECT WAREHOUSE,ITEM, QTY
FROM
(SELECT ITEM,WAREHOUSE,FOR1,FOR2,for3,for4,for5,for6,for7,for8,for9,for10, for11,for12,for13,for14,for15,for16,for17,for18,for19,for20,for21, for22,for23,for24 FROM mvxreport.tbldmsForecasttoMovex) p
UNPIVOT
(QTY FOR tbldmsForecasttoMovex IN (FOR1,FOR2,for3,for4,for5,for6,for7, for8,for9,for10,for11,for12,for13,for14,for15,for16,for17,for18,for19, for20,for21,for22,for23,for24))AS unpvt
I would like to add some more code to the query, so for each FOR% column, i can put a numeric value in it. The value will be the numbers ,1 - 24 . One for each line as this represents Months Forward.
Q1-2013 Q2-2013 Q3-2013 Q4-2013 2013 Total Distinct Count Member Distinct Count Member Distinct Count Member Distinct Count Member Member Months AMBULANCE - LAND 264 301 355 352 1272 AMBULATORY SURGICAL CENTER 16 38 30 34 118 COMMUNITY MENTAL HEALTH CENTER 6 7 11 13
I have the above data, and want to create a column adding up the Distinct count member of each quarter. The number of distinct count member of each quarter is from a measure. The result I want to create is the column of 2013 Total Member Months as shown above. How to create a dax formula.
Using PowerPivot I created a connection to a view on a SQL Server database. All fields imported correctly.A column has since been added to the view. How do I get this to appear in my PowerPivot?
I have two tables shown below and I wish to add a calculated column to Table 1.
Table1 Table2 ID Activity Activity 1 Activity 2 Activity 3 etc etc etc Total 1 Activity 1 values values values Total value 2 Activity 2 values values values Total value 3 Activity 3 values values values Total value etc
I want each row in the new column to show the corresponding activity divided by the sum of the total. So in the row with Activity 3 I want the formula SUM(Table2[Activity 3])/SUM(Table2[Total]). Also the number of activities will vary.
I am actually wanting to calculate a more complicated formula which calculates the correlation .
I am trying to figure out how to pivot a temporary table. I have a table which starts with a date but the number of columns and columns names will vary but will be type INT (Data, col2,col3,col4………….n)
So it could look like
Date , TS-Sales, Budget , Toms sales 01-Jan-14,100,120,300 02-Jan-14,80,150,300 03-Jan-14,100,20,180
What I want is to be able to sum al the columns but without knowing the name and the amount columns to start with this is a manually processes. How could I automate this?