Represent one to many relationship on one report and get the many-table in a row instead of col - sql

My user wants a report which rows and columns of many-table repeats in a single row until all rows of many-table are finished.
what is the best way to write query for that?
i hope could get what i want
______________________________________________________________________________________
|table(1) | table(2) |
|---------------|--------------------|--------------------|------|--------------------|
|table(1) (row1)|table(2 of 1)(row 1)|table(2 of 1)(row 2)| .... |table(N of 1)(row X)|
|---------------|--------------------|--------------------|------|--------------------|
|table(1) (row2)|table(2 of 2)(row 1)|table(2 of 2)(row 2)| .... |table(N of 2)(row y)|
|---------------|--------------------|--------------------|------|--------------------|
|table(1) (row3)|table(2 of 3)(row 1)|table(2 of 3)(row 2)| .... |table(N of 3)(row Z)|
|---------------|--------------------|--------------------|------|--------------------|
...
...
...

These are the tools you need:
Dynamic SQL
nested queries
rownum function
case when
If possible, I would load the data from the database to your front end and change the representation there.

Related

Microsoft SQL Server generates two select queries and puts data in separate columns

I am looking for a query that separate the data with the condition WHERE in the same output but in separates columns.
Example: I have the table Product_2:
I have two separates queries (to separate the products by Produt_Tag):
SELECT
Product_Mark AS "PIT-10_Product_Mark",
Product_Model AS "PIT-10_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-10';
SELECT
Product_Mark AS "PIT-11_Product_Mark",
Product_Model AS "PIT-11_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-11';
And I get this output:
But I need the output to be like this:
Can someone tell me how I need to modify my query to have the four columns in the same table/ output?
Thank you
I forgot to tell that in the data I Have the “Porduct_Mark” that only appears one time. (in reality the data in “Product_Mark” is the name of the place where the instrument is located and one place can have one or two instruments “Product_Model”. At the end I’m looking for the result show in the image here below. I tried to use LEFT JOIN but that don’t work.
here is the new table "Product_2"
Result that I'm looking for:
Luis Ardila
I am assuming Product_PK is the primary key for the table and the repeated value 1002 shown in the question is a mistake. Considering this assumption, you can get the result set using self join as below.
SELECT pa.Product_Mark AS "PIT-10_Product_Mark", pa.Product_Model AS "PIT-10_Product_Model",
pb.Product_Mark AS "PIT-11_Product_Mark", pb.Product_Model AS "PIT-11_Product_Model"
FROM Product_2 pa
INNER JOIN Product_2 pb
ON pa.Product_Mark = pb.Product_Mark
WHERE pa.product_pk != pb.product_pk
and pa.Product_Tag = 'PIT-10'
and pb.Product_Tag = 'PIT-11';
verified same in https://dbfiddle.uk/NiOO8zc1

SQL One to many relationships, duplicate/results being returned as unique rows

I have a database and some of the tables have one to many relationships. How do I eliminate the results being returned as its own unique row?
For instance I have a initiative table and a initiative can have many funding requirements. When I perform an inner join I'm getting the results but it looks like the rows are duplicating to output a unique value from the funding table.
From the results, it should be like this
Row 3,4,5 should be in one row listing the results with the the funding required
Description | Acad_priority_1 | Acad_priority_2 | beginning_fiscal_year |
Develop... | false | true | 2018/2019 |
| 2018/2019 |
| 2019/2020
Can you please steer me in the right direction or show me how the SQL should be structured to achieve this?
SQL:
SELECT plan_master.plan_id,
plan_master.date_submitted,
plan_master.filename,
initiative_master.plan_id,
initiative_master.NAME,
initiative_master.acad_priority_1,
funding.initiative_id,
funding.beginning_fiscal_year
FROM plan_master
JOIN initiative_master
ON plan_master.plan_id = initiative_master.plan_id
JOIN funding
ON initiative_master.initiative_id = funding.initiative_id
ORDER BY Filename
|plan_id|date_submitted|filename|plan_id|NAME|acad_priority_1|initiative_id|begginning_fiscal_year|
|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|2018-12-03|1.txt|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|Space Utilization framework|false|8CCE0311-0E3C-467D-B675-04817A473056|2018/2019
|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|2018-12-03|1.txt|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|Space Utilization framework|false|8CCE0311-0E3C-467D-B675-04817A473056|2019/2020
|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|2018-12-03|1.txt|16F44FFE-5434-4E52-9D9A-F45C0A49D8E2|Space Utilization framework|false|8CCE0311-0E3C-467D-B675-04817A473056|2020/2021
The 2 beginning fiscal year values cannot be combined into a single row unless you want to concatenate them with commas (or another separator), or write a function to show the value as a range, for example 2018-2020. You can however get rid of the 4th record using distinct or using the below mentioned over/partition by clauses.
If you don't mind, can you run the following query and provide the results that will help me identify the duplication issue:
SELECT plan_master.plan_id,
plan_master.date_submitted,
plan_master.filename,
plan_master.department,
plan_master.last_name,
plan_master.first_name,
plan_master.email,
plan_master.mission_statement,
plan_master.vision_statement,
plan_master.goals_objectives,
initiative_master.plan_id,
initiative_master.NAME,
initiative_master.description,
initiative_master.acad_priority_1,
initiative_master.acad_priority_2,
initiative_master.acad_priority_3,
initiative_master.acad_priority_4,
initiative_master.acad_priority_5,
initiative_master.acad_priority_6,
initiative_master.operational_sustainability,
initiative_master.people_plan,
funding.initiative_id,
funding.beginning_fiscal_year
FROM plan_master
JOIN initiative_master
ON plan_master.plan_id = initiative_master.plan_id
JOIN funding
ON initiative_master.initiative_id = funding.initiative_id
ORDER BY Filename
Once you get to the cause, you can either use a better join clause (multiple conditions), add a where clause, or use the OVER clause in conjunction with the PARTITION BY clause to filter the data based on a ROW_NUMBER().
A simple join returns a cartesion product. If one table has 2 rows and another has 3, then there will be 6 rows of data. Need to do distinct on the data. You can do this:
SELECT plan.date_submitted,
plan.filename,
plan.department,
plan.last_name,
plan.first_name,
plan.email,
plan.mission_statement,
plan.vision_statement,
plan.goals_objectives,
initiative.Name,
initiative.description,
initiative.acad_priority_1,
initiative.acad_priority_2,
initiative.acad_priority_3,
initiative.acad_priority_4,
initiative.acad_priority_5,
initiative.acad_priority_6
FROM plan_master as plan
inner join (select distinct init.plan_id, init.NAME,
init.description,
init.acad_priority_1,
init.acad_priority_2,
init.acad_priority_3,
init.acad_priority_4,
init.acad_priority_5,
init.acad_priority_6,
init.operational_sustainability,
init.people_plan,
funding.beginning_fiscal_year from initiative_master as init
join funding on funding.initiative_id = init.initiative_id ) as initiative
ON plan.plan_id = initiative.plan_id
ORDER BY Filename

SQL Query not searching data if record contains zero

I have two tables and with column paperNo and some data regarding that paper. I am trying to search all data based on paper no. from both the tables. I have successfully written the query and it is retrieving the data successfully. but I have noticed that. If my paperNo contains zero(0) then the query is not searching for that data. And for the non zero contains paperNo it is retrieving the same record twice.
I don't understand what is going wrong. tried every thing.
Here is my Query .-
SELECT PaperDate.paperNo,
PaperDate.RAW_PAPER,
PaperDate.EDGE_SEALED,
PaperDate.HYDRO_120,
PaperDate.HYDRO_350,
PaperDate.CATALYST_1ST,
PaperDate.CATALYST_2ND,
PaperDate.SIC_350,
tblThicknessPaperDate.rawThickness,
tblThicknessPaperDate.catThickness,
tblThicknessPaperDate.sicThickness,
tblThicknessPaperDate.rejectedThickness
FROM tblThicknessPaperDate
FULL OUTER JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (tblThicknessPaperDate.paperNo = #paperNo)
I would try:
FROM tblThicknessPaperDate
RIGHT JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (PaperDate.paperNo = #paperNo)
The two changes are: swapping to a right join so even if a record isn't in tblThicknessPaperDate we will still see the record in PaperDate. The other change is to use PapterDate.paperNo in the where clause. Since tblThicknessPaperDate.paperNo could be null we don't want to use that in the where if we can avoid it.
SELECT PaperDate.paperNo,
PaperDate.RAW_PAPER,
PaperDate.EDGE_SEALED,
PaperDate.HYDRO_120,
PaperDate.HYDRO_350,
PaperDate.CATALYST_1ST,
PaperDate.CATALYST_2ND,
PaperDate.SIC_350,
tblThicknessPaperDate.rawThickness,
tblThicknessPaperDate.catThickness,
tblThicknessPaperDate.sicThickness,
tblThicknessPaperDate.rejectedThickness
FROM tblThicknessPaperDate
FULL OUTER JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (tblThicknessPaperDate.paperNo = #papNo | PaperDate.paperNo = #paperNo)

SQL query that return all table fields after check if the value exists on 3 tables

I have 3 tables without any relationship between them in a SQLite database.
I have to return all the fields about one of the 3 tables where i have a conditions with something like this:
(table1.field1="something" and table1.field2="something") OR (table2.filed1="something" and table2.field2="something") ...
So I want to know what table have the field with "something" and return the other fields of that table with that information.
If the question was not clear at all, I'll try to do a picture with the problem.
Edit:
I tried something like this:
SELECT idpontosturisticos,idrestauracao,idalojamento from Alojamento,pontosturisticos ,restauracao WHERE (alojamento.latitude=41.158764 AND alojamento.longitude=-7.784906 AND alojamento.nome='Restaurante1') OR (pontosturisticos.latitude=41.158764 AND pontosturisticos.longitude=-7.784906 AND pontosturisticos.nome='Restaurante1') OR (restauracao.latitude=41.158764 AND restauracao.longitude=-7.784906 AND restauracao.nome='Restaurante1')
I think you just want a UNION.
Like this:
SELECT idpontasturisticos, otherfielda1, otherfielda2, otherfieda3 from pontosturisticos WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1')
UNION
SELECT idrestauracao, otherfieldb1, otherfieldb2, otherfiedb3 from restauracao WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1')
UNION
SELECT idalojamento, otherfieldc1, otherfieldc2, otherfiedc3 from alojamento WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1')
You just need to make sure that you retrieve the same number of fields from each of the three subqueries. The fields don't have to be the same name, but they will all come down in the same column. The column names will be the names from the first subquery (in this case, idpontasturistocs, otherfieda1, otherfielda2, otherfielda3)

How do I INSERT info from one table into its LINK table?

I'm trying to build an asp.net application, where I have three tables.
tblRoute
tblHalte
tblHalteRoute
I want to insert a routeID into tblHalteRoute and for every routeID multiple halteID's
TblRoute has the needed fields (routeID(1) &
allHaltes[halteID1,halteID2,halteID3])
--> so Every route has multiple Haltes that I can access by parsing the allHaltes field and using the IDS.
What I want to do is update tblHalteRoute like this:
tblRoute.routeID(1), tblRoute.allHaltes(halteID1)
tblRoute.routeID(1), tblRoute.allHaltes(halteID2)
tblRoute.routeID(1), tblRoute.allHaltes(halteID3)
inserting these values into the tblHalteRoute .. I'm really not sure on where to look or how to start, I tried using SUBquerys with an Insert before a select, but no success.
If I understand you correctly, you're trying to pivot data - eg
Route 1 Halte 1
Route 1 Halte 2
Route 1 Halte 3
-->
Route 1 Halte 1 Halte 2 Halte 3
I'm also assuming you mean LINQ not LINK (as in the data access mechanism)
If this is the case, you could write a function that does something like:
Dim MyNewRow = New tblHalteRoute
Dim Routes = MyRoutes.Where(function(x) x.RouteId = 1).OrderBy(function(x) x.RouteId)
MyNewRow.Route1 = Routes.First.id
MyNewRow.Route2 = Routes.Skip(1).First.id
MyNewRow.Route£ = Routes.Skip(2).First.id
It's worth nnoting that you're making an assumption that your Route table will have more Id fields than there are Ids = This may be a valid assumption for your app but even if it is, it feels dangerous to me - I'd suggest you store this properly (ie keep it relational) and then just For Each each row as required - then there's no limit to the number of Ids available.