Can we create pivot table with Multiple columns and each column contains multiple rows.
For example...........
Database Table:
BatchID BatchName Chemical Value
--------------------------------------------------------
BI-1 BN-1 CH-1 1
BI-2 BN-2 CH-2 2
--------------------------------------------------------
This is the table , i need to display like below in Excel Sheet
BI-1 BI-2
BN-1 BN-2
------------------------------------------
CH-1 1 null
------------------------------------------
CH-2 null 2
------------------------------------------
Here BI-1,BN-1 are two rows in a single columns i need to display chemical value as row of that.
Could Please help me to solve this problem.
Thank You.
Its bit difficult to get it in the format you want however a closer option would be as below -
Sum of Value BatchName BatchID
BN-1 BN-1 Total BN-2 BN-2 Total Grand Total
Chemical BI-1 BI-2
CH-1 1 1 NULL NULL 1
CH-2 NULL NULL 1 1 1
Grand Total 1 1 1 1 2
Process -
Create a pivot table
Add checmical to row field
Add Batch Name and Batch ID fields to column field
Add value to data items field.
Hope this helps... Cheers..
Related
Please find below input and required output. i need a query/procedure/function in T-SQL to get this output.
Requirement: I have table a and table b.
get all the date ranges from table b and missing date ranges from table a (when compared with table b).
Basically we need to make sure all the date ranges in table a, need to be covered in the output
Input
table b
Start date End date ID
1/1/2009 9/30/2009 1
1/1/2013 9/30/2013 1
11/1/2014 11/30/2014 1
2/2/2015 12/31/2016 1
table a
1/1/2009 12/31/2011 1
1/1/2013 9/30/2013 1
1/1/2014 4/30/2014 1
10/1/2014 12/31/2014 1
2/2/2015 12/31/9999 1
Output
table b
Start date End date ID
1/1/2009 9/30/2009 1
1/1/2013 9/30/2013 1
11/1/2014 11/30/2014 1
2/2/2015 12/31/2016 1
table a
10/1/2009 12/31/2011 1
1/1/2014 4/30/2014 1
10/1/2014 10/31/2014 1
12/1/2014 12/31/2014 1
1/1/2017 12/31/9999 1
Table [a] contain 4 records and table[b] contain 5 records. And you need that 5th record to be inserted into table [b]. If this is correct then...
Do simple outer join on table [a] and [b] on start data and end data. Get the value from table [a] where you find respective row NULL in table [b]
seems simple as it is :-)
Happy coding!!!
I have a scenario where in I want a field in a table to be incremented sequentially.
Suppose I have a table Test, with columns TestID, TestResult1,2 etc.. and TestCount.
I have data bulk inserted into the table. Some of the records may be retests, which means new data to be inserted matches existing records in the table, Test Count should be updated.Matching is done on TestID
If the table is as follows:
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
Data to be inserted in
TestID TestResult1 TestResult2..
12 3 5
12 2 2
The table should be updated as
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
12 3 5 3
12 2 2 4
I tried adding a trigger on the table to update the TestCount Counting the number of records that matches. But it was updating the table as follows
TestID TestResult1 TestResult2.. TestCount
12 1 1 1
12 2 2 2
13 4 1 1
12 3 5 3
12 2 2 3
CREATE TRIGGER trgTestCount
on Test
AFTER INSERT
AS
Update g
Set TestCount= (Select Count(*)+1 from Test g join INSERTED g1 where g.TestID=g1.TestID )
from Test g
This is a SSIS package and I use a dataflow task to load data from STg table to test table.
Can you tell me what I am doing wrong here?
If you can change the table structure, I would suggest adding an Identity column, change the TestCount column to a computed column, and have it's value as the count of distinct test ids that are the same is the current row test id and the create date is lower than the current value of the Identity column.
This will eliminate the need for triggers and will handle inserting multiple records with the same test id automatically.
I have a table with 106 columns. One of those columns is a "Type" column with 16 types.
I want 16 rows, where the Type is distinct. So, row 1 has a type of "Construction", row 2 has a type of "Elevator PVT", etc.
Using Navicat.
From what I've found (and understood) so far, I can't use Distinct (because that looks across all rows), I can't use Group By (because that's for aggregating data, which I'm not looking to do), so I'm stuck.
Please be gentle- I'm really really new at this.
Below is a part of the table (how can I share this normally?)- it's really big so I didn't share the whole thing. Below is a partial result I'm looking for, where the Violation_Type is unique and the rest of the columns display.
Got it.. Sheesh... (took me forever, but got it...)
D_ID B_ID V_ID V_Type S_ID c_f d_y l_u p_s du_p
------ ------ ------- -------------- ------ ----- ------ ------ ----- ------
184 117 V 032 Elevator PVT 2 8 0 0
4 140 V 100 Construction 1 8 0 0
10 116 V 122 Electric 1 8 2005 0 0
11 117 V 033 Boiler Local 1 0 2005 0 0
You can use ROW_NUMBER for this:
SELECT *
FROM(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY V_Type ORDER BY (SELECT NULL))
FROM tbl
)t
WHERE rn = 1
Modify the ORDER BY depending on what row you want to prioritize.
From the documentation:
Returns the sequential number of a row within a partition of a result
set, starting at 1 for the first row in each partition.
This means that for every row within a partition (specified by the PARTITION BY clause), sql-server assigns a number from 1 depending on the order specified in the ORDER BY clause.
ROW_NUMBER requires an ORDER BY clause. SELECT NULL tells the sql-server that we do not want to enforce a particular order. We just want the rows numbered by partition.
The WHERE rn = 1 obviously filters only rows that has a ROW_NUMBER of 1. This gives you one row for every V_TYPE available.
ID Name tuition num of courses
1 Brandon 4430 6
2 Lisa 2300 3
3 Victoria null 0
4 Jack 3330 4
The type of the tuition column is money, but I need to return return null in my select statement without updating the values in the table.
I tried nullif(tuition is not null), but it didn't work.
How can I return results like those in the table below, without updating the table or modifying the data in database?
ID Name tuition num of courses
1 Brandon null 6
2 Lisa null 3
3 Victoria null 0
4 Jack null 4
If you are returning null for every row, just code the column as:
NULL AS Tuition
Example query:
SELECT Id, Name, NULL as Tuition, NumCourses FROM TheTable
I have created the table and inserted records as you have shown above
It is a self join query.
-- To make sure that the underlying table is not updated run both the queries together.
select TT.Id, TT.Name,
nullif(TT.Tuition, BT.Tuition) as Tuition, TT.NOCs
from tblTuition TT
join tblTuition BT
on TT.Id = Bt.Id
select * from tblTuition
Whenever you need to get value as null then you can use like this,
SELECT NULL AS ABC FROM MYTABLE
So above statement add one ABC column in your select list AS All NULL Values, same thing can be use as getting a Default value e.g. if you want to get 1 then simply use SELECT 1 AS ABC FROM MYTABLE
I using Microsoft SQL Server 2008 to try and identify the chronological order of data points in order to create a filter field that will allow me to create a query that only includes the first and last record for each ID number, where multiple rows represent different data points from the same ID
Here is an example of my current data and desired data to give a better idea of what I mean:
Current Data
ID Indicator Date
1 1 1988-02-11
1 1 1989-03-9
1 1 1993-04-3
1 1 2001-05-4
2 1 2000-01-01
2 1 2001-02-03
2 1 2002-04-22
3 1 1990-02-01
3 1 1998-02-01
3 1 1999-03-02
3 1 2000-04-02
4 0 NA
Desired Data
ID Indicator Date Order_Indicator
1 1 1988-02-11 1
1 1 1989-03-9 2
1 1 1993-04-3 3
1 1 2001-05-4 4
2 1 2000-01-01 1
2 1 2001-02-03 2
2 1 2002-04-22 3
3 1 1990-02-01 1
3 1 1998-02-01 2
3 1 1999-03-02 3
3 1 2000-04-02 4
4 0 NULL NULL
The field I want to create is the "Order_Indicator" field in the "Desired Data" table and with the only relevant records are records with Indicator = 1. With this information I would create a query where I only select the rows where Order_Indicator = 1 and Order_Indicator = MAX(Order_Indicator) for each "row group" that share the same ID. Does anyone have any idea about how I might go about this? I know I could do this very easily in Excel but I need to do it on SQL server in order for it to be reproducible with my colleagues.
Thank you so much in advance!
You can do this with the ranking functions:
select c.*,
(case when indicator = 1
then row_number() over (partition by id, indicator order by [date])
end) as OrderIndicator
from current c
This assigns a sequential number based on the date and indicator. The case statement takes care of the indicator = 0 case.
By the way, this assumes that "date" is being stored as a date.
Use below query :
select YourTable.ID,
YourTable.indicator,
case when date<>'NA' then date end as date,
case when indicator = 1 then row_number() over (partition by id, indicator order by ID) end as Order_Indicator
from YourTable