how to find modified objects for specific databases? - tsql

How did i get the below changes for the specific databases Adventureworksdw,NorthWind as a single result set.
-- Modified Tables
SELECT NAME, TYPE, TYPE_DESC, CREATE_DATE, MODIFY_DATE FROM SYS.OBJECTS
WHERE TYPE ='U' ORDER BY MODIFY_DATE DESC

You can use below query.
SELECT
'Adventureworksdw' AS SourceDatabase,
NAME,
TYPE,
TYPE_DESC,
CREATE_DATE,
MODIFY_DATE
FROM Adventureworksdw.SYS.OBJECTS
WHERE
TYPE ='U'
UNION ALL
SELECT
'NorthWind ' AS SourceDatabase,
NAME,
TYPE,
TYPE_DESC,
CREATE_DATE,
MODIFY_DATE
FROM NorthWind.SYS.OBJECTS
WHERE
TYPE ='U'
ORDER BY MODIFY_DATE DESC

Related

Using a parameter for the function to call

Within PostgresSQL how can I define a parameter as a function, i.e.
SELECT MAX(value) FROM table;
SELECT MIN(value) FROM table;
SELECT AVG(value) FROM table;
where the function is a parameter, something like the following:
SELECT $1(value) FROM table;
You cannot do that. Use multiple separate queries, and choose the one to execute. If you have to do this within SQL, you can also do the choosing depending on a parameter:
SELECT (CASE $1::text
WHEN 'max' THEN (SELECT MAX(value) FROM table)
WHEN 'min' THEN (SELECT MIN(value) FROM table)
WHEN 'avg' THEN (SELECT AVG(value) FROM table)
END);

add Exists operator in a union sql query

I have two tables that am using union to combine the result-set of them my problem here is Each SELECT statement within UNION must have the same number of columns and data types, I don't have the same number of columns so am creating null columns
select
d.deal_id as order_id,
EXISTS(select * from table1 c where d.user_id = c.user_id) as IsUser, --this returns boolean value
from table 1 c
union
select
cast(o.id as varchar) as order_id,
coalesce('No_user'::text,'0'::text) as IsUser, --i get an error :UNION types boolean and character varying cannot be matched
from table2 o
how can I create a null column in table2 that matches the boolean data type of the table1
how can I create a null column in table2 that matches the boolean data type of the table1
By putting NULL into the SELECT list of the second query.
Generally (in SQL) the datatype for the column is dictated by the first query in the union set and other queries must match. You don't need column aliases for subsequent queries either, but they may help make a query more readable:
select
d.deal_id as order_id,
EXISTS(select * from table1 c where d.user_id = c.user_id) as IsUser, --this returns boolean value
from table 1 c
union
select
cast(o.id as varchar) as order_id,
null --IsUser
from table2 o
If you have a case where the type of the column in the first query is different to what you want in the output, you cast the first column:
select 1::boolean as boolcol
UNION
select true
This will ensure that the column is boolean, rather than giving a "integer and bool cannot be matched". Remember also that, should you need to, NULL can be cast to a type, e.g. select null::boolean as bolcol

How to make the number of columns same using 'Union' in postgresql?

Here is the code:
SELECT 0 customer_id, 0 store_id, '' first_name, '' last_name,
'' email, 0 address_id, activebool, create_date, last_update, 0 active, * FROM country
UNION ALL
SELECT *, 0 country_id, '' country, last_update FROM customer
I don't know how to do that for certain data types like boolean type and date/time type like date and timestamp. I'm new to postgresql. So can anyone tell me what values should I put for the data types I mentioned? Sorry for my english.
I hope this can help.
I Tried those on psql they work fine:
For timestamp
SELECT '-infinity' AS "test" UNION SELECT '0001-01-01 00:00:00'::timestamp;
For boolean
SELECT null AS "test" UNION SELECT 0::boolean;
SELECT 'true' AS "test" UNION SELECT 0::boolean;

Count the number of child categories in hierarchical postgresql structure

I have a table with categories (id, name, parent_id) and that parent_id is an id of an upper category.
I get my category tree via the following query:
WITH RECURSIVE query AS (
SELECT id, name, parent_id
FROM category
UNION all
SELECT s2.id, s2.name, s2.parent_id
FROM category s2 join query on query.id= s2.parent_id
)
SELECT * from query order by parent_id;
How can I count the child categories in my table?
I believe tracking the depth of your recursive-ocity will give you the answer. I almost always add a depth and path into my recursive CTE's because it's a quick add and they offer a wealth of helpful information in the result set:
WITH RECURSIVE query AS (
SELECT id, name, parent_id, 1 as depth, CAST(id || '>' || parent_id as VARCHAR(500)) as path
FROM category
UNION all
SELECT s2.id, s2.name, s2.parent_id, query.depth + 1, query.path || '>' || s2.parent_id
FROM category s2 join query on query.id= s2.parent_id
)
SELECT * from query order by depth, path;
That may not get at exactly what you are needing, but you can COUNT() in your final query too:
SELECT parent_id, count(*) from query GROUP BY parent_id;
Which should give you the count of records from your recursive result set for each parent_id, which theoretically, should be the count of children, right?

T-SQL Replacing Nulls with Empty String

I have the following union query:
select
'type1' as type,
id,
add_date
from Table A
where type = type1
union all
select
'type2' as type,
id,
'' as add_date
from Table B
Since add_date isn't applicable to type 2, it is providing 1900-01-01 00:00:00.000 for any records returned. I could change the statement from '' as add_date to NULL as add_date, but the user is asking if I can remove the null from the report and leave the result as an empty string where applicable. What is the best way to accomplish this? Thanks.!
You can use null and cast the result as a string with the ISNULL function applied:
SELECT [type], id, ISNULL(CAST(add_date as VARCHAR), '') AS add_date
FROM (
SELECT 'type1' as [type]
,id
,add_date
FROM TableA
UNION
SELECT 'type2' as [type]
,id
,null as add_date
FROM TableB
) inside_query
This will allow you to provide blanks instead of 1/1/1900 or NULL keyword. You can also change the CAST to a CONVERT if you need to provide specific date formatting.
try this:
select
'type1' as type,
id,
CAST(add_date AS VARCHAR) as add_date
from Table A
where type = type1
union all
select
'type2' as type,
id,
'' as add_date
from Table B