Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table of projects which are all tagged with a lookup for the associated client and want to show the data like this:
Client 1
Project 1
Project 2
Project 3
Client 2
Project 1
Project 2
Client 3
Project 1
Project 2
Project 3
That way I get the projects back from my table and have them "grouped" by client.
What do you call this action? Is it nested groups or something like that?
SQL queries cannot supply a nested result as you have laid out1. Instead, your result set has to be flat, repeating the Client multiple times, once for each Project. Really in your SQL query, it is just a matter of ordering to sort the like Client values together: ORDER BY client
Client 1 Project 1
Client 1 Project 2
Client 1 Project 3
Client 2 Project 1
Client 2 Project 2
Client 2 Project 3
In your application display logic then, you only print the new Client when the value changes from the previous one.
Pseudocode:
# Start with empty last_client
last_client = ""
loop_over_rows
if last_client is not equal to current_row->client
print new row->client
# Print all projects
print row->project
# Store the current client to compare on next loop
last_client = row->client
endloop
1 It can be done in SQL with a lot of crazy string manipulation and UNION, but this is a matter of presentation and really belongs in the application presentation logic rather than the SQL.
If your RDBMS, which is not specified in your tags, support recursive SQL you can approximate the layout you are trying to accomplish.
The following SQL works against Teradata's data dictionary tables. The Hierarchy column is offset based on depth of the database in the ownership chain. From there you should be able to take the concept and apply it to your situation.
WITH RECURSIVE cte (DatabaseName, Path, Parent, Level) AS
(
SELECT TRIM(DatabaseName)
,DatabaseName(VARCHAR(600))
,TRIM(DatabaseName)
,0 (BYTEINT)
FROM DBC.Databases d
WHERE DatabaseName = 'DBC'
UNION ALL
SELECT TRIM(d.DatabaseName)
,cte.Path || '.' || TRIM(d.DatabaseName)
,cte.Path
,Level + 1
FROM DBC.Databases d
,cte
WHERE d.OwnerName = cte.DatabaseName
AND d.DatabaseName <> d.OwnerName
AND Level < 20
)
SELECT Level
, SUBSTRING(CAST('' AS CHAR(60)) FROM 1 FOR Level * 2) || DatabaseName AS Hierarchy
, Path
, Parent
FROM cte
ORDER BY Path;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Based on the table structure below, I want to count a value only once per column value.
Person ID Email Template Email Event
1 A DELIVER
1 A OPEN
1 A OPEN
1 B CLICK
2 A DELIVER
2 A OPEN
3 A DELIVER
3 C DELIVER
What I would like to is to get number of email delivered, clicked and opened for each person. I am looking to get this number for each person not necessarily for templates. All email templates are delivered just once to a person but they can open and click that template multiple times. I would like to not include the repeat opens and click in this aggregation.
Something like this? I'm not sure what your table is called
select t.person_id,
sum(case when t.email_event_type = 'DELIVER' then 1 else 0) deliver_count,
sum(case when t.email_event_type = 'OPEN' then 1 else 0) open_count,
sum(case when t.email_event_type = 'CLICK' then 1 else 0) click_count
from <your_table_name> t
group by t.person_id
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a column of values where I need to take the average of the two previous values and the current value and display as a new column. I'm using MS Access 2013 and am more familier using SQL code rather than the SQL Query Wizard. So, if you could provide the code I'd appreciate it.
I've read on other threads that talked about a Lag function, but I believe Access does not allow that. Also, I've seen similar questions answered with subqueries, but I'm not familiar enough with those yet.
Below is what I'm looking for. Given column A (1,2,3,4,5) how do I make B (0,0,2,3,4)?
A | B
----------
1 | 0
2 | 0
3 | 2 = (1+2+3)/3
4 | 3 = (2+3+4)/3
5 | 4 = (3+4+5)/3
Utilize a self join to the same table for when the value of A is BETWEEN a - 2 and A.
SELECT
t.A
,SUM(pre.A) / 3 as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A
Note that this will actually give you 0,1,2,3,4. If you want 0,0,2,3,4 you will have to count the preceding rows to determine if there are three and if not make it 0 such as:
SELECT
t.A
,IIF(COUNT(pre.A) < 3, 0, SUM(pre.A) / 3) as B
FROM
TableName t
LEFT JOIN TableName pre
ON pre.A >= (t.A - 2)
AND pre.A <= t.A
GROUP BY
t.A
In a project we have several SSIS packages (around 200), all the package names are stored in a control table. We need to create a master package which can run all the 200 packages.
Since the max concurrent executable setting was set to 8. So planning to create 8 execute package tasks in a container and was thinking of generating the connection string(Execute package task- File connection String) dynamically using the package names stored in the table.
The control table is in the below format
Id PackageName
---------------
1 Package1
2 Package2
Ideas on how should be implemented helps.
I covered this pattern on https://stackoverflow.com/a/34868545/181965 but you're looking for a package that looks something like this
A sequence container that contains everything that one of those 8 discrete buckets of work would require. In your case, a Variable for
CurrentPackage String
rsObject Object
ContainerId Int32
The containerId will be the values 0 through 7 (since you have 8 buckets of work). As outlined in the other answer, we must scope the variables to the Sequence Container. The default in 2012+ is to create them at the Control Flow level, whereas 2005/2008 would create them at the level of the selected object.
Set up
I created a table and loaded it with 200 rows
CREATE TABLE dbo.so_35415549
(
id int IDENTITY(1,1) NOT NULL
, PackageName sysname
);
INSERT INTO
dbo.so_35415549
(
PackageName
)
SELECT TOP 200
'Package' + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS varchar(3))
FROM
sys.all_columns AS AC;
Get My Bucket's data
The modulus, modulo, mod whatever you call it operator is our friend here. The mod operator will return the remainder after division. e.g. 10 mod 3 is 1 because 3*3 + 1 = 10
In your case, you'll be modding via 8 so you know the remainder will be bounded between 0 and 7.
SQL Server implements the mod operator as % and you can test the correctness via the following query
SELECT
S.id
, S.PackageName
, S.id % 8 AS ModValue
FROM
dbo.so_35415549 AS S
ORDER BY
1;
Sample output
id PackageName ModValue
1 Package1 1
2 Package2 2
3 Package3 3
4 Package4 4
5 Package5 5
6 Package6 6
7 Package7 7
8 Package8 0
9 Package9 1
10 Package10 2
...
199 Package199 7
200 Package200 0
SQL Get Work List
Using the above query as a template, we will use the following query. Notice the ? in there. That is the placeholder for an Execute SQL Tasks parameterization for an OLE DB Connection Manager.
SELECT
S.PackageName
FROM
dbo.so_35415549 AS S
WHERE
S.id % 8 = ?
ORDER BY
1;
The Parameter we pass in will be #[User::ContainerId]
The Result Set option will be updated from None to Full ResultSet and we push the value into rsObject
FELC Shred Work List
This is a standard shredding of a recordset. We got our variable populated in the previous step so let's enumerate through the results. There will be one column in our result set and you will map that to User::CurrentPackageName
EPT Run Package
This is your Execute Package Task. Use the value of CurrentPackageName and you're set.
I have the following structure
construction
id
1
building-step
id construction_id step_id worker_id
1 1 1 1
1 1 2 2
step
id name
1 foundation
2 wall
3 roof
When a new construction is added using a template, building-steps are generated. After the initial configuration steps can be added or removed.
One of the requirements is to display the construction template as a string. The template is dynamic because you can add and remove steps.
In this case the template name is "foundation+wall". If I add another building-step the template name will be "foundation+wall+roof".
My question is, how can this be done in optimized way?
Is the SQL structure the problem for this dynamic complexity?
In Postgres specifically you can use the string_agg function, which concats rows of strings.
In you example, you could try:
select string_agg(nm,'+') from(
select st.name as nm
from building-step bs
inner join
step st
on bs.step_id=st.id
where bs.construction_id=1
order by bs ASC
); --done by hand, may have small errors
It will support edits in both tables anytime, and will perform nicely.
I have the following tables, the groups table which contains hierarchically ordered groups and group_member which stores which groups a user belongs to.
groups
---------
id
parent_id
name
group_member
---------
id
group_id
user_id
ID PARENT_ID NAME
---------------------------
1 NULL Cerebra
2 1 CATS
3 2 CATS 2.0
4 1 Cerepedia
5 4 Cerepedia 2.0
6 1 CMS
ID GROUP_ID USER_ID
---------------------------
1 1 3
2 1 4
3 1 5
4 2 7
5 2 6
6 4 6
7 5 12
8 4 9
9 1 10
I want to retrieve the visible groups for a given user. That it is to say groups a user belongs to and children of these groups. For example, with the above data:
USER VISIBLE_GROUPS
9 4, 5
3 1,2,4,5,6
12 5
I am getting these values using recursion and several database queries. But I would like to know if it is possible to do this with a single SQL query to improve my app performance. I am using MySQL.
Two things come to mind:
1 - You can repeatedly outer-join the table to itself to recursively walk up your tree, as in:
SELECT *
FROM
MY_GROUPS MG1
,MY_GROUPS MG2
,MY_GROUPS MG3
,MY_GROUPS MG4
,MY_GROUPS MG5
,MY_GROUP_MEMBERS MGM
WHERE MG1.PARENT_ID = MG2.UNIQID (+)
AND MG1.UNIQID = MGM.GROUP_ID (+)
AND MG2.PARENT_ID = MG3.UNIQID (+)
AND MG3.PARENT_ID = MG4.UNIQID (+)
AND MG4.PARENT_ID = MG5.UNIQID (+)
AND MGM.USER_ID = 9
That's gonna give you results like this:
UNIQID PARENT_ID NAME UNIQID_1 PARENT_ID_1 NAME_1 UNIQID_2 PARENT_ID_2 NAME_2 UNIQID_3 PARENT_ID_3 NAME_3 UNIQID_4 PARENT_ID_4 NAME_4 UNIQID_5 GROUP_ID USER_ID
4 2 Cerepedia 2 1 CATS 1 null Cerebra null null null null null null 8 4 9
The limit here is that you must add a new join for each "level" you want to walk up the tree. If your tree has less than, say, 20 levels, then you could probably get away with it by creating a view that showed 20 levels from every user.
2 - The only other approach that I know of is to create a recursive database function, and call that from code. You'll still have some lookup overhead that way (i.e., your # of queries will still be equal to the # of levels you are walking on the tree), but overall it should be faster since it's all taking place within the database.
I'm not sure about MySql, but in Oracle, such a function would be similar to this one (you'll have to change the table and field names; I'm just copying something I did in the past):
CREATE OR REPLACE FUNCTION GoUpLevel(WO_ID INTEGER, UPLEVEL INTEGER) RETURN INTEGER
IS
BEGIN
DECLARE
iResult INTEGER;
iParent INTEGER;
BEGIN
IF UPLEVEL <= 0 THEN
iResult := WO_ID;
ELSE
SELECT PARENT_ID
INTO iParent
FROM WOTREE
WHERE ID = WO_ID;
iResult := GoUpLevel(iParent,UPLEVEL-1); --recursive
END;
RETURN iResult;
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
END GoUpLevel;
/
Joe Cleko's books "SQL for Smarties" and "Trees and Hierarchies in SQL for Smarties" describe methods that avoid recursion entirely, by using nested sets. That complicates the updating, but makes other queries (that would normally need recursion) comparatively straightforward. There are some examples in this article written by Joe back in 1996.
I don't think that this can be accomplished without using recursion. You can accomplish it with with a single stored procedure using mySQL, but recursion is not allowed in stored procedures by default. This article has information about how to enable recursion. I'm not certain about how much impact this would have on performance verses the multiple query approach. mySQL may do some optimization of stored procedures, but otherwise I would expect the performance to be similar.
Didn't know if you had a Users table, so I get the list via the User_ID's stored in the Group_Member table...
SELECT GroupUsers.User_ID,
(
SELECT
STUFF((SELECT ',' +
Cast(Group_ID As Varchar(10))
FROM Group_Member Member (nolock)
WHERE Member.User_ID=GroupUsers.User_ID
FOR XML PATH('')),1,1,'')
) As Groups
FROM (SELECT User_ID FROM Group_Member GROUP BY User_ID) GroupUsers
That returns:
User_ID Groups
3 1
4 1
5 1
6 2,4
7 2
9 4
10 1
12 5
Which seems right according to the data in your table. But doesn't match up with your expected value list (e.g. User 9 is only in one group in your table data but you show it in the results as belonging to two)
EDIT: Dang. Just noticed that you're using MySQL. My solution was for SQL Server. Sorry.
-- Kevin Fairchild
There was already similar question raised.
Here is my answer (a bit edited):
I am not sure I understand correctly your question, but this could work My take on trees in SQL.
Linked post described method of storing tree in database -- PostgreSQL in that case -- but the method is clear enough, so it can be adopted easily for any database.
With this method you can easy update all the nodes depend on modified node K with about N simple SELECTs queries where N is distance of K from root node.
Good Luck!
I don't remember which SO question I found the link under, but this article on sitepoint.com (second page) shows another way of storing hierarchical trees in a table that makes it easy to find all child nodes, or the path to the top, things like that. Good explanation with example code.
PS. Newish to StackOverflow, is the above ok as an answer, or should it really have been a comment on the question since it's just a pointer to a different solution (not exactly answering the question itself)?
There's no way to do this in the SQL standard, but you can usually find vendor-specific extensions, e.g., CONNECT BY in Oracle.
UPDATE: As the comments point out, this was added in SQL 99.