How can i get all childs row with entity commands? - sql-server

i used this SQL Command to get all child and childs of child users and i dont know how can i write it with entities?
WITH ParentUser AS (
SELECT *, 1 AS nthLevel
FROM [OnlinePage].[dbo].[Users] usr1
WHERE ID = #UserID
UNION ALL
SELECT usr2.*,
nthLevel + 1 AS nthLevel
FROM ParentUser pu INNER JOIN
[OnlinePage].[dbo].[Users] usr2 ON pu.ID = usr2.UserParentID
)
SELECT * FROM ParentUser WHERE 1 < nthLevel
and my users table is:
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Username] [nvarchar](50) NOT NULL,
[UserParentID] [int] NOT NULL
)
it`s something like this:
|--------------------------------------|
|ID |Username |UserParentID |
|-----|-----------|--------------------|
|1 |admin |0 |
|2 |reseler1 |1 |
|3 |user1 |1 |
|4 |reseler2 |2 |
|5 |user2 |4 |
|6 |user3 |5 |
|--------------------------------------|
and if i want to get all childs of reseler1 with upper sql command i change #UserID with 2 and my result is:
|--------------------------------------|
|ID |Username |UserParentID |
|-----|-----------|--------------------|
|2 |reseler1 |1 |
|3 |user1 |1 |
|4 |reseler2 |2 |
|5 |user2 |4 |
|6 |user3 |5 |
|--------------------------------------|
now i need entity model of this sql command,
i search it on google and i found something in stackoverflow(Querying child entities) like down code, but not work correctly and only get one level of childs:
var query = from m in dbSet.Where(x => x.ID == 1)
join s in dbSet
on m.ID equals s.UserParentID into masterSlaves
from ms in masterSlaves.DefaultIfEmpty()
select ms;

Entity framework and Linq-to-entities does not support hierarchical queries. You must use your SQL query and call it with dbSet.SqlQuery or dbContext.Database.SqlQuery. With EF 5 / .NET 4.5 and database first approach you would also be able to create SQL function wrapping your query and map it (so you would be able to use that function from Linq-to-entities query) but that is not available in earlier versions and in code first.

Related

How to : update column with sum column in same table in SQL Server 2014?

Before table data:
-----------------------
|ID |WORK|VALUE|TOTAL|
-----------------------
|ID1|WRITE |10 | |
|ID1|TYPE |5 | |
|ID2|READ |25 | |
|ID2|SCAN |30 | |
|ID3|PRINT |15 | |
|ID4|SETTING|20 | |
|ID5|REPAIR |5 | |
|ID5|MAINTE |25 | |
|ID5|MONITOR|20 | |
Total is sum value from same id
ID1 10+5
ID2 25+30
ID3 15
ID4 20
ID5 50
For now I use the insert method with create table data2 (ID,TOTAL)
INSERT INTO DATA2(DATA2.ID, DATA2.TOTAL)
SELECT DATA.ID, SUM (DATA.VALUE) AS TOTAL
FROM DATA
GROUP BY DATA.ID
Then I do SELECT JOIN FROM DATA2 AND DATA
After table data
-----------------------
|ID |WORK|VALUE|TOTAL|
-----------------------
|ID1|WRITE |10 |15 |
|ID1|TYPE |5 |15 |
|ID2|READ |25 |55 |
|ID2|SCAN |30 |55 |
|ID3|PRINT |15 |15 |
|ID4|SETTING|20 |20 |
|ID5|REPAIR |5 |50 |
|ID5|MAINTE |25 |50 |
|ID5|MONITOR|20 |50 |
To update the value total to table, you need to have the column..
alter table tblname
add total int
If this is not a one time approach,i would recommend creating a view like below
create view somename
as
select id,work,value,
sum(value) over (partition by id ) as total
from tabel
if you want to update table as one time excercise
;with cte
as
(select id,work,value,
sum(value) over (partition by id ) as total1
from tablename
)
update t
set t.total=c.total1
from cte c
join tablename t
on t.id=c.id
Another way to update the total.
UPDATE T1
SET T1.TOTAL=T2.TOTAL
FROM YOUT_TABLE T1
JOIN (
SELECT ID,SUM(VALUE) AS TOTAL
FROM YOUR_TABLE
GROUP BY ID
) T2 ON T1.ID=T2.ID
DEMO

Sum up delivered companies for every customer(ID)

I'm back in action ;) This time I have a pretty heavy task (I think).
Here's what I got:
|customerID ||company |compdel |Street |Code |Date 1 |Date 2 |
+-------------------------------+--------------------------------------+
|1 ||Example1 |DELExam1|ABC Rd.1|4025 |01.01.2015 |01.08.2015 |
|1 ||Example1 |DELExam1|ABC Rd.1|4025 |13.04.2015 |01.12.2015 |
|1 ||Example1 |DELExam2|DEL St.1|0212 |13.03.2015 |09.07.2015 |
|1 ||Example1 |DELExam3|REF Wy.1|9875 |26.05.2015 |16.09.2015 |
|2 ||Example2 |DELExam4|REG St.1|6754 |21.02.2015 |16.05.2015 |
|2 ||Example2 |DELExam5|HIO Wy.1|9999 |01.03.2015 |06.08.2015 |
|2 ||Example2 |DELExam5|HIO Wy.1|9999 |01.01.2015 |06.02.2015 |
I want to show for every customerID every delivered company (compdel) summed in one line with the earliest date in Date 1 and the newest Date in Date 2. To make it easier to understand, I want this result:
|customerID ||company |compdel |Street |Code |Date 1 |Date 2 |
+-------------------------------+--------------------------------------+
|1 ||Example1 |DELExam1|ABC Rd.1|4025 |01.01.2015 |01.12.2015 |
|1 ||Example1 |DELExam2|DEL St.1|0212 |13.03.2015 |09.07.2015 |
|1 ||Example1 |DELExam3|REF Wy.1|9875 |26.05.2015 |16.09.2015 |
|2 ||Example2 |DELExam4|REG St.1|6754 |21.02.2015 |16.05.2015 |
|2 ||Example2 |DELExam5|HIO Wy.1|9999 |01.01.2015 |06.08.2015 |
I tried it already with this select-Statement but it won´t work: I know, that this can only be a part of the answer....
SELECT *
FROM
(SELECT
customerID, company, compdel, Street, Code, Date 1, Date 2,
ROW_NUMBER() OVER(PARTITION BY compdel ORDER BY customerID) rn
FROM
table 1) as Y
WHERE
rn = 1
Use GROUP BY with distinct values (customerId, company etc.) and MIN and MAX for dates
SELECT CustomerId
, Company
, CompDel
, Street
, Code
, MIN(Date1) As EarliestDate1
, MAX(Date2) AS NewestDate2
FROM YourTable
GROUP BY CustomerId, Company, CompDel, Street, Code

How to update column with another column in the same table?

I have data like this :
+--+----------+--------+------+
|Id|class_name|class_id|medals|
+--+----------+--------+------+
|1 |7IPA1 |7 |3 |
|2 |7IPA2 |7 |2 |
|3 |7IPA3 |7 |5 |
|4 |8IPA1 |8 |1 |
|5 |8IPA2 |8 |7 |
|6 |8IPA3 |8 |3 |
+--+----------+--------+------+
I want data on class_id be 7IPA & 8IPA (4 first character from class_name).
You have to use substring function:
UPDATE MYTABLE SET CLASS_ID=SUBSTRING(CLASS_NAME,1,4)
Another way is using LEFT string function
Select LEFT(CLASS_NAME,4) from yourtable
Looks like you want a new column and not to update existing column, I will suggest you to create a computed column
alter table yourtable add new_class_id as (left(class_name,4)) persisted

How to get the count of each distinct value in Multiple columns and get the result in separate columns?

I need the following table to be queried to get the result given below the table.
Table:
----------------------------------
| Name | Age | slot |
|-------|--------|---------------|
|A |20 | 1 |
|B |30 | 2 |
|C |30 | 1 |
|D |20 | 1 |
|E |40 | 2 |
|F |40 | 3 |
|G |50 | 3 |
----------------------------------
Result:
-------------------------------------------
|Age |Age_Count |Slot |Slot_Count|
-------------------------------------------
|20 | 2 |1 |3 |
-------------------------------------------
|30 | 2 |2 |2 |
-------------------------------------------
|40 | 2 |3 |2 |
-------------------------------------------
|50 | 1 |
-----------------------
While searching stackoverflow i found this question for single column question and there is [this link for multiple columns] (get the count of each distinct value in "Multiple" columns) question. The answers from the second link (for the multiple coulmn's distinct count) is displayed under a single column and my requirement is i guess quite different from the answers posted there.
Thanks in advance
Your request is kind of odd. Are you sure you want that?
If so, this may help:
SET #x:=0,#y:=0,#m:=0,#n:=0;
SELECT
DISTINCT age,age_count, slot,slot_count
FROM (
SELECT
age, age_count, slot, slot_count
FROM (
SELECT
#x:=#x + 1 AS aid, age, COUNT(*) age_count
FROM
slots
GROUP BY age
) a
LEFT JOIN (
SELECT
#y:=#y + 1 AS sid, slot, COUNT(*) slot_count
FROM
slots
GROUP BY slot
) s ON a.aid = s.sid
UNION
SELECT
age, age_count, slot, slot_count
FROM (
SELECT
#m:=#m + 1 AS aid, slot, COUNT(*) slot_count
FROM
slots
GROUP BY slot
) a
LEFT JOIN (
SELECT
#n:=#n + 1 AS sid, age, COUNT(*) age_count
FROM
slots
GROUP BY age
) s ON a.aid = s.sid
) a
If you know for sure that you have more unique ages than unique slots , or opposite, you can get ride of messy union.

How to exclude rows for the same ID based on Category and Date

+--+-------+----------+---+
|ID|OrderID|OrderDate |Cat|
+--+-------+----------+---+
|1 |240904 |23/05/2013|63 |
+--+-------+----------+---+
|1 |338584 |11/12/2013|64 |
+--+-------+----------+---+
|1 |47309 |21/11/2011|64 |
+--+-------+----------+---+
|2 |175307 |23/11/2012|63 |
+--+-------+----------+---+
+--+-------+----------+---+
|2 |195307 |24/12/2012|64 |
+--+-------+----------+---+
+--+-------+----------+---+
|2 |175300 |23/11/2011|63 |
+--+-------+----------+---+
+--+-------+----------+---+
|5 |175307 |23/11/2012|63 |
+--+-------+----------+---+
+--+-------+----------+---+
|5 |215307 |15/1/2013|64 |
+--+-------+----------+---+
+--+-------+----------+---+
|7 |195307 |15/06/2012|63 |
+--+-------+----------+---+
I'm using SQL Server 2008 R2. I trying (struggling) to find a way to get more recent OrderDate for Cat 64 then Cat 63 for on the same ID all from the table that looks as those records separated by | above.
Try Like this
Select OrderID,OrderDate From
(
Select *,ROW_NUMBER() over(PARTITION by ID,CAT Order by OrderDate DESC) AS RN
From tbl
) AS Tmp
Where RN = 1
Working Fiddle

Resources