Dynamic SQL query in SQL Server 2008 stored procedure - sql-server

I have 4 tables A, B, C and X.
X table :
Icid Trade_Id Counter_Party
---------------------------------
1 101 HDFC1
2 102 HDFC1
3 103 HDFC2
4 104 HDFC2
5 105 HDFC2
6 106 HDFC3
7 107 HDFC4
8 108 HDFC4
9 109 HDFC5
10 110 HDFC5
A table :
Icid Trade_Id Name
----------------------------------
1 110 HDFC Bank Pvt Ltd
2 105 HDFC Bank Pvt Ltd
3 101 HDFC Bank Pvt Ltd
4 102 HDFC Bank Pvt Ltd
B table:
Icid Trade_Id Name
----------------------------------------
1 103 HSBC Pvt Ltd
2 104 HSBC Pvt Ltd
3 106 HSBC Pvt Ltd
C table :
Icid Trade_Id Name
--------------------------------------
1 107 HK Pvt Ltd
2 108 HK Pvt Ltd
3 109 HK Pvt Ltd
A, B, C table like this and I have created another table and store tables name as below.
Findtbl table:
Icid Table_name
------------------
2 A
3 B
4 C
I need to table X Trade_ID find to tables A, B and C. If found in table A, then print location column INDIA, if found in table B then print location column USA, and if found in table C, then location column is Hongkong in #Temp table location, Trade_Id, Name columns:
#temp table :
Icid location Trade_Id Name
I tried this:
Declare #Fst_value nvarchar(100)
Declare #Counter INT
Declare #tablename nvarchar(20)
Declare #sql nvarchar(max)
Declare #isvalue int
Declare #loop int = 1
Declare #sqlsecond nvarchar(max)
Declare #sqlthird nvarchar(max)
SET #Fst_value = '104'
SET #Counter = (Select COUNT(Icid) From Findtbl)
WHILE #Loop < = #Counter
BEGIN
SET #tablename = (SELECT Table_name
FROM Findtbl
WHERE Icid = #Loop)
SET #sql = 'Select #isvalue = Icid,#sqlsecond = Trade_Id,#sqlthird = Name From '+#tablename+' Where Trade_Id = '+#Fst_value+''
Execute sp_executesql #sql,N'#isvalue int OUTPUT,#sqlsecond nvarchar OUTPUT,#sqlthird nvarchar OUTPUT',#isvalue = #isvalue OUT,#sqlsecond = #sqlsecond OUT,#sqlthird = #sqlthird OUT
if(#isvalue <> 0)
begin
Select #sql
break
end
SET #isvalue = 0
SET #Loop = #Loop + 1
END
This code is contained in a stored procedure.

I'll give it a shot, though you probably need to spend some time re-stating your question.
I'm going to have to ignore the procedural code, and use set-based code.
insert into #temp (Icid, location, Trade_Id, Name)
select case
when A.Trade_ID is not null then A.Icid
when B.Trade_ID is not null then B.Icid
when C.Trade_ID is not null then C.Icid
end
,case
when A.Trade_ID is not null then 'INDIA'
when B.Trade_ID is not null then 'USA'
when C.Trade_ID is not null then 'Hongkong'
end
,case
when A.Trade_ID is not null then A.Trade_ID
when B.Trade_ID is not null then B.Trade_ID
when C.Trade_ID is not null then C.Trade_ID
end
,case
when A.Trade_ID is not null then A.Name
when B.Trade_ID is not null then B.Name
when C.Trade_ID is not null then C.Name
end
from X
left outer join A on X.Trade_ID on A.Trade_ID
left outer join B on X.Trade_ID on B.Trade_ID
left outer join C on X.Trade_ID on C.Trade_ID

Related

How to count Exec Stored Procedure output values count based on conditions?

I Have one stored Procedure name called "sp_Proc_EmployeeDetails"
In this Procedure return values like below format:
SNo EmpName EmpNumber
----------------------------
1 abc 101
2 abc 102
3 abc1 103
4 abc2 104
5 abc2 105
6 abc2 106
In this above result I need below output while executing the stored procedure.
E.g:
Exec sp_Proc_EmployeeDetails
Output:
-------------
abc Count = 2
abc1 Count = 1
abc2 Count = 3
Create table Emp (
SNo int ,
EmpName varchar(50),
EmpNumber int
)
go
insert into Emp
select 1 ,'abc' , 101
union all select 2 ,'abc' ,102
union all select 3 ,'abc1' ,103
union all select 4 ,'abc2' ,104
union all select 5 ,'abc2' ,105
union all select 6 ,'abc2' ,106
go
create proc sp_Proc_EmployeeDetails
as
begin
select EmpName + ' Count = ' + cast( COUNT(*) as varchar(10)) as [Output:] from Emp
group by EmpName
end
go
exec sp_Proc_EmployeeDetails
Output:
---------------------
abc Count = 2
abc1 Count = 1
abc2 Count = 3

T-SQL Help require for Pivoting multiple columns

I have a table
City Region Zone Passcode SureveyBy SureveyDone SureveyExpiry
Noida Sector 62 East 1 ABC Y NULL
Noida Sector 2 West 1 XYZ N 6/10/2016
Delhi CP Cnt 10 ABC N 10/10/2018
Delhi KB West 11 RST Y NULL
I need result in this format
Final Result
DECLARE #col NVARCHAR(max),#sql NVARCHAR(max)
SELECT #col=ISNULL(#col+',[','[')+t.SureveyBy+']' FROM dbo.myTable AS t GROUP BY t.SureveyBy
PRINT #col
SET #sql='
SELECT * FROM dbo.myTable
PIVOT(MAX(SureveyDone) FOR SureveyBy IN ('+#col+')) p'
EXEC(#sql)

SQL Server: Pivot

Please help me with this. I have the data as this:
ID Name TotalCost IsCorporate
---- ---------------- ---------- -----------
1 Wash, Dry & Fold 175.00 1
2 Hand Wash and Fold 275.00 0
3 Pressing Only 25.00 0
4 Hand Wash and Fold 205.00 1
5 Pressing Only 100.00 0
If IsCorporate = 0 then the Total Cost will align to the Corporate column like this:
ID Wash, Dry & Fold Hand Wash and Fold Pressing Only Corporate
---- ---------------- ----------------- -------------- -----------
1 175.00
2 275.00
3 25.00
4 205.00
5 100.00
This is my stored procedure code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ReportSales]
AS
BEGIN
SELECT
IDJO, ISCORP, ST.[Wash, Dry & Fold], ST.[Pressing Only],
ST.[Dry Clean], ST.[Hand Wash and Fold], ST.[Wash, Dry & Press],
ST.[Stain Removal], ST.[Hand Wash and Press], CORPORATE
FROM
(SELECT
JO.Id AS IDJO, CI.Active AS ISCORP, ST.Name,
ISNULL(JO.TotalCost, 0) AS TC
FROM
JobOrders JO
INNER JOIN
ClientInformations CI ON JO.ClientId = CI.Id
INNER JOIN
JobOrderDetails JOD ON JO.Id = JOD.JOrderId
INNER JOIN
ServiceTypes ST ON JOD.ServiceId = ST.Id
INNER JOIN
Payments P ON JO.Id = P.JobOrderId
INNER JOIN
PaymentStatus PS ON JO.PaymentStatusId = PS.Id
INNER JOIN
Status S ON JO.StatusId = s.Id) AS J
PIVOT
(SUM(TC) for Name IN ([Wash, Dry & Fold], [Pressing Only], [Dry
Clean], [Hand Wash and Fold], [Wash, Dry & Press], [Stain Removal],
[Hand Wash and Press], [Corporate]) ) AS ST
END
Use dynamic column collection to select PIVOT Data, because it gives you any new column value added in table, suppose after 2-3 days if new Name say for XYZ added in your table even that it show your new column in PIVOT result:
CREATE TABLE ReportSales
(
ID INT,
Name VARCHAR(50),
TotalCost DECIMAL(10,2),
IsCorporate BIT
)
INSERT INTO ReportSales VALUES(1,'Wash, Dry & Fold',175.00,1)
,(2,'Hand Wash and Fold',275.00,0)
,(3,'Pressing Only',25.00,0)
,(4,'Hand Wash and Fold',205.00,1)
,(5,'Pressing Only',100.00,0)
DECLARE #Name AS NVARCHAR(MAX),#Query AS NVARCHAR(MAX);
SET #Name = STUFF((SELECT distinct ',' + QUOTENAME(Name)
FROM ReportSales c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET #Query =
'IF OBJECT_ID(''tempdb..#tblRS'') IS NOT NULL
DROP TABLE #tblRS
SELECT
ID,
RS.Name,
TotalCost,
ISCorporate,
CASE RS.IsCorporate WHEN 1 THEN 0 ELSE RS.TotalCost END AS Corporate
INTO #tblRS
FROM ReportSales RS
SELECT ID, ' + #Name + ',Corporate from
(
SELECT
*,
CASE ISCorporate WHEN 1 THEN TotalCost ELSE 0 END AS NewTotalCost
FROM #tblRS
) x
pivot
(
SUM(TotalCost)
FOR Name in (' + #Name + ')
) p '
PRINT(#query)
EXECUTE(#query)
#Name : It will give you your column list on which you wants to apply SUM

SQL Server Pivot Table with multiple column with dates

I have a PIVOT situation.
Source table columns:
Title Description Datetime RecordsCount
A California 2015-07-08 10:44:39.040 5
A California 2015-07-08 12:44:39.040 6
A California 2015-05-08 15:44:39.040 3
B Florida 2015-07-08 16:44:39.040 2
B Florida 2015-05-08 19:44:39.040 4
Now I need this pivoted as
2015-07-08 2015-05-08
Title Description
A California 11 3
B Florida 2 4
if we have two record counts on same dates (no matter of time) then sum them, else display in different column.
Trying to write something like this, but it throws errors.
Select * from #DataQualTest
PIVOT (SUM(RecordCount) FOR DateTime IN (Select Datetime from #DataQualTest) )
AS Pivot_Table
Please help me out with this.
Thanks
Not exactly the word for word solution but this should give you a direction.
create table #tmp
(
country varchar(max)
, date1 datetime
, record int
)
insert into #tmp values ('California', '2010-01-01', 2)
insert into #tmp values ('California', '2010-01-01', 5)
insert into #tmp values ('California', '2012-01-01', 1)
insert into #tmp values ('Florida', '2010-01-01', 3)
insert into #tmp values ('Florida', '2010-01-01', 5)
select * from #tmp
pivot (sum(record) for date1 in ([2010-01-01], [2012-01-01])) as avg
output
country 2010-01-01 2012-01-01
California 7 1
Florida 8 NULL
If you want to be more flexible, you need some pre-processing to get from full timestamps to days (in order for later on the PIVOT's grouping to actually have the anticipated effect):
CREATE VIEW DataQualTestView AS
SELECT
title
, description
, DATEFROMPARTS (DATEPART(yyyy, date_time),
DATEPART(mm, date_time),
DATEPART(dd, date_time)) AS day_from_date_time
, recordsCount
FROM DataQualTest
;
From there you could continue:
DECLARE #query AS NVARCHAR(MAX)
DECLARE #columns AS NVARCHAR(MAX)
SELECT #columns = ISNULL(#columns + ',' , '')
+ QUOTENAME(day_from_date_time)
FROM (SELECT DISTINCT
day_from_date_time
FROM DataQualTestView) AS TheDays
SET #query =
N'SELECT
title
, description
, ' + #columns + '
FROM DataQualTestView
PIVOT(SUM(recordsCount)
FOR day_from_date_time IN (' + #columns + ')) AS Pivoted'
EXEC SP_EXECUTESQL #query
GO
... and would get:
| title | description | 2015-05-08 | 2015-07-08 |
|-------|-------------|------------|------------|
| A | California | 3 | 11 |
| B | Florida | 4 | 2 |
See it in action: SQL Fiddle.
Please comment, if and as this requires adjustment / further detail.

Using PIVOT over a group by query

I've been trying to create a pivot for following query :
select mainstate, customertypeid, count(1) as [counter] from customers group by customertypeid, mainstate
This query should display as many customers types per state, it looks like this (order by doesn't matter) :
State|customertypeid|counter
UT 3 200
CA 3 500
NY 3 300
UT 2 100
CA 2 200
NY 2 120
UT 1 20
CA 1 50
NY 1 30
I've tried to use PIVOT as follow (I'm sure I'm wrong) :
SELECT *
FROM ( select mainstate, customertypeid, count(1) as [counter] from customers where customertypeid in (1,2,3) and mainstate != '' group by customertypeid, mainstate) as NonPivotedDataForReport2
PIVOT
(
COUNT([counter])
FOR mainstate IN ([# of Amb],[# Whole Sale Customers],[# Retail Customers])
) AS PivotedDataForReport2
I'm getting this :
customertypeid|type1|type2|type3
1 0 0 0
2 0 0 0
3 0 0 0
and the report should look like this :
State|type1|type2|type3
UT 20 100 200
CA 50 200 500
NY 30 120 300
*Ps : I don't really want to go back to CASE + SUM Statement,
Thanks a lot!
This will do:
SELECT mainstate [State],
[1] type1,
[2] type2,
[3] type3
FROM ( SELECT mainstate, customertypeid, COUNT(1) [counter]
FROM customers
WHERE customertypeid in (1,2,3)
AND mainstate != ''
GROUP BY customertypeid, mainstate) as NonPivotedDataForReport2
PIVOT(SUM([counter]) FOR customertypeid IN ([1],[2],[3])) AS PivotedDataReport2
This (perhaps slightly edited) should do the job for you without case/sum/pivot. Create a temp table, insert starting data and then dynamically add columns depending on how many customer type ids there is.
declare #s varchar(10), #xx1 varchar(500)
select distinct state into #temp from customers
DECLARE myCursor CURSOR FOR SELECT distinct customertypeid from customers
open MyCursor
FETCH NEXT FROM myCursor into #S
WHILE ##FETCH_STATUS = 0
begin
set #xx1 = 'alter table #temp add ['+#s+'] varchar(5)'
execute sp_executesql #xx1
set #xx1 = 'update a set a.['+#s+'] = coalesce(b.counter,0) from #temp a, customers b where b.customertypeid = '+#s+' and a.state = b.state'
execute sp_executesql #xx1
FETCH NEXT FROM myCursor into #S
End
Close myCursor
DEALLOCATE myCursor
select * from #temp

Resources