i want to write the dynamic script to get data from DBMS - sql-server

There are multiple records in db against single ID. I want to get only single value from duplicate data and single different value in next column against single ID. do i need to use the pivot or something else.
i am trying this on SQL server
select distinct (d.SetId), s.Description, sd.SourceValue, d.TargetValue, d.TargetEntityDetailCode,e.entitydisplayname,e.entitycode from P2P_ADRSet s
inner join P2P_ADRSourceDataMaster sd on sd.SourceCombinationId = s.SourceCombinationId
inner join P2P_ADRDataMaster d on d.SourceDataId = sd.SourceDataId and d.SetId = s.SetId
Inner join org_entitydetails e on e.Entitydetailcode = d.TargetEntityDetailCode
where sd.SourceValue in (249,66)
-- i want this source value which is 249 and 66 in different column. for now its showing in single column.

Related

Looking up values in multiple tables with multiple joins

How would you do a Select statement to lookup a value in a table and then use that value to lookup another value in another table which is then used to lookup a third value in a third table? I cannot do a join from my initial table as there is no common matching field to do the join on.
In my initial table I have an Instr_Id value which is the only possible way to eventually get the Legal_Id value but the table where the Legal_Id is stored does not have an Instr_Id value. In order to do this I need to look up multiple values from 2 other tables to eventually get to the INSTR_ID value.
Example:
I use INSTR_ID in tbl.ABC and join to INSTR_ID in tbl.DEF in order to get the Fin_Enty_Name value in tbl.DEF I then need to use this Fin_Enty_Name value to join to tbl.GHI to get the Fin_Enty_Id . I finally then need to use this Fin_Enty_Id to look up the LEGAL_ID in tbl.JKL so that I can show the LEGAL_ID for each INSTR_ID in my Select query results.
select
a.INSTR_ID,
d.LEGAL_IT
from tbl.ABC as A
Inner join tbl.DEF as B on A.INSTR_ID = B.INSTR_ID
It looks pretty much straight forward:
select a.instr_id, d.legal_id
from tbl.abc a
join tbl.def b
on a.instr_id = b.instr_id
join tbl.ghi c
on b.fin_enty_name = c.fin_enty_name
join tbl.jkl d
on c.fin_enty_id = d.fin_enty_id

How to get column value of join query in SSRS?

I have select join query for generating report in SSRS. Query is working fine in SQL Server but as I add same query as dataset in SSRS and try to get rows count using CountRows() function it always return 0 (Zero). I'm not getting where my query is going wrong.
SQL Query
SELECT PR.NAME
FROm innovator.PROJECT PR
INNER JOIN innovator.PROJECT_RISK LPR ON LPR.SOURCE_ID = Pr.ID
INNER JOIN innovator.RISK_MANAGEMENT LR ON LR.id = LPR.RELATED_ID
Inner join innovator.PROGRAM_PROJECT P ON PR.ID = P.RELATED_ID
Inner Join innovator.PROGRAM PP ON P.SOURCE_ID = PP.ID
WHERE pp.ID = #Id
Fetching total count using CountRows() for Textbox
=CountRows(Fields!NAME.Value, "DataSetRisk")
DataSetRisk is Dataset name and Name is column name of Project Table
Use the CountRows function. For example
=CountRows("MyDataset")
Example : =CountRows("DataSetRisk")
will give you the number of rows in MyDataSet.
Try something a little simpler: Count(Fields!NAME.Value) as a column. This assumes, of course, that field name actually is populated. If the column is in separate groups, it will provide a count for each group, otherwise it will count for the entire report.

Select tables that contain a specific value (Postgres)

in my DB there are more than 100 tables. Some of them have a column name "date".
I want to get all tables (table names) for a specific date.
so far I have been able to retrieve the table names that contain a date attribute:
SELECT pg_class.relname
FROM pg_class
INNER JOIN pg_attribute
ON pg_attribute.attrelid = pg_class.oid
WHERE pg_attribute.attname = 'date'
but I want to do somethin like that and it doesn´t work of course:
SELECT pg_class.relname
FROM pg_class
INNER JOIN pg_attribute
ON pg_attribute.attrelid = pg_class.oid
WHERE pg_attribute.attname = 'date'
AND date = '2014-12-05'
You can create a function that loops through the tables you've found and execute dynamically composed queries in it.
(Executing dynamic queries in PL/pgSQL)

Right way to use distinct in SQL Server

I am trying to retrieve some records based on the query
Select distinct
tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID,
tblSpecies.CommonName
from
tblAssessmentEcosystemCredit
left join
tblSpeciesVegTypeLink on tblAssessmentEcosystemCredit.VegTypeID = tblSpeciesVegTypeLink.VegTypeID
left join
tblSpecies on tblSpecies.SpeciesID = tblSpeciesVegTypeLink.SpeciesID
where
tblAssessmentEcosystemCredit.SpeciesTGValue < 1
The above query returns 17,000 records but when I remove tblSpecies.CommonName, it retrieves only 4200 (that's actually correct).
I have no idea how to distinct only tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID column and retrieve all other table columns in the query.
This query selects the different COMBINATION of AssessmentEcosystemCreditID and CommonName; if you want only one row per value of AssessmentEcosystemCreditID then you need to use a GROUP BY, as suggested by #JonasB; however, in that case, there could be several values of CommonName per value of AssessmentEcosystemCreditID , and so SQL requires you to specify WHICH one you want
Select tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID ,
max(tblSpecies.CommonName) as CommonName,
min(tblSpecies.CommonName) as CommonName2, -- so you can verify you only have one value
from tblAssessmentEcosystemCredit
left join tblSpeciesVegTypeLink
on tblAssessmentEcosystemCredit.VegTypeID = tblSpeciesVegTypeLink.VegTypeID
left join tblSpecies on tblSpecies.SpeciesID= tblSpeciesVegTypeLink.SpeciesID
where tblAssessmentEcosystemCredit.SpeciesTGValue <1
GROUP BY tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID
See this topic: mySQL select one column DISTINCT, with corresponding other columns
You probably have to deactivate ONLY_FULL_GROUP_BY, see http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by

SQL Same Column in one row

I have a lookup table that has a Name and an ID in it. Example:
ID NAME
-----------------------------------------------------------
5499EFC9-925C-4856-A8DC-ACDBB9D0035E CANCELLED
D1E31B18-1A98-4E1A-90DA-E6A3684AD5B0 31PR
The first record indicates and order status. The next indicates a service type.
In a query from an orders table I do the following:
INNER JOIN order.id = lut.Statusid
This returns the 'cancelled' name from my lookup table. I also need the service type in the same row. This is connected in the order table by the orders.serviceid How would I go about doing this?
It Cancelled doesnt connect to 31PR.
Orders connects to both. Orders has 2 fields in it called Servicetypeid and orderstatusid. That is how those 2 connect to the order. I need to return both names in the same order row.
I think many will tell you that having two different pieces of data in the same column violates first normal form. There is a reason why having one lookup table to rule them all is a bad idea. However, you can do something like the following:
Select ..
From order
Join lut
On lut.Id = order.StatusId
Left Join lut As l2
On l2.id = order.ServiceTypeId
If order.ServiceTypeId (or whatever the column is named) is not nullable, then you can use a Join (inner join) instead.
A lot of info left out, but here it goes:
SELECT orders.id, lut1.Name AS OrderStatus, lut2.Name AS ServiceType
FROM orders
INNER JOIN lut lut1 ON order.id = lut.Statusid
INNER JOIN lut lut2 ON order.serviceid = lut.Statusid

Resources