I have following query to get data from multiple tables
SELECT TOP (100) PERCENT
dbo.tblProduct.PK AS PRODUCT_PK, dbo.tblProduct.PRODUCT_NAME,
dbo.tblProductSubComponent.SUB_COMPONENT_PK,
dbo.tblComponent.COMPONENT_NAME, dbo.tblSubComponent.SUB_COMPONENT_NAME
FROM
dbo.tblProductComponent
RIGHT OUTER JOIN
dbo.tblComponent
INNER JOIN
dbo.tblSubComponent
INNER JOIN
dbo.tblComponentSubComponent ON dbo.tblSubComponent.PK = dbo.tblComponentSubComponent.SUB_COMPONENT_PK
ON dbo.tblComponent.PK = dbo.tblComponentSubComponent.COMPONENT_PK
ON dbo.tblProductComponent.COMPONENT_PK = dbo.tblComponent.PK
LEFT OUTER JOIN
dbo.tblProductSubComponent ON dbo.tblSubComponent.PK = dbo.tblProductSubComponent.SUB_COMPONENT_PK
FULL OUTER JOIN
dbo.tblProduct ON dbo.tblProductSubComponent.PRODUCT_PK = dbo.tblProduct.PK AND dbo.tblProductComponent.PRODUCT_PK = dbo.tblProduct.PK
ORDER BY
PRODUCT_PK
The result is
and now I want to show data in gridview like this
Never Mind.. I solved it myself. Created a stored procedure to fetch data and stored it in table variable and then used alter column query to add my rows as column of temporary table.
After creating the temporary table, I looped through each component and inserted record for each component and sub-component.
Related
New to SQL but I want to be able to optimize my query by bringing just the right amount of data. I am doing a left join on CS Rep Name and WE, which are two columns present in both tables. I find that if I don't bring in CS Rep Name and WE in the TECDR table, the query would error. Is there a workaround to this? Since it is a left join, I don't need redundant data.
SELECT *
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN (SELECT CS_Rep_Name,
Team_Leader,
Operations_Manager,
Tenure,
WE,
FileName
FROM Tish_Email_CSAT_Dump_Roster) AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name
AND TECD.WE = TECDR.WE
When you embed a SELECT inside a query in place of a table, the result of a select (projection) behave like a table visible only inside the query.
In your case, the join is the same as if there were a table called TECDR with the columns that you select. Hence, if you leave out some columns of Tish_Email_CSAT_Dump_Roster from your SELECT, these columns would not be available for joining or selection.
However, in your case this is unnecessary: all you need to do is joining to the underlying table, like this:
SELECT
TECD.*
, TECDR.Team_Leader
, TECDR.Operations_Manager
, TECDR.Tenure
, TECDR.FileName
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN Tish_Email_CSAT_Dump_Roster AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name AND TECD.WE = TECDR.WE
select
<place the columns you want here>
from
Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
Hope the following helps or else please share the query that errors:
select TECD.Column1, TECD.Column2, TECDR.Column1, TECDR.Column2
from Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
Topology db: Asset table -> Geometry table -> Valid_value table -> Unit_of_Measure
how can i start a query by referencing or starting from
Valid_value table and joining the rest of the tables. My objective here is to show a data where valid_value_descr is not empty. the Original query was (see the query) but the problem was- I have to show the null values of the dbo.GEOMETRY.GEOMETRIC_VALUE. If I used the query below it doesn't show the null values... I have to start from valid value table then connecting the rest. How can i do this..
SELECT dbo.ASSET.OID,
dbo.ASSET.ASSET_ID,
dbo.ASSET.ASSET_NAME,
dbo.GEOMETRY.GEOMETRIC_VALUE,
dbo.VALID_VALUE.VALID_VALUE_DESCR
FROM dbo.ASSET
INNER JOIN dbo.GEOMETRY
ON dbo.ASSET.OID = dbo.GEOMETRY.ASSET_OID
INNER JOIN dbo.VALID_VALUE
ON dbo.GEOMETRY.GEOMETRIC_TYPE_OID = dbo.VALID_VALUE.OID
INNER JOIN dbo.UNIT_OF_MEASURE
ON dbo.VALID_VALUE.UNIT_OF_MEASURE_OID = dbo.UNIT_OF_MEASURE.OID
If I have read your problem right, you have more values in your VALID_VALUE table than will match up to values in your ASSET or GEOMETRY tables, which means that if you want to start from ASSET, go through GEOMETRY, and still see all values in VALID_VALUE, you need to use a RIGHT JOIN to get to the table that you want to see all the rows of:
SELECT dbo.ASSET.OID,
dbo.ASSET.ASSET_ID,
dbo.ASSET.ASSET_NAME,
dbo.GEOMETRY.GEOMETRIC_VALUE,
dbo.VALID_VALUE.VALID_VALUE_DESCR
FROM dbo.ASSET
RIGHT JOIN dbo.GEOMETRY
ON dbo.ASSET.OID = dbo.GEOMETRY.ASSET_OID
RIGHT JOIN dbo.VALID_VALUE
ON dbo.GEOMETRY.GEOMETRIC_TYPE_OID = dbo.VALID_VALUE.OID
LEFT JOIN dbo.UNIT_OF_MEASURE
ON dbo.VALID_VALUE.UNIT_OF_MEASURE_OID = dbo.UNIT_OF_MEASURE.OID
(The final LEFT JOIN may need to be changed back to an INNER JOIN, as it appears that the purpose of that join is to restrict results since none of its columns are in the result set.)
This query will give you all of the rows in VALID_VALUE, with everything else joined onto it as though you had started there and LEFT JOINed everything on.
I am new to SQL and want to compare two tables using inner join and then use the result to compare it with the third table and so on. Also the third table does not share the same column name as compared in the first two.
Below is the SQL statement to compare two tables.
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Now whatever is returned I want to use that and do an inner join with a third table which does not have a column WORKLOAD_ITEM_ID. So the question is how to save and compare the result?
Please Help!
You can follow this to hold records in temp table
INSERT INTO #WORKLOAD_ITEM_TEMP
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Then with your third table made a join with this #WORKLOAD_ITEM_TEMP table.
Now you have to decide on which column need to put inner join. Either add a common column between output result set and third table or use just cross join.
I think you need to reevaluate your Database structure and make efficient tables relationship.
I have a legacy DB. A stored procedure SC_Total_Alerts_SP runs and fetches data every week. This has been going on for years. Now the requirements are such that I need to get a few extra columns. These columns are not part of the legacy DB. I know the changes that I need to do to the stored procedure, but my question is that how can I make sure that I do not lose any legacy data.
Query:
DECLARE #my_Date DATETIME = GETDATE()
SELECT
#my_Date AS Date,
Title_9691DD10_7211_C835_E3E7_6B38AF8B8104 AS Alert,
Priority_B930B964_A1C4_0B5A_B2D1_BFBE9ECDC794 AS Priority,
SDUA.DisplayName AS Name
FROM
[ServiceManager].[dbo].[MT_System$WorkItem$Incident] WII
LEFT OUTER JOIN
(SELECT *
FROM [ServiceManager].[dbo].Relationship
WHERE RelatiONshipTypeId = '15E577A3-6BF9-6713-4EAC-BA5A5B7C4722') ATU ON ATU.SourceEntityId = WII.BaseManagedEntityId
LEFT OUTER JOIN
[ServiceManager].[dbo].MT_System$Domain$User SDUA ON SDUA.BaseManagedEntityId = ATU.TargetEntityId
INNER JOIN
(SELECT *
FROM [ServiceManager].[dbo].LocalizedText
WHERE ElementName LIKE '%IncidentSTATUSEnum%'
AND LanguageCode = 'ENU'
AND DisplayStringId IS NOT NULL) LTS ON LTS.LTStringId = WII.Status_785407A9_729D_3A74_A383_575DB0CD50ED
WHERE
LTS.ElementName LIKE '%Resolved'
AND SDUA.DisplayName IS NOT NULL
AND CreatedDate_6258638D_B885_AB3C_E316_D00782B8F688 > DateAdd(dd, -7, #my_Date)
The easy way is to put the original query in a WITH statement, then use that derived table and left outer join the new data to it to get the new columns.
How can I add the results from a query by parameter to another table using a stored procedure?
Here is my select query text:
SELECT
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
FROM
dbo.PortfolioH
LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY
dbo.PortfolioH.Epic,
dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING
(dbo.PortfolioH.AlertRatings = N'yes')
I want to append to table AlertEmails where dbo.PortfolioH.Epic = #Epic
any help greatly appreciated.
Just use the standard INSERT.....SELECT construct, which takes the data to be inserted from a query, something like this:
INSERT INTO dbo.AlertsEmails (Epic, AlertRatings, Email, UserN, AlertType)
SELECT dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings,
dbo.UserAccounts.Email, dbo.PortfolioH.UserN, 1 As AlertType
FROM dbo.PortfolioH LEFT OUTER JOIN
dbo.UserAccounts ON dbo.PortfolioH.UserN = dbo.UserAccounts.UserN
GROUP BY dbo.PortfolioH.Epic, dbo.PortfolioH.AlertRatings, dbo.UserAccounts.Email,
dbo.PortfolioH.UserN
HAVING (dbo.PortfolioH.AlertRatings = N'yes') ;
It just runs the query, but instead of returning a result set, inserts the resulting rows into the table. Note that I've added the AlertType column in the INSERT part and added a fixed value in the SELECT part, as they must match exactly.