I have 2 tables as follows (sample data shown):
TableName: A
ID Type
1 Bug
2 Requirement
3 Task
4 Specification
5 Bug
6 Specification
7 Production Issue
8 Production Issue
9 Bug
10 Task
Tablename: B
ID RelatedID
1 2
1 7
5 8
5 4
9 6
9 10
I want to fetch all the bugs that have atleast one related production issue or bugs that have no related production issue.
Expected output will be as shown below (since these are the bugs with at least one related production issue)
output
1
5
Aliases are the way to go here
SELECT pri.Type AS 'Primary Type', rel.Type AS 'Related Type'
FROM A AS pri
INNER JOIN B ON B.ID = pri.ID
INNER JOIN A AS rel ON B.RelatedID = rel.ID
WHERE pri.Type = 'Bug' AND rel.Type = 'Production Issue;
Related
This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
Closed 5 months ago.
I am trying to split a column in my table as separate table in SQL Server. I currently have a table with data. I have a table with available courses for a program separated by semi column. I need to split this and keep it as two different tables as I need to search based on a course details.
Current table (program_details)
program_code
course_available
start_date
active
1
AB;01;ERl;KL09;324
18-Sep-2022
1
2
ER;02;EJl;DL09;414
14-Sep-2022
1
3
JK;CD;201;PL08;201
28-Sep-2022
1
4
FV;50;301;GL07;234
18-Oct-2022
1
I need to split this as two table for better searchability with course codes, I can write program for this or is there any easy way to achieve this using any functions of SQL Server?
Table program_details:
program_code
start_date
active
1
18-Sep-2022
1
2
14-Sep-2022
1
3
28-Sep-2022
1
4
18-Oct-2022
1
Table program_course_mapping:
mapping_id
pgm_code
course_id
1
1
AB
2
1
01
3
1
ER1
4
1
KL09
5
1
324
6
2
ER
7
2
02
8
2
EJ1
9
2
DL09
10
2
414
If you have SQL Server 2016,
SELECT program_code
,A.value FROM #T T
CROSS APPLY(SELECT * FROM STRING_SPLIT(T.Course_available,';')) A
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed last year.
I have an SQL database with below rules:
There are items with same name but different versions.
Status of item production is stored inside a column State.
The structure of table log inside test database is like below:
id
Name
Ver
State
1
A
1
OK
2
B
1
Failed
3
C
1
OK
4
D
1
OK
5
A
2
OK
6
B
2
OK
7
C
2
Failed
8
D
2
OK
9
A
3
Failed
According to our production rules, the last version made from an item should have state OK (not Failed). So I want a query to grab list of unacceptable items with below constraints:
Name of items which their last version state is 'Failed'
In my sample data, the query should return A and C since Max version of A is 3 but its state is 'Failed' and max version of C is 2 but its state is Failed.
B and D are OK since their last version is 2 and the state of the version 2 for these items is OK.
How can I write query to bring list of items which their last version is failed (and not OK)?
Ah, yes, super rusty. Yet, you can definitely do this with a subquery:
SELECT mytable.name, mytable.ver, mytable.state
from
(SELECT NAME, VER
FROM TABLE GROUP BY NAME
HAVING VER = max(VER)) subq
INNER JOIN table on subq.name = mytable.name and subq.ver = mytable.ver
WHERE mytable.STATE = 'failed'
This is not a performant answer.
I am working on row level security in my database. I have two tables. Row based security is implemented on data_table and only returns rows that the user can see.
data_table:
data_id name role
-----------------------------
1 test USER
2 another ADMIN
3 yep USER
type_table:
type_id name
-----------------
1 this
2 is
3 a
4 type
EXECUTE AS USER = 'USER';
SELECT * FROM data_table;
returns rows 1 and 3 only. If you execute as ADMIN, all of the rows are returned. This is working properly in my database.
However, my issue is my bridging table.
data_type_table:
data_type_id data_id type_id
1 1 2
2 1 3
3 2 1
4 2 2
5 3 1
6 3 4
As of right now
EXECUTE AS USER = 'USER';
SELECT COUNT(data_type_id) FROM data_type_table;
returns 6 because it's looking at all 6 rows in the table. I'm trying to set it up in such a way that user USER will only see rows in data_type_table which are referencing rows where data_table.role = 'USER' (this means that the select count query would return 4). What would be the simplest way to implement something like this?
My data_table will more than likely contain hundreds of thousands of rows. Efficiency could become a problem here.
In SQL Server 2012, I am tring to recreate a detailed sales transaction record from two tables that have historical summary information but can't seem to limit the records based on an customer start date. (Actually there are 3 tables, one with customer item categories and % of sales by category, but I'm not having trouble with that part of the cross join). Any help would be appreciated.
Imagine two table:
Customer
ID Customername Sales_Monthly Date_start
1 Acme $80,000.00 1/15/2012
2 Universal $50,000.00 1/3/2013
3 SuperMart $12,000.00 4/14/2013
Calendar
ID Date
1 1 /31/2014
2 2 /28/2014
3 3 /31/2014
4 4 /30/2014
5 5 /30/2014
6 6 /30/2014
7 7 /30/2014
8 8 /30/2014
9 9 /30/2014
10 10/30/2014
11 11/30/2014
12 12/30/2014
A simple cross join:
SELECT Calendar.Date, Customer.ID, Customer.Customername, Customer.Sales_2013
FROM Calendar, Customer
produces 36 entries as you'd expect (3 customers x 12 months)
However, I only want to produce entries 28 entries, where [Calendar.Date] > [Customer.Date_start]
I can't seem to find WHERE CLAUSE and any join type or subquery that will limit my records based on the Customer.Date_start field. Any suggestions on this?
If you're joining on a field in a cross join, it's no longer a cross join. Assuming your customer data in the question is incorrect, and you meant it to be 2014 on all the customer records, you can do the join like this.
SELECT *
FROM Customer a
JOIN Calendar b ON b.Date > a.Date_start
This produces 33 rows (12 for customer 1, 12 for customer 2, and 9 for customer 3, not 28 like you're expecting), but hopefully my answer will point you in the right direction.
Here's the scenario:
I have 3 tables in a SQL Server 2008 database - SERVERS, InstalledPatches and Patchlist.
The SERVERS table has list of servers. InstalledPatches has list of servers and patches installed on them. Patchlist has list of all patches that SHOULD be installed on each server. All patches in PATCHLIST should be ideally installed on all servers in SERVERS table. I am trying to find the patches that are missing.
Sample data:
SERVERS
SERVERID SERVERNAME
-----------------------
1 ABC
.. ..
1500 XYZ
INSTALLEDPATCHES:
SERVERID PATCHID
-----------------
1 1
1 2
2 1
.. ..
1500 1
1500 2
PATCHLIST:
PATCHID PATCHNUMBER
---------------------
1 aaa
2 bbb
3 ccc
4 ddd
.. ..
15 ZZZ
Final report should indicate missing patches:
SERVERID MissingPATCHID
-------------------------
1 3
1 4
1 1500
2 3
2 4
2 1500
..
I have tried to use below query, but cant find all missing patches for each server.
SELECT
A.*
FROM
INSTALLEDPATCHES A
RIGHT OUTER JOIN
PATCHLIST B ON A.PATCHID = B.PATCHID
WHERE
A.PATCHID IS NULL
Any help would be really appreciated.
Thanks.
What about something like?
select s.SERVERID,
pl.PATCHID MissingPATCHID
from SERVERS s
cross join PATCHLIST pl
where not exists (select SERVERID,
PATCHID
from INSTALLEDPATCHES ip
where ip.SERVERID = s.SERVERID
and ip.PATCHID = pl.PATCHID)
I just created this SQLFiddle demo.
Try my query. It works now.
select s.serverid, p1.patchid as MissingPatchID
from [servers] as s
left join patchlist as p1
on 1=1
left join installedpatches as p2
on s.serverid = p2.serverid
and p1.patchid = p2.patchid
where p2.patchid is null