I need, if possible, a T-SQL query that will return the values in a specific format from the columns.
I have two tables:
The first table is PrimeClosureInformation table
[This is dbo.PrimeClosureInformation table]
And the second table is dbo.Event:
I did join with two table to get All the data for them Where This condition
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
but it returns the result like this:
Here is the result i want to get it
The format I need it for every PrimeClosureInformationPK record all events comments in one record or in one line like this example
PrimeClosureInformationPK Invoiced Remarks EventCommentFirst EventCreationDateTimeFirst EventCommentSecond EventCreationDateTimeSecond
And so on
This is the join I did it with the image for the result
this is the result of join i was did
SELECT TOP 1000
dbo.PrimeClosureInformation.PrimeClosureInformationPK,
dbo.PrimeClosureInformation.Invoiced,
dbo.PrimeClosureInformation.Remarks,
dbo.Event.EventComment,
dbo.Event.EventCreationDateTime,
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
if I understand it correclty all values of one PK should be display in one column, than you can try this:
SELECT CONCAT(dbo.PrimeClosureInformation.PrimeClosureInformationPK, '|' , dbo.PrimeClosureInformation.Invoiced, '', dbo.PrimeClosureInformation.Remarks, '|'
dbo.Event.EventComment, '|'
dbo.Event.EventCreationDateTime
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
Related
I'm trying to do a join statement for an inventory report of sorts but I am not sure what I am missing.
So I tried doing the reverse of my join statement and some columns remain omitted. I'm just not sure what I should add or change in the code.
My tables look something like this:
PRODUCT_TBL: productID|productDescription|stockQuantity
PRODUCT_SUPPLIER_TBL: supplierID|productID|vendorPartID|productCost|purchased Quantity
select PRODUCT_SUPPLIER_TBL.productID,PRODUCT_SUPPLIER_TBL.vendorPartID,PRODUCT_SUPPLIER_TBL.productCost
from PRODUCT_SUPPLIER_TBL
inner join PRODUCT_TBL on PRODUCT_SUPPLIER_TBL.productID = PRODUCT_TBL.productID
order by productCost desc
I expected one other column aside from productID to appear in the results but what I got only has information from the product_supplier_tbl and the productID from product_supplier_tbl and product_tbl.
You include any columns - from any of the tables you have joined together - in the SELECT list to return it in your query results.
select PRODUCT_SUPPLIER_TBL.productID
,PRODUCT_SUPPLIER_TBL.vendorPartID
,PRODUCT_SUPPLIER_TBL.productCost
,PRODUCT_TBL.product_description --adding a column to the SELECT list
from PRODUCT_SUPPLIER_TBL
inner join PRODUCT_TBL on PRODUCT_SUPPLIER_TBL.productID = PRODUCT_TBL.productID
order by productCost desc
I have two views that I am trying to join. I am joining on three elements, date, case number and surgeon id number. Each should only have one match for the previous case out value, but I am getting multiple rows after my left join.
Here is my code:
CREATE VIEW [dbo].[OR]
AS
SELECT DISTINCT
[ID].*,
[BYSURG].[PREV_PAT_OUT] AS PrevPtOut
FROM
[dbo].[OR_LOG_INDEXED] [ID]
LEFT JOIN
[DBO].[OR_CASE_NUM] BYSURG ON [ID].[SURG_DT] = [BYSURG].[SURG_DT]
AND [ID].[SURGEON_ID] = [BYSURG].[SURGEON_ID]
AND [ID].[CASE_NUM_BY_ROOM] = [BYSURG].[CASE_NUM_BY_ROOM_ADJ]
Any insights are much appreciated.
Thanks!
M
Replace your select block with one that retrieves all columns:
SELECT
*
FROM
[dbo].[OR_LOG_INDEXED] [ID]
LEFT JOIN
[DBO].[OR_CASE_NUM] BYSURG ON [ID].[SURG_DT] = [BYSURG].[SURG_DT]
AND [ID].[SURGEON_ID] = [BYSURG].[SURGEON_ID]
AND [ID].[CASE_NUM_BY_ROOM] = [BYSURG].[CASE_NUM_BY_ROOM_ADJ]
Run it and look at your "duplicate" rows - something about them will no longer be a duplicate - perhaps you've forgotten to include some other criteria in your where clause
Putting DISTINCT in the select block is not the answer - find out what data element about the "duplicate" rows is different and then filter out the rows you don't want
I have a query which has a column (PROCESS) which was obtained using a concat function. Now i need to lookup this column in a column which is on another table (Table2) and then return a value from the same table (table 2).
Query:
My Current output will look like this.
I have a reference Table like this.
I need to lookup "Process" (Query Result) in "Type" (the reference Table) and return "Description" (Ref table) in "Process Column".
Final Output should look like this
I'm unable to figure out how to modify my query to do this. pls help.
You need a join. Join your reference table to your other query based on the concatenated value and you can display the description you are looking for.
FROM #Source As s) D INNER JOIN [Reference Table] AS rt ON d.Process = rt.type
You need an INNER JOIN between Source and Reference tables for your nested query with the JOIN condition as in the following :
SELECT ... -- all columns of your queries outer part
(
SELECT s.Date, s.Station, s.worktype, s.tasktype, description as process, ....
FROM source s
INNER JOIN reference r on ( concat(s.worktype,s.tasktype) = r.type )
) D
GROUP BY D.date, D.station, D.worktype, D.accountno;
SQL Fiddle Demo
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.
This is the table I'm working with:
I would like to identify only the ReviewIDs that have duplicate deduction IDs for different parameters.
For example, in the image above, ReviewID 114 has two different parameter IDs, but both records have the same deduction ID.
For my purposes, this record (ReviewID 114) has an error. There should not be two or more unique parameter IDs that have the same deduction ID for a single ReviewID.
I would like write a query to identify these types of records, but my SQL skills aren't there yet. Help?
Thanks!
Update 1: I'm using TSQL (SQL Server 2008) if that helps
Update 2: The output that I'm looking for would be the same as the image above, minus any records that do not match the criteria I've described.
Cheers!
SELECT * FROM table t1 INNER JOIN (
SELECT review_id, deduction_id FROM table
GROUP BY review_id, deduction_id
HAVING COUNT(parameter_id) > 1
) t2 ON t1.review_id = t2.review_id AND t1.deduction_id = t2.deduction_id;
http://www.sqlfiddle.com/#!3/d858f/3
If it is possible to have exact duplicates and that is ok, you can modify the HAVING clause to COUNT(DISTINCT parameter_id).
Select ReviewID, deduction_ID from Table
Group By ReviewID, deduction_ID
Having count(ReviewID) > 1
http://www.sqlfiddle.com/#!3/6e113/3 has an example
If I understand the criteria: For each combination of ReviewID and deduction_id you can have only one parameter_id and you want a query that produces a result without the ReviewIDs that break those rules (rather than identifying those rows that do). This will do that:
;WITH review_errors AS (
SELECT ReviewID
FROM test
GROUP BY ReviewID,deduction_ID
HAVING COUNT(DISTINCT parameter_id) > 1
)
SELECT t.*
FROM test t
LEFT JOIN review_errors r
ON t.ReviewID = r.ReviewID
WHERE r.ReviewID IS NULL
To explain: review_errors is a common table expression (think of it as a named sub-query that doesn't clutter up the main query). It selects the ReviewIDs that break the criteria. When you left join on it, it selects all rows from the left table regardless of whether they match the right table and only the rows from the right table that match the left table. Rows that do not match will have nulls in the columns for the right-hand table. By specifying WHERE r.ReviewID IS NULL you eliminate the rows from the left hand table that match the right hand table.
SQL Fiddle