Complex SQL query Help Pls - sql

Well I have 2 Table named 'A' and 'B'
One of the tables(A) include many records in this records ı have a column holding 'SENDER_CONTACT_NUMBER' it can be too many invoice from same sender.
Other table Holding 'SENDER_CONTACT_NUMBER' and 'STATUS'(1 - is active )( 0 - is not active) so in this table 'SENDER_CONTACT_NUMBER' is unique and I want make sql query that Union 2 tables 'SENDER_CONTACT_NUMBER' and I want to create 2. column for each 'SENDER_CONTACT_NUMBER' And I want to take Counts in table A (I solve the first part but second is real problem to me cause B table 'SENDER_CONTACT_NUMBER' may not be in A table so in this case I couldn't show counts :(
TABLE A
INVOICE_TYPE_CODE varchar no
SENDER_CONTACT_NUMBER varchar no
SENDER_IDENTIFIER varchar no
SENDER_NAME varchar no
RECEIVER_CONTACT varchar no
RECEIVER_IDENTIFIER varchar no
RECEIVER_NAME varchar no
TABLE B
SENDER_CONTACT_NUMBER varchar no
URUN varchar no
BASTAR char no
BITTAR char no
STATUS smallint no
Result that I want to see
COLUMN 1
Union SENDER_CONTACT_NUMBER LIST BY STATUS=1
COLUMN 2
For each record in sender_contact_number at column 1 show counts in a if there is no record put 0
enter image description here

Assuming you want a count of the number of occurrences of each SENDER_CONTACT_NUMBER in table A, including records from B that have no corresponding record in A, then this should work:
SELECT B.SENDER_CONTACT_NUMER, COUNT(A.INVOICE_TYPE_CODE) AS Invoices
FROM B
LEFT JOIN A ON B.SENDER_CONTACT_NUMER = A.SENDER_CONTACT_NUMER
WHERE B.STATUS = 1 -- Only include active contacts

Related

SQL/T-SQL Substring LEFT or Right doesn't appear to resolve

I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);

Finding non-matching data in two tables as per columns and arranging column data as row by row

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

union table, change serial primary key, postgresql

Postgresql:
I have two tables 'abc' and 'xyz' in postgresql. Both tables have same 'id' columns which type is 'serial primary key;'.
abc table id column values are 1,2,3 and also xyz id column containing same values 1,2,3,4
I want to union both tables with 'union all' constraint. But I want to change 'xyz' id column values to next value of 'abc' id column last value as 1,2,3,4,5,6,7
select id from abc
union all
select id from xyz
|id|
1
2
3
1
2
3
4
my wanted resuls as
|id|
1
2
3
4
5
6
7
BETTER - Thanks to #CaiusJard
This should do it for you
select id FROM abc
UNION ALL select x.id + a.maxid FROM xyz x,
(SELECT MAX(id) as maxid from abc) a
ORDER BY id
For anyone who's doing something like this:
I had a similar problem to this, I had table A and table B which had two different serials. My solution was to create a new table C which was identical to table B except it had an "oldid" column, and the id column was set to use the same sequence as table A. I then inserted all the data from table B into table C (putting the id in the oldid field). Once I fixed the refernces to point to from the oldid to the (new)id I was able to drop the oldid column.
In my case I needed to fix the old relations, and needed it to remain unique in the future (but I don't care that the ids from table A HAVE to all be before those from table C). Depending on what your trying to accomplish, this approach may be useful.
If anyone is going to use this approach, strictly speaking, there should be a trigger to prevent someone from manually setting an id in one table to match another. You should also alter the sequence to be owned by NONE so it's not dropped with table A, if table A is ever dropped.

Insert data from one table to other using select statement and avoid duplicate data

Database: Oracle
I want to insert data from table 1 to table 2 but the catch is, primary key of table 2 is the combination of first 4 letters and last 4 numbers of the primary key of table 1.
For example:
Table 1 - primary key : abcd12349887/abcd22339887/abcder019987
In this case even if the primary key of table 1 is different, but when I extract the 1st 4 and last 4 chars, the output will be same abcd9887
So, when I use select to insert data, I get error of duplicate PK in table 2.
What I want is if the data of the PK is already present then don't add that record.
Here's my complete stored procedure:
INSERT INTO CPIPRODUCTFAMILIE
(productfamilieid, rapport, mesh, mesh_uitbreiding, productlabelid)
(SELECT DISTINCT (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4)))
productnummer,
ps.rapport, ps.mesh, ps.mesh_uitbreiding, ps.productlabelid
FROM productspecificatie ps, productgroep pg,
product p left join cpiproductfamilie cpf
on (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4))) = cpf.productfamilieid
WHERE p.productnummer = ps.productnummer
AND p.productgroepid = pg.productgroepid
AND cpf.productfamilieid IS NULL
AND pg.productietype = 'P'
**AND p.ROWID IN (SELECT MAX(ROWID) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))**
AND (CONCAT(SUBSTR(p.productnummer,1,2),SUBSTR(p.productnummer,8,4))) not in
(select productfamilieid from cpiproductfamilie));
The highlighted section seems to be wrong, and because of this the data is not picking up.
Please help
Try using this.
p.productnummer IN (SELECT MAX(productnummer) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))

Find which column differs between 2 rows?

We are using audit tables for each operational table, which stores the previous value of its operational equivalent plus change date, change type (UPDATE or DELETE) and its own auto incremental Primary Key.
So, for a table Users with columns UserID, Name, Email there would be a table xUsers with columns ID, OpererationType, OperationDate, UserID, Name, Email.
See that the xTable contains every column that its 'parent' does with 3 extra fields. This pattern is repeated for all tables used by our system.
table Users:
UserID int
Name nvarchar
Email nvarchar
table xUsers:
xUserID int
OpererationType int
OperationDate datetime
UserID int
Name nvarchar
Email nvarchar
Now, my question:
If I have a certain UserID, for which there is 2 entries in the xUsers table when the email was changed twice,
how would I construct a query that identifies which columns (can be more than 1) differ between the two rows in the audit table?
If I'm understanding this correctly, you'd like to create a query passing in the UserID as a parameter, which I'll call #UserID for the following example.
This query will select all rows from xUsers joined onto itself where there is a difference in a non-UserID column, using a series of case statements (one per column) to pull out specifically which columns differ.
SELECT *
, CASE
WHEN a.OperationType <> b.OperationType
THEN 1
ELSE 0
END AS OperationTypeDiffers
, CASE
WHEN a.OperationDate <> b.OperationDate
THEN 1
ELSE 0
END AS OperationDateDiffers
FROM xUsers a
JOIN xUsers b
ON a.xUserID < b.xUserID
AND a.UserID = b.UserID
AND (a.OperationType <> b.OperationType
OR a.OperationDate <> b.OperationDate) -- etc.
WHERE a.UserID = #UserID
You can put the rows of xUsers in a temporary table and then make a while cycle to go for each one and compare the results.
OR
You can do some dynamic SQL and use sysobjects and syscolumns tables to compare each result. It would be more dynamic and then it would be easy to implement for other tables.