I have 3 tables. Namely Job_Master,Print_details,Paper_Details.
The Structure of the table is as follows:
Job_Master:
jobno jobname amount
1 A 100
2 B 200
3 C 300
Print_Details id being the PK and jobno being FK
id jobno color
1 1 Cyan
2 1 Red
3 2 Black
4 3 Black
5 3 Green
Paper Details id being the PK and jobno being FK
id jobno Type
1 1 Art Paper
2 1 Photo Paper
3 2 Art Paper
4 3 Copier
5 3 Glossy Paper
I want a write a query in SQL server or perform Dataset operations in ASP.net so as to display the below resultset in the grid view:
Desired Resultset:
jobno jobname printDetails paperDetails amount
1 A CYAN,RED Art Paper,Photo Paper 100
2 B Black Art Paper 200
3 C Black,Green Copier,Glossy Paper 300
is this possible using dataset operations in ASP.net or in a SQL server query. I am using SQL server 2008 R2 as my database and the fromt end has been designed using ASP.net3.5.
Any help on this is much appreciated.Thanks in advance.
Try this
SELECT DISTINCT a.Jobno, a.jobname, COALESCE(b.color + ', ', '') + b.color,COALESCE(c.type + ', ', '') + c.type
FROM job_master a left JOIN cprint_details b ON a.jobno=b.jobno
left join paper_details c on a.jobno=c.jobno
ORDER BY a.jobno
Read here for more info LINK
Related
I got a table like this
a b c
-- -- --
1 1 10
2 1 0
3 1 0
4 4 20
5 4 0
6 4 0
The b column 'points' to 'a', a bit like if a is the parent.
c was computed. Now I need to propagate the parent c value to their children.
The result would be
a b c
-- -- --
1 1 10
2 1 10
3 1 10
4 4 20
5 4 20
6 4 20
I can't make an UPDATE/SELECT combo that works
So far I got a SELECT that procuce the c column I'd like to get
select t1.c from t t1 join t t2 on t1.a=t2.b;
c
----------
10
10
10
20
20
20
But I dunno how to stuff that into c
Thanx in advance
Cheers, phi
You have to look up the value with a correlated subquery:
UPDATE t
SET c = (SELECT c
FROM t AS parent
WHERE parent.a = t.b)
WHERE c = 0;
I finnally found a way to copy back my initial 'temp' SELECT JOIN to table 't'. Something like this
create temp table u as select t1.c from t t1 join t t2 on t1.a=t2.b;
update t set c=(select * from u where rowid=t.rowid);
I'd like to know how the 2 solutions, yours with 1 query UPDATE correlated SELECT, and mine that is 2 queries and 1 correlated query each, compare perf wise. Mine seems more heavier, and less aesthetic, yet regarding perf I wonder.
On the Algo side, yours take care not to copy the parent data, only copy child data, mine copy parent on itself, but that's a nop, yet consuming some cycles :)
Cheers, Phi
I have 3 tables.
person
equipment
relation between person and equipment
I have a page with a search button and a checklistbox that binds with the equipment. I want the user to be able to select one or more check list and then show people associated with the selected equipment.
-------------------------
table1
p_famil p_code
ali 1
reza 2
mohamad 3
kazem 4
---------------------------------
table 2
e_code e_title
1 home
2 car
3 job
--------------------------------
table 3
er_eq_code er_per_code
2 1
3 1
1 2
1 4
I have two tables customer & home in a local database for a ipad application.
I have a SQL query
SELECT C.CustomerID AS 'CID', H.CustomerID AS 'HID', H.lotNumber AS 'lotNumber' FROM CustomerInformation C LEFT JOIN HomeInformation AS H ON ( H.CustomerID = C.CustomerID )
It returns the proper amount of records, and display my address data correctly. However, all the data in the HomeInformation table is null
CustomerID FirstName HomeID CusomterID lotNumber
1 Josh 1 1 73824
2 Smith 2 2 54353
3 Chris 3 3 75342
4 Bob 4 4 42342
5 John 5 5 24342
I have tried to use INNER join, Cross Join, and Outer Join, but no records are returned.
I hava this table in data base
http://i.stack.imgur.com/r7ECj.jpg
I want to group data in this form
------------ Group 1 -------------
1.FoodGroupName : Milk
a.FoodSubGroup: Milk(type1)
1.food 1
2.food 2
3.food 3
4.food 4
b.FoodSubGroup: Milk(type2)
1.food 1
2.food 2
3.food 3
4.food 4
c.FoodSubGroup: Milk(type3)
1.food 1
2.food 2
3.food 3
4.food 4
--------- Group 2 ------------
2.FoodGroupName : Meat
a.FoodSubGroup: Meat(type1)
1.food 1
2.food 2
3.food 3
4.food 4
b.FoodSubGroup: Meat(type2)
1.food 1
2.food 2
3.food 3
4.food 4
c.FoodSubGroup: Meat(type3)
1.food 1
2.food 2
3.food 3
4.food 4
You could use a query like this:
SELECT fg.FoodGroupName, fsg.FoodSubGroupName, f.FoodName
FROM FoodGroups fg
INNER JOIN FoodSubGroups fsg
ON fg.FoodGroupId = fsg.FoodGroupId
INNER JOIN Foods f
ON fsg.FoodSubGroupId = f.FoodSubGroupId
ORDER BY fg.FoodGroupName, fsg.FoodSubGroupName, f.FoodName
to retrieve data and then output results as you please...
There is several ways to do that kind of thing :
1) you write a single query that will combines the 3 table (using joins) and return a single dataset (like Marco suggested).
eg :
Group SubGroup Food
--------------------------------
Meat Meat1 Food 1
Meat Meat1 Food 2
Meat Meat1 Food 3
Meat Meat2 Food 1
Milk Milk1 Food 1
Milk Milk2 Food 1
Then by using C# code, you group them.
You can do this using very few lines of code (and efficiently) by using GroupBy() linq lambda expression. Have also a look at ToLookUp() extension.
If you are not familiar with them, check this page :
http://code.msdn.microsoft.com/LINQ-to-DataSets-Grouping-c62703ea
In the end you should get a collection of objects that allows you to do the following:
var groups = ...
foreach(var group in groups)
{
//do something with group
foreach(var subGroup in group.SubGroups)
{
//do something with subGroup
foreach(var food in subGroup.Foods)
{
//do something with food
}
}
}
Then it is very easy to fill a treeview or present data using nested repeaters.
2) you write independent queries that will query each table separately. In the end, you get 3 datasets.
Group
-------
1 Meat
2 Milk
SubGroup
---------
1 1 Meat1
2 1 Meat2
3 2 Milk1
4 2 Milk2
Food
----------
1 1 Food 1
2 1 Food 2
3 1 Food 3
4 2 Food 1
5 3 Food 1
6 4 Food 1
Then you use can Group Join linq operator to group them.
In that case, C# will do the joins for you, not sql.
Check here : http://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9/description#groupjoin
In the end you will get same result at 1). Both have advantages / disavantages.
I currently have 2 tables as follows within my database:
Table: SampleProducts
SampleProductsId (PK) Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Table: SampleProductsBoms
SampleProductsBomId (PK) ParentId (FK) ChildId (FK) Quantity
1 1 2 3
2 2 3 4
3 4 6 2
ParentId and ChildId both reference SampleProductsId
In English so I can ensure that we are all on the same page:
Product A is made up of 3 of B
Product B is made up of 4 of C
Product D is made up of 2 of F
I would like to create a Stored Procedure / LinQ statement or something which I can use in my MVC 3 c# Web Application which will give me the following table structure / object to use...
Example:
Recursive Query to find the components of B
ProductId Name Quantity
3 C 4
6 F 2
This could go quite deep, so I really do need recursion!
CTE is helpfull for recursing as require in your problem statement check the link
Common Table Expression
or i think following query may also solve your purpose
select components.SampleProductId as productid,components.Name as Name,Quantity
from SampleProductsBOM bom
inner join SampleProducts products
on products.ParentId=bom.ParentId
inner join SampleProducts components
on components.SampleProductId=bom.ChildId
where products.Name='B'