Convert row data to column data SQL Server 2008 - sql-server-2008

I have gone through the existing solutions that are present and couldn't progress much.I have a table as below #CompCodes
Table
I want the data to be displayed as
ID Code1 Code1EffDate Code2 Code2EffDate
and so on till Code15 Code15EffDate
If it doesn't have value for any of the code and effdate it should be null
Currently I am using the below query which is not that accurate. for some ids it is giving me wrong values and some times it is duplicating the codes
SELECT DISTINCT
pcc.[ID],
pcd1.Code as Code1, pcd1.EffDate as EffDate1,
pcd1.CancelDate as CancelDate1, pcd1.ReasonCode as ReasonCode1,
pcd2.Code as Code2, pcd2.EffDate as EffDate2,
pcd2.CancelDate as CancelDate2, pcd2.ReasonCode as ReasonCode2,
pcd3.Code as Code3, pcd3.EffDate as EffDate3,
pcd3.CancelDate as CancelDate3, pcd3.ReasonCode as ReasonCode3,
pcd4.Code as Code4, pcd4.EffDate as EffDate4,
pcd4.CancelDate as CancelDate4, pcd4.ReasonCode as ReasonCode4,
pcd5.Code as Code5, pcd5.EffDate as EffDate5,
pcd5.CancelDate as CancelDate5, pcd5.ReasonCode as ReasonCode5,
pcd6.Code as Code6, pcd6.EffDate as EffDate6,
pcd6.CancelDate as CancelDate6, pcd6.ReasonCode as ReasonCode6,
pcd7.Code as Code7,pcd7.EffDate as EffDate7,pcd7.CancelDate as CancelDate7,pcd7.ReasonCode as ReasonCode7,
pcd8.Code as Code8,pcd8.EffDate as EffDate8,pcd8.CancelDate as CancelDate8,pcd8.ReasonCode as ReasonCode8,
pcd9.Code as Code9,pcd9.EffDate as EffDate9,pcd9.CancelDate as CancelDate9,pcd9.ReasonCode as ReasonCode9,
pcd10.Code as Code10,pcd10.EffDate as EffDate10,pcd10.CancelDate as CancelDate10,pcd10.ReasonCode as ReasonCode10,
pcd11.Code as Code11,pcd11.EffDate as EffDate11,pcd11.CancelDate as CancelDate11,pcd11.ReasonCode as ReasonCode11,
pcd12.Code as Code12,pcd12.EffDate as EffDate12,pcd12.CancelDate as CancelDate12,pcd12.ReasonCode as ReasonCode12,
pcd13.Code as Code13,pcd13.EffDate as EffDate13,pcd13.CancelDate as CancelDate13,pcd13.ReasonCode as ReasonCode13,
pcd14.Code as Code14,pcd14.EffDate as EffDate14,pcd14.CancelDate as CancelDate14,pcd14.ReasonCode as ReasonCode14,
pcd15.Code as Code15,pcd15.EffDate as EffDate15,pcd15.CancelDate as CancelDate15,pcd15.ReasonCode as ReasonCode15,''
FROM #CompCodes pcc
LEFT OUTER JOIN ProvCompDetails pcd1 ON pcc.ProviderID=pcd1.ProviderID AND pcd1.ColumnNumber=1
LEFT OUTER JOIN ProvCompDetails pcd2 ON pcc.ProviderID=pcd2.ProviderID AND pcd2.ColumnNumber=2
LEFT OUTER JOIN ProvCompDetails pcd3 ON pcc.ProviderID=pcd3.ProviderID AND pcd3.ColumnNumber=3
LEFT OUTER JOIN ProvCompDetails pcd4 ON pcc.ProviderID=pcd4.ProviderID AND pcd4.ColumnNumber=4
LEFT OUTER JOIN ProvCompDetails pcd5 ON pcc.ProviderID=pcd5.ProviderID AND pcd5.ColumnNumber=5
LEFT OUTER JOIN ProvCompDetails pcd6 ON pcc.ProviderID=pcd6.ProviderID AND pcd6.ColumnNumber=6
LEFT OUTER JOIN ProvCompDetails pcd7 ON pcc.ProviderID=pcd7.ProviderID AND pcd7.ColumnNumber=7
LEFT OUTER JOIN ProvCompDetails pcd8 ON pcc.ProviderID=pcd8.ProviderID AND pcd8.ColumnNumber=8
LEFT OUTER JOIN ProvCompDetails pcd9 ON pcc.ProviderID=pcd9.ProviderID AND pcd9.ColumnNumber=9
LEFT OUTER JOIN ProvCompDetails pcd10 ON pcc.ProviderID=pcd10.ProviderID AND pcd10.ColumnNumber=10
LEFT OUTER JOIN ProvCompDetails pcd11 ON pcc.ProviderID=pcd11.ProviderID AND pcd11.ColumnNumber=11
LEFT OUTER JOIN ProvCompDetails pcd12 ON pcc.ProviderID=pcd12.ProviderID AND pcd12.ColumnNumber=12
LEFT OUTER JOIN ProvCompDetails pcd13 ON pcc.ProviderID=pcd13.ProviderID AND pcd13.ColumnNumber=13
LEFT OUTER JOIN ProvCompDetails pcd14 ON pcc.ProviderID=pcd14.ProviderID AND pcd14.ColumnNumber=14
LEFT OUTER JOIN ProvCompDetails pcd15 ON pcc.ProviderID=pcd15.ProviderID AND pcd15.ColumnNumber=15
Order by pcc.ID
Can some one please help in writing this more efficiently?
Thanks in advance

Related

Inner join query return more than one row?

I dont no what is the problem with the below query. It returns 12 rows instead of single row. Kindly suggest me whether below query is correct or not. If not, correct the problem in below query. Thanks in advance.
SELECT
M.name, MR. review, MAN.running_time, MC.screen_name, MI.producer,
MI.director,MI.story, MI.bgm_score,MI.screenplay, MI.music,
MS.story, MS.screenplay, MS.dialogue, MS.direction, MS.music, MS.bgm,
MS.cinematography, MS.characterization, MS.shotlocation, MS.editing,
MS.production, MS.performance, CC.certificate FROM tttbl_review_language RL
JOIN tttbl_censor_certificate CC
JOIN tttbl_movie M
INNER JOIN tttbl_movie_review MR ON MR.movie_id=M.id
INNER JOIN tttbl_movie_analytics MAN ON MAN.movie_id=M.id
INNER JOIN tttbl_movie_cast MC ON MC.movie_id=M.id
INNER JOIN tttbl_movie_info MI ON MI.censor_id=CC.id AND MI.movie_id=M.id
INNER JOIN tttbl_movie_score MS ON MS.movie_id=M.id
WHERE M.id=1
You have not on condition between tttbl_review_language RL, tttbl_censor_certificate CC and tttbl_movie M
then these table could produce a cartesian product with other result ..
select
M.name,
MR. review,
MAN.running_time,
MC.screen_name,
MI.producer,
MI.director,
MI.story,
MI.bgm_score,
MI.screenplay,
MI.music,
MS.story,
MS.screenplay,
MS.dialogue,
MS.direction,
MS.music,
MS.bgm,
MS.cinematography,
MS.characterization,
MS.shotlocation,
MS.editing,
MS.production,
MS.performance,
CC.certificate
FROM tttbl_review_language RL
JOIN tttbl_censor_certificate CC
JOIN tttbl_movie M
INNER JOIN tttbl_movie_review MR ON MR.movie_id=M.id
INNER JOIN tttbl_movie_analytics MAN ON MAN.movie_id=M.id
INNER JOIN tttbl_movie_cast MC ON MC.movie_id=M.id
INNER JOIN tttbl_movie_info MI ON MI.censor_id=CC.id AND MI.movie_id=M.i

Left Join issue in mysql query

I have a query
SELECT DISTINCT c.camp_id AS camp_id,
c.camp_key AS camp_key,
c.camp_active AS camp_active,
c.camp_deleted AS camp_deleted,
c.camp_name AS camp_name,
c.camp_cpc AS camp_cpc,
c.camp_destination AS camp_destination,
camp_token1,
camp_token2,
camp_token3,
camp_token4,
camp_token5,
camp_token6,
camp_token7,
camp_token8,
camp_token9,
camp_token10,
token1_field,
token2_field,
token3_field,
token4_field,
token5_field,
token6_field,
token7_field,
token8_field,
token9_field,
token10_field,
group_name,
group_id,
source_id,
source_name,
user_name
FROM mt_campaigns c
LEFT JOIN mt_offers USING (camp_id)
LEFT JOIN mt_groups USING (group_id)
LEFT JOIN mt_traffic_sources USING (source_id)
LEFT JOIN mt_account WHERE c.owner_id = mt_account.user_id
WHERE camp_deleted=0
Now I want to join another table(mt_account) which has a column called user_id and it matches with the the owner_id column of the mt_canpaigns.
How can i edit the join query?
replace
LEFT JOIN mt_account WHERE c.owner_id=mt_account.user_id
for
LEFT JOIN mt_account ON c.owner_id=mt_account.user_id
You need to use ON to define the fields you want to join on instead of WHERE

Mysql query with multiple joins and conditions

I am trying to connect four tables using joints,here i used left join to connect tables and my condition is all the goods,item should be same and all the site should be same.same site have multiple goods, so i want to get the sum number of goods from each table. my query is given
select
a.goods
,sum(a.no_of_units) as totala
, a.site
,b.item
,sum(b.quantity) as totalb
,b.site
,c.goods
,c.site
,sum(c.no_of_units) as totalc
,d.site
,d.goods
,sum(d.quantity)b as totald
from
inward_stock a
left join
opening_balance b
on
a.site=b.site
and
a.goods=b.item
left join
return_stock c
on
b.site=c.site
and
b.item=c.goods
left join
stock_consumed d
on
d.site=c.site
and
d.goods=c.goods
Can you put your conditions at the end of the joints like this:
select a.goods,sum(a.no_of_units) as totala, a.site,b.item,sum(b.quantity) as totalb,b.site,c.goods,c.site,sum(c.no_of_units) as totalc,d.site,d.goods,sum(d.quantity) as totald
from inward_stock a left join
opening_balance b on (a.site=b.site) left join
return_stock c on (b.site=c.site) left join
stock_consumed d on (d.site=c.site) where (a.goods=b.item) and (b.item=c.goods) and (d.goods=c.goods)
I have not tested your request but it seems that it's not bad.

SQL Join for Five Tables

I am actually trying to Join Five Tables in MySQL Database based upon RegionID which is a unique key present in all tables. I am new to it and using the following left join statement:
SELECT `tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*,
`tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*
FROM tbllistings
LEFT JOIN `webspace_db`.`tblregions`
ON `tbllistings`.`intregionid` = `tblregions`.`intregionid`
LEFT JOIN `webspace_db`.`tblcontent`
ON `tblregions`.`intregionid` = `tblcontent`.`intregionid`
LEFT JOIN `webspace_db`.`tblgallery`
ON `tblregions`.`intregionid` = `tblgallery`.`regionid`
LEFT JOIN `webspace_db`.`tbltowns`
ON `tblregions`.`intregionid` = `tbltowns`.`intregionid`
The problem that I am facing is each value is showing up more then 20+ times and I am unsure why is this happening. What I actually want is to simply join all fields from all tables based on RegionID.
Any help and suggestions are highly welcomed. Thanks a lot.
Use tbllistings.intregionid in every ON clause.
SELECT `tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*,
`tbllistings`.*,
`tblgallery`.*,
`tbltowns`.*,
`tblregions`.*,
`tblcontent`.*
FROM tbllistings
LEFT JOIN `webspace_db`.`tblregions`
ON `tblregions`.`intregionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tblcontent`
ON `tblcontent`.`intregionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tblgallery`
ON `tblgallery`.`regionid` = `tbllistings`.`intregionid`
LEFT JOIN `webspace_db`.`tbltowns`
ON `tbltowns`.`intregionid` = `tbllistings`.`intregionid`

Generate FULL JOIN with LinqToSQL

I have this linq query :
(from rapportBase in New_RapportReferencementBases
join rapportExtensionAll in New_RapportReferencementExtensionBases on rapportBase.New_RapportReferencementId equals rapportExtensionAll.New_RapportReferencementId into jointureRapportExtension
from rapportExtension in jointureRapportExtension.DefaultIfEmpty()
join packExtensionAll in New_PackExtensionBases on rapportExtension.New_PackId equals packExtensionAll.New_PackId into jointurePackExtension
from packExtension in jointurePackExtension.DefaultIfEmpty()
join packBaseAll in New_PackBases on packExtension.New_PackId equals packBaseAll.New_PackId into jointurePackBase
from packBase in jointurePackBase.DefaultIfEmpty()
join domaineBaseAll in New_DomaineBases on packExtension.New_DomaineId equals domaineBaseAll.New_DomaineId into jointureDomaineBase
from domaineBase in jointureDomaineBase.DefaultIfEmpty()
join domaineExtensionAll in New_DomaineExtensionBases on domaineBase.New_DomaineId equals domaineExtensionAll.New_DomaineId into jointureDomaineExtension
from domaineExtension in jointureDomaineExtension.DefaultIfEmpty()
join compteBaseAll in AccountBases on domaineExtension.New_AccountId equals compteBaseAll.AccountId into jointureCompteBase
from compteBase in jointureCompteBase.DefaultIfEmpty()
join compteExtensionAll in AccountExtensionBases on compteBase.AccountId equals compteExtensionAll.AccountId into jointureCompteExtension
from compteExtension in jointureCompteExtension.DefaultIfEmpty()
select rapportBase)
which generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
LEFT OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
LEFT OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
LEFT OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
LEFT OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
LEFT OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
LEFT OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
LEFT OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
But I want to generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
FULL OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
FULL OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
FULL OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
FULL OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
FULL OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
FULL OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
FULL OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
In other word, i want to generate full outer join for this query and not just left.
someone know how to do this in a simple way ?
thanks
There isn't a Full Outer Join in Linq. You have to do two left joins and concat them together. Here's some pseudocode that looks like linq:
var foj =
(from l in left
join r in right on l.Id equals r.Id into g
from r in g.DefaultIfEmpty()
select new { l, r })
.Concat
(from r in right
join l in left on r.Id equals l.Id into g
from l in g.DefaultIfEmpty()
where l == null
select new { l, r });
Probably be better to push this logic into a stored procedure if you're planning on using Linq to Sql.
See this post from the VB Team:
http://blogs.msdn.com/vbteam/archive/2008/02/12/converting-sql-to-linq-part-9-full-outer-join-bill-horst.aspx
Looks a little verbose.
Otherwise, there may be a way to achieve what you want using Foreign Keys and the Union operator.
Sadly I saw that before but i wanted to be sure.
The thing we did is to do it in a stored procedure and access it via linqtosql method.
then we map table result, like linqtosql do, using grouping to obtain collection of enties link to another entity.
for example, if we have an account with some contacts link to it. We request all contact with for each of them the account with a full join and them group on account id to obtain list of contact of each.
It work very well but it would be better if linqtosql was able to generate full join....