ALTER Procedure [dbo].[GetItemsTypesByProduct]
(
#ProductID INT
)
As
Begin
Select distinct iMap.ProductId, iTyp.Id ItemTypeId, iTyp.ItemType, iTyp.SortOrder
from itemMapping iMap
inner join items itm on itm.itemid = imap.itemid
inner join itemType iTyp on iTyp.id = itm.ItemTypeId
Where ProductId = #ProductID
UNION
Select distinct rMap.ProductId, iTyp.Id, iTyp.ItemType, iTyp.SortOrder
from ReaderMapping rMap
inner join itemType iTyp on iTyp.id = 25
Where ProductId = #ProductID
UNION
Select distinct aMap.ProductId, iTyp.Id, iTyp.ItemType, iTyp.SortOrder
from AssessmentMapping aMap
inner join itemType iTyp on iTyp.id = 24
Where ProductId = #ProductID
order by productid, iTyp.SortOrder
Select distinct aTyp.AssessmentTypeId, AssessmentTypeName, MappingLevel
from eds_quiz..AssessmentType aTyp
inner join eds_quiz..Assessments asm on (asm.AssessmentTypeId = aTyp.AssessmentTypeId)
inner join AssessmentMapping aMap on (aMap.AssessmentId = asm.AssessmentId)
Where ProductId = #ProductID
Select Id, TypeName from eds_edusmart..lessontype
Where Id NOT IN (1, 2, 4)
End
Related
I have the following queries and have trouble putting them together:
DECLARE #Value1 INT = 3
DECLARE #Value2 INT = 6
UPDATE TableA SET
Column1 = B.NewValue,
FROM TableA A INNER JOIN TableB B ON A.NumberId = B.NumberId AND
AND A.Type = #Value1
UPDATE TableA SET
Column2 = B.NewValue,
FROM TableA A INNER JOIN TableB B ON A.NumberId = B.NumberId AND
AND A.Type = #Value2
My goal is to have one query with a join that updates the columns depending on the values in the join.
This I just an example (in my case there are more columns and therefore more queries) but overall I want to have as few queries as possible (in this example: one query instead of two)
DECLARE #Value1 INT = 3
DECLARE #Value2 INT = 6
UPDATE TableA SET
Column1 = B.NewValue, --if the join joins on #Value1
Column2 = B.NewValue, --if the join joins on #Value2
FROM TableA A INNER JOIN TableB B ON A.NumberId = B.NumberId AND
AND A.Type = B.#Value1/#Value2
Is this possible (using a sub query for example)?
You can try using CASE EXPRESSION
UPDATE TableA SET
Column1 = CASE WHEN A.Type = #Value1 THEN B.NewValue
ELSE A.Column1 END,
Column2 = CASE WHEN A.Type = #Value2 THEN B.NewValue
ELSE A.Column2 END
FROM TableA A INNER JOIN TableB B ON A.NumberId = B.NumberId AND
AND A.Type IN (#Value1, #Value2)
How can I convert the below query in Oracle to Hive?
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP FROM TABLE1 A, TABLE2 B
WHERE A.EMP_NO = 1234 AND B.EMP_CURR =
(SELECT MIN(EMP_CURR) FROM TABLE2 WHERE EMP_NO = A.EMP_NO AND
LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP AND EMP_STATUS_CODE <> 'P')
Use dense_rank() to get rows with minimum EMP_CURR:
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP
FROM TABLE1 A
INNER JOIN (select B.*,
dense_rank() over(partition by B.EMP_NO, B.LOGIN_TIMESTAMP order by B.EMP_CURR) rn
from TABLE2 B where EMP_STATUS_CODE <> 'P'
) B
on B.EMP_NO = A.EMP_NO and B.LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP and B.rn=1
where B.rn=1 and A.EMP_NO = 1234;
I'm trying to make a query that have a lot of subquerys (4 subquerys), but for the subquerys I need to take a value of the query, can anyone help me? This is the query:
SELECT DISTINCT tab1.value1,
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 WHERE tab4.value = "value1"), 0) AS v1,
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 WHERE tab4.value = "value2"), 0) AS v2
What I need to do is that in the subquerys, make an INNER JOIN with that value1 the subquery make the adding but just with the values that are of the value1, because right now the subquery make the add of all the values located in the table that are equal with "value1" or "value2", I have tryed to make a subquery like these:
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 INNER JOIN tab5 ON tab2.id_tab5 = tab5.id_tab5 INNER JOIN tab1 ON tab5.id_tab1 = tab1.id_tab1 INNER JOIN tab1 ON tab1.value1 = tab1.value1 WHERE tab4.value = "value1"), 0) AS v1
But obviously it didn't function, I also have tried to use an allias for the value1 but it says that it doesn't exist, anyone have an idea?
Im going to take a stab at it... It looks like you are trying to join the four tables by _id and aggregate a sum based on the child table's (tab4) value.
In the below example I used three tables, but the idea is the same. If this doesnt meet your needs perhaps you can repurpose this example to clearly define your requirements:
declare #tab1 table (i int primary key, value varchar(10));
insert into #tab1
select 1, 'one' union all select 2, 'two'
declare #tab2 table (i int primary key, quantity varchar(10));
insert into #tab2
select 1, 10 union all
select 2, 20
declare #tab3 table (i int primary key, value varchar(10));
insert into #tab3
select 1, 'value1' union all
select 2, 'value2'
select t1.value,
sum(case when t3.value = 'value1' then t2.quantity else 0 end),
sum(case when t3.value = 'value2' then t2.quantity else 0 end)
from #tab1 t1
join #tab2 t2 on t1.i = t2.i
join #tab3 t3 on t2.i = t3.i
group
by t1.value;
I have a database where the tables are:
Table1:
Table1Id int
Table2:
Table2Id int
ForeignKeyToTable1 int
LookupTable:
Table2Id
Table3Id
Table3:
Table3Id int
Table3Field varchar
I want to:
select table1.* from table1
inner join table2 on table1.Table1Id = ForeignKeyToTable1
inner join LookupTable on LookupTable.Table2Id = table2.Table2Id
inner join Table3 on table3.Table3Id = LookupTable.Table3Id
where table3.Table3Field ='qwerty'
How can this be achieved in Linq?
var query =
from table1 in db.Table1
join table2 in db.Table2 on new { Table1Id = table1.Table1Id } equals new { Table1Id = table2.ForeignKeyToTable1 }
join lookuptable in db.LookupTable on table2.Table2Id equals lookuptable.Table2Id
join table3 in db.Table3 on lookuptable.Table3Id equals table3.Table3Id
where
table3.Table3Field == "qwerty"
select new {
table1.Table1Id
};
I believe something like this will go
var query = from t1 in context.Table1
join t2 in context.Table2
on t1.Table1Id equals t2.ForeignKeyToTable1
join lt in context.LookupTable
on t2.Table2Id equals lt.Table2Id
join t3 in context.Table3
on lt.Table3Id equals t3.Table3Id
where t3.Table3Field == "qwerty"
select t1;
Can something like this be written in LINQ to SQL:
UPDATE Table1
SET CustomerName = t2.Name
FROM Table1 t1 JOIN Table2 t2
ON t1.TableID = t2.TableID
var result = from t1 in context.Table1
join t2 in context.Table2 on t1.TableID = t2.TableID
select new {t1, t2};
result.ToList().ForEach(i => i.t1.CustomerName = i.t2.Name);
context.SubmitChanges();