This table is made by pulling a variable number of columns from several workbooks into one. I keep track of the column index for "Key1" and "Key2" with integers "i1" and "i2", respectively.
+-------------+-----------+-------------+-----------+-----------+--------+-------------+-----------+-------+
| Thisjust | Indexcode | Specialcode | Key1 | Indexcode | Some | Specialcode | Key2 | MATCH |
+-------------+-----------+-------------+-----------+-----------+--------+-------------+-----------+-------+
| isanexample | 101 | 27223 | 101_27223 | 101 | Values | 1112 | 101_1112 | |
| isanexample | 102 | 27224 | 102_27224 | 102 | Values | 1113 | 102_1113 | |
| isanexample | 103 | 27225 | 103_27225 | 103 | Values | 1114 | 103_1114 | |
| isanexample | 104 | 27226 | 104_27226 | 104 | Values | 1115 | 104_1115 | |
| isanexample | 108 | 11130 | 108_11130 | 105 | Values | 1116 | 105_1116 | Yes |
| isanexample | 106 | 27228 | 106_27228 | 106 | Values | 1117 | 106_1117 | |
| isanexample | 107 | 27229 | 107_27229 | 107 | Values | 1118 | 107_1118 | |
| isanexample | 110 | 27227 | 110_27227 | 108 | Values | 11130 | 108_11130 | Yes |
| isanexample | 109 | 37345 | 109_37345 | 109 | Values | 1112 | 109_1112 | |
| isanexample | 105 | 37346 | 105_37346 | 110 | Values | 1113 | 110_1113 | |
| isanexample | 111 | 37347 | 111_37347 | 111 | Values | 1114 | 111_1114 | |
| isanexample | 112 | 37348 | 112_37348 | 112 | Values | 1115 | 112_1115 | |
| isanexample | 113 | 37349 | 113_37349 | 113 | Values | 1116 | 113_1116 | |
| isanexample | 114 | 37350 | 114_37350 | 114 | Values | 1117 | 114_1117 | |
| isanexample | 115 | 37351 | 115_37351 | 115 | Values | 1118 | 115_1118 | |
| isanexample | 116 | 11138 | 116_11138 | 116 | Values | 11138 | 116_11138 | Yes |
| isanexample | 117 | 11139 | 117_11139 | 117 | Values | 11139 | 117_11139 | Yes |
| isanexample | 118 | 11140 | 118_11140 | 118 | Values | 11140 | 118_11140 | Yes |
+-------------+-----------+-------------+-----------+-----------+--------+-------------+-----------+-------+
I am checking if
EITHER key1 in a particular row matches one of the key2 cells
OR if key2 in a particular row matches one of the key1 cells
I used this formula to accomplish that
=IF(AND(ISNA(MATCH(H2,$D$2:$D$19,0)),ISNA(MATCH(D2,$H$2:$H$19,0))),"","Yes")
Knowing that this whole table could have a wildly different number of columns, and the order of columns is also variable:
How do I do my match function in VBA? The MATCH column will always be at the end.
If you are fine with creating a Table (ListObject) from your data, your MATCH column formula will be =IF(AND(ISNA(MATCH([Key1],[Key2],0)),ISNA(MATCH([Key2],[Key1],0))),"","Yes").
This references the Key1 and Key2 columns based on their headers, eliminating the problem of a changing number and/or order of columns.
Related
I'm a bit new in sql.
I have the following table:
+-----+---------+------------------------+
| ID | ID_TEST | FILE_PATH |
+-----+---------+------------------------+
| 575 | 3 | Landscapes_001_h_A.jpg |
| 576 | 3 | Landscapes_001_h_B.jpg |
| 577 | 3 | Landscapes_001_h_C.jpg |
| 578 | 3 | Landscapes_001_h_D.jpg |
| 579 | 3 | Landscapes_001_h_E.jpg |
| 580 | 3 | Landscapes_002_h_A.jpg |
| 581 | 3 | Landscapes_002_h_B.jpg |
| 582 | 3 | Landscapes_002_h_C.jpg |
| 583 | 3 | Landscapes_002_h_D.jpg |
| 584 | 3 | Landscapes_002_h_E.jpg |
+-----+---------+------------------------+
The pattern for picture is Landscapes_XXX_h_Y.jpg
where
XXX is number from 1 to 185 and Y is quality version from A to E
I wanna select each image name with different quality.
The output should be
+-----+---------+------------------------+
| ID | ID_TEST | FILE_PATH |
+-----+---------+------------------------+
| 575 | 3 | Landscapes_001_h_A.jpg |
| 576 | 3 | Landscapes_002_h_E.jpg |
| 577 | 3 | Landscapes_003_h_C.jpg |
| 578 | 3 | Landscapes_004_h_B.jpg |
| 579 | 3 | Landscapes_005_h_D.jpg |
| 580 | 3 | Landscapes_006_h_A.jpg |
| 581 | 3 | Landscapes_007_h_E.jpg |
| 582 | 3 | Landscapes_008_h_C.jpg |
| 583 | 3 | Landscapes_009_h_B.jpg |
| 584 | 3 | Landscapes_010_h_E.jpg |
+-----+---------+------------------------+
but of course for 185 elements.
I'm using 5.5.60-MariaDB.
How to write SELECT statement? Using REGEXP?
In SQL Server 2016, I need to create nearly exact copies of records from 2 tables. The only difference will be their primary keys, one other column that I'm resetting to zero, and a foreign key in my 2nd table (which is the PK of my 1st table). I can create copies of both tables fine, however I don't know how to assign new FK values to the 2nd table, to correctly reflect the new primary keys from the 1st table.
Here are the current records in both tables:
Table 1: Batches
+-----------------------+-----------+----------------+------------+
| BatchID (pk identity) | StartDate | ProcessingStep | BatchCount |
+-----------------------+-----------+----------------+------------+
| 1 | 5/10/2019 | 2 | 8203 |
| 2 | 5/11/2019 | 2 | 345 |
| 3 | 5/12/2019 | 2 | 5014 |
+-----------------------+-----------+----------------+------------+
Table 2: ItemList
+--------------------------+---------+--------+-----------+-------------+
| ItemListID (pk identity) | BatchID | ItemID | Processed | ProcessDate |
+--------------------------+---------+--------+-----------+-------------+
| 1000 | 1 | 201 | 1 | 5/10/2019 |
| 1001 | 1 | 689 | 1 | 5/10/2019 |
| 1002 | 2 | 548 | 1 | 5/11/2019 |
| 1003 | 2 | 693 | 1 | 5/11/2019 |
| 1004 | 3 | 123 | 1 | 5/12/2019 |
| 1005 | 3 | 999 | 1 | 5/12/2019 |
+--------------------------+---------+--------+-----------+-------------+
I now want to create copies of these records with the following exceptions:
Batches.ProcessingStep for all records will now be set to zero
ItemList's Processed & ProcessDate are reset to zero & null respectively
Update ItemList.BatchID to reflect the new PK of the copied Batches records (this is where I'm having trouble)
Currently, my script for updating my tables is as follows:
INSERT INTO Batches(StartDate, ProcessingStep, BatchCount)
SELECT StartDate, 0, BatchCount
FROM Batches
WHERE BatchID IN (1,2,3)
INSERT INTO ItemList(BatchID, ItemID, Processed, ProcessDate)
SELECT <<?? not sure ??>>, ItemID, 0, NULL
WHERE ItemListID BETWEEN 1000 AND 1005
And here would be my final results:
Table 1: Batches
+---------+-----------+----------------+------------+
| BatchID | StartDate | ProcessingStep | BatchCount |
+---------+-----------+----------------+------------+
| 1 | 5/10/2019 | 2 | 8203 |
| 2 | 5/11/2019 | 2 | 345 |
| 3 | 5/12/2019 | 2 | 5014 |
| 4 | 5/10/2019 | 0 | 8203 |
| 5 | 5/11/2019 | 0 | 345 |
| 6 | 5/12/2019 | 0 | 5014 |
+---------+-----------+----------------+------------+
Table 2: ItemList
+------------+---------+--------+-----------+-------------+
| ItemListID | BatchID | ItemID | Processed | ProcessDate |
+------------+---------+--------+-----------+-------------+
| 1000 | 1 | 201 | 1 | 5/10/2019 |
| 1001 | 1 | 689 | 1 | 5/10/2019 |
| 1002 | 2 | 548 | 1 | 5/11/2019 |
| 1003 | 2 | 693 | 1 | 5/11/2019 |
| 1004 | 3 | 123 | 1 | 5/12/2019 |
| 1005 | 3 | 999 | 1 | 5/12/2019 |
| 1006 | 4 | 201 | 0 | NULL |
| 1007 | 4 | 689 | 0 | NULL |
| 1008 | 5 | 548 | 0 | NULL |
| 1009 | 5 | 693 | 0 | NULL |
| 1010 | 6 | 123 | 0 | NULL |
| 1011 | 6 | 999 | 0 | NULL |
+------------+---------+--------+-----------+-------------+
How would I go about populating that ItemList.BatchID foreign key correctly?
Thanks.
I am having trouble in SQl query,The query result should be like this
+------------+------------+-----+------+-------+--+--+--+
| District | Tehsil | yes | no | Total | | | |
+------------+------------+-----+------+-------+--+--+--+
| ABBOTTABAD | ABBOTTABAD | 377 | 5927 | 6304 | | | |
| ABBOTTABAD | HAVELIAN | 112 | 2276 | 2388 | | | |
| ABBOTTABAD | Overall | 489 | 8203 | 8692 | | | |
| CHARSADDA | CHARSADDA | 289 | 3762 | 4051 | | | |
| CHARSADDA | SHABQADAR | 121 | 1376 | 1497 | | | |
| CHARSADDA | TANGI | 94 | 1703 | 1797 | | | |
| CHARSADDA | Overall | 504 | 6841 | 7345 | | | |
+------------+------------+-----+------+-------+--+--+--+
The overall total should be should be shown at the end of every parent category but now it is showing like this
+------------+------------+-----+------+-------+--+--+--+
| District | Tehsil | yes | no | Total | | | |
+------------+------------+-----+------+-------+--+--+--+
| ABBOTTABAD | ABBOTTABAD | 377 | 5927 | 6304 | | | |
| ABBOTTABAD | HAVELIAN | 112 | 2276 | 2388 | | | |
| ABBOTTABAD | Overall | 489 | 8203 | 8692 | | | |
| CHARSADDA | CHARSADDA | 289 | 3762 | 4051 | | | |
| CHARSADDA | Overall | 504 | 6841 | 7345 | | | |
| CHARSADDA | SHABQADAR | 121 | 1376 | 1497 | | | |
| CHARSADDA | TANGI | 94 | 1703 | 1797 | | | |
+------------+------------+-----+------+-------+--+--+--+
My query is sorting second column with respect to first column although order by query is applied on my first column. This is my query
select District as 'District', tName as 'tehsil',[1] as 'yes',[0] as 'no',ISNULL([1]+[0], 0) as "Total" from
(
select d.Name as 'District',
case when grouping (t.Name)=1 then 'Overall' else t.Name end as tName,
BoundaryWallAvailable,
count(*) as total from School s
INNER JOIN SchoolIndicator i ON (i.refSchoolID=s.SchoolID)
INNER JOIN Tehsil t ON (t.TehsilID=s.refTehsilID)
INNER JOIN district d ON (d.DistrictID=t.refDistrictID)
group by
GROUPING sets((d.Name, BoundaryWallAvailable), (d.Name,t.Name, BoundaryWallAvailable))
) B
PIVOT
(
max(total) for BoundaryWallAvailable in ([1],[0])
) as Pvt
order by District
P.S: BoundaryWall is one column through pivoting i am breaking it into Yes and No Column
I have following data in a table
+---------+-----+----------+
| Zip_ cd | id | assignmt.|
|---------+-----+----------+
| 1812 | 777 | S |
| 1812 | 111 | P |
| 1451 | 878 | S |
| 55 | 45 | x |
| 55 | 646 | T |
| 55 | 455 | Z |
+---------+-----+----------+
I want to transpose it as following
+---------+-----+----+---------+-----+----------+---------+-----+----------+
| Zip_ cd | id | ass| Zip_cd1 | id1 |assignmt1 | Zip_cd2 | id2 |assignmt3 |
+---------+-----+----+---------+-----+----------+---------+-----+----------+
| 1812 | 777 | S | 1812 | 111 | P | 1812 | 111 | P |
| 1451 | 878 | S | | | | | | |
| 55 | 45 | X | 55 | 646 | T | 55 | 455 | Z |
+---------+-----+----------+---------+----------+---------+-----+----------+
So, I basically want to transpose based on the zip code. If 2 rows have same zip code its needs to be in single row.
Another query using pivot functionality:
select *
from table_name
pivot ( max(id) id
, max(assignmt) assignmt
FOR assignmt IN ('S' S,
'P' P));
+---------+-------+------------+--------+------------+
| ZIP_ CD | S_ID | S_ASSIGNMT.| P_ID | P_ASSIGNMT.|
+---------+-------+------------+--------+------------+
| 1812 | 777 | S | 111 | P |
| 1451 | 878 | S | NULL | NULL |
+---------+-------+------------+--------+------------+
I've been struggling with this for a while, I have two different tables that share a column but both have different amount of rows.
One of the tables is for money requests (table 1) and the other one is for proving the expenses (table 2)
Table 1
+-----------+-----------+
|expenseid | requestid |
+-----------+-----------+
| 16333 | 7454 |
| NULL | 7455 |
| 16336 | 7456 |
| 16338 | 7457 |
| NULL | 7458 |
| 16341 | 7459 |
| 16345 | 7460 |
| NULL | 7461 |
| NULL | 7462 |
+-----------+-----------+
Table 2
+-----------+-----------+
|expenseid | amount |
+-----------+-----------+
| 16333 | 200 |
| 16334 | 150 |
| 16335 | 300 |
| 16336 | 900 |
| 16337 | 100 |
| 16338 | 120 |
| 16339 | 700 |
| 16340 | 431 |
| 16341 | 420 |
| 16342 | 150 |
| 16343 | 240 |
| 16344 | 465 |
| 16345 | 200 |
| 16346 | 120 |
| 16347 | 90 |
| 16348 | 50 |
| 16349 | 245 |
+-----------+-----------+
As you can see the tables share the same column 'expenseid', but the amount of rows is different and there are two different columns that don't correspond to each other, i would like to have a table as follows
Combined table
+-----------+-----------+-----------+
|expenseid | amount | requestid |
+-----------+-----------+-----------+
| 16333 | 200 | 7454 |
| NULL | NULL | 7455 |
| 16334 | 150 | NULL |
| 16335 | 300 | NULL |
| 16336 | 900 | 7456 |
| 16337 | 100 | NULL |
| 16338 | 120 | 7457 |
| NULL | NULL | 7458 |
| 16339 | 700 | NULL |
| 16340 | 431 | NULL |
| 16341 | 420 | 7459 |
| 16342 | 150 | NULL |
| 16343 | 240 | NULL |
| 16344 | 465 | NULL |
| 16345 | 200 | 7460 |
| NULL | NULL | 7461 |
| NULL | NULL | 7462 |
| 16346 | 120 | NULL |
| 16347 | 90 | NULL |
| 16348 | 50 | NULL |
| 16349 | 245 | NULL |
+-----------+-----------+-----------+
I've managed to merge both tables in a way it shows the null values for expenseid related to table 1 with a left outer join, but it doesn't show the null values for the column requestid, any ideas on how to do this?
You need a FULL OUTER JOIN instead of a LEFT OUTER JOIN.
SELECT
COALESCE(Table1.expenseid, Table2.expenseid) AS expenseid,
amount,
requestid
FROM Table1
FULL OUTER JOIN Table2
ON Table1.expenseid = Table2.expenseid
Results:
EXPENSEID AMOUNT REQUESTID
16333 200 7454
(null) (null) 7455
16336 900 7456
16338 120 7457
...etc...
See it working online: sqlfiddle