MS SQL Store Procedure Optimizing - sql

I have attached my query result. How can I optimize this sp? Also do I need to optimize? I can get the result in 0.2 or in some case more.
Client Execution Time 18:18:18 18:18:08 18:17:49 18:17:24 18:13:18
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements 281 281 281 50 0 178.6000
Rows affected by INSERT, DELETE, or UPDATE statements 235 235 235 44 0 149.8000
Number of SELECT statements 4870 4870 4870 741 13 3072.8000
Rows returned by SELECT statements 3653 3653 3653 598 37 2318.8000
Number of transactions 281 281 281 50 0 178.6000
Network Statistics
Number of server roundtrips 1 1 1 3 3 1.8000
TDS packets sent from client 1 1 1 3 3 1.8000
TDS packets received from server 119 110 90 898 78 259.0000
Bytes sent from client 138 138 138 284 288 197.2000
Bytes received from server 327491 327491 327491 2861601 197860 808386.8000
Time Statistics
Client processing time 2755 3793 2364 908 332 2030.4000
Total execution time 3225 4294 2825 2095 1375 2762.8000
Wait time on server replies 470 501 461 1187 1043 732.4000

There's a number of options you can look at:
1.SQL Merge
SQL Merge can be used to perform Inserts, Updates and Deletes in a single statement.
http://technet.microsoft.com/en-us/library/bb510625.aspx
http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/
2.Output Clause
The SQL output clause can be used to return any value from ‘inserted’ and ‘deleted’ (New value and Old value) tables when doing an insert or update.
http://msdn.microsoft.com/en-us/library/ms177564(v=sql.90).aspx

Related

Dynamically Calculate difference columns based off slicer- POWERBI

I have a table with quarterly volume data, and a slicer that allows you to choose what quarter/year you want to see volume per code for. The slicer has 2019Q1 through 2021Q4 selections. I need to create dynamic difference column that will adjust depending on what quarter/year is selected in the slicer. I know I need to create a new measure using Calculate/filter but am a beginner in PowerBI and am unsure how to write that formula.
Example of raw table data:
Code
2019Q1
2019Q2
2019Q3
2019Q
2020Q1
2020Q2
2020Q3
2020Q4
11111
232
283
289
19
222
283
289
19
22222
117
481
231
31
232
286
2
19
11111
232
397
94
444
232
553
0
188
22222
117
411
15
14
232
283
25
189
Example if 2019Q1 and 2020Q1 are selected:
Code
2019Q1
2020Q1
Difference
11111
232
222
10
22222
117
481
-364
11111
232
397
-165
22222
117
411
-294
Power BI doesn't work that way. This is an Excel pivot table setup. You don't have any parameter to distinguish first and third or second and fourth row. They have the same code, so Power BI will aggregate their volumes. You could introduce a hidden index column but then why don't you simply stick to Excel? The Power BI approch to the problem would be to unpivot (stack) your table to a Code, Quarter and a Volume column, create 2 independent slicer tables for Minuend and Subtrahend and then CALCULATE your aggregated differences based on the SELECTEDVALUE of the 2 slicers.

matching customer id value in postgresql

new to learning sql/postgresql and have been hunting all over looking for help with a query to find only the matching id values in a table so i can pull data from another table for a learning project. I have been trying to use the count command, which doesn't seem right, and struggling with group by.
here is my table
id acct_num sales_tot cust_id date_of_purchase
1 9001 1106.10 116 12-Jan-00
2 9002 645.22 125 13-Jan-00
3 9003 1096.18 137 14-Jan-00
4 9004 1482.16 118 15-Jan-00
5 9005 1132.88 141 16-Jan-00
6 9006 482.16 137 17-Jan-00
7 9007 1748.65 147 18-Jan-00
8 9008 3206.29 122 19-Jan-00
9 9009 1184.16 115 20-Jan-00
10 9010 2198.25 133 21-Jan-00
11 9011 769.22 141 22-Jan-00
12 9012 2639.17 117 23-Jan-00
13 9013 546.12 122 24-Jan-00
14 9014 3149.18 116 25-Jan-00
trying to write a simple query to only find matching customer id's, and export them to the query window.

SQL: How to remove & update records in a table, sync'ing with a (Q)List data in order to decrease the burden on database/table?

I am using Qt and MS-Sql Server on Windows7 OS.
What I have is an MS-SQL database that I use to store data/info coming from equipment that is mounted in some vehicles.
There is a table in the database named TransactionFilesInfo - a table used to store information about transaction files from the equipment, when they connect to the TCP-server.
We are using this table as we are requested to avoid duplicate files. It happens (sometimes) when the remote equipment does NOT delete the transaction files after they are sent to the server. Hence, I use the info from the table to check [size and CRC] to avoid downloading duplicates.
Some sample data for TransactionFilesInfo table looks like this:
[TransactionFilesInfo]:
DeviceID FileNo FileSequence FileSize FileCRC RecordTimeStamp
10203 2 33 230 55384 2015-11-26 14:54:15
10203 7 33 624 55391 2015-11-26 14:54:15
10203 2 34 146 21505 2015-11-26 14:54:16
10203 7 34 312 35269 2015-11-26 14:54:16
10203 2 35 206 23022 2015-11-26 15:33:22
10203 7 35 208 11091 2015-11-26 15:33:22
10203 2 36 134 34918 2015-11-26 15:55:44
10203 7 36 104 63865 2015-11-26 15:55:44
10203 2 37 140 35466 2015-11-26 16:20:38
10203 7 37 208 62907 2015-11-26 16:20:38
10203 2 38 134 17706 2015-11-26 16:38:33
10203 7 38 104 42358 2015-11-26 16:38:33
11511 2 21 194 29913 2015-12-02 16:22:59
11511 7 21 114 30038 2015-12-02 16:22:59
On the other hand, every time a device connects to the server, it first sends a list of file information. The Qt application takes care of that.
The list contains elements like this:
struct FileInfo
{
unsigned short FileNumber;
unsigned short FileSequence;
unsigned short FileCRC;
unsigned long FileSize;
};
So, as an example (inspired by the table above) the connected device (DeviceID=10203) may say that it has the following files:
QList<FileInfo> filesList;
// here is the log4qt output...
filesList[0] --> FileNo=2 FileSeq=33 FileSize=230 and FileCRC=55384
filesList[1] --> FileNo=2 FileSeq=34 FileSize=146 and FileCRC=21505
filesList[2] --> FileNo=7 FileSeq=33 FileSize=624 and FileCRC=55391
filesList[3] --> FileNo=7 FileSeq=34 FileSize=312 and FileCRC=35269 ...
Well, what I need is a method to remove/delete, for a given DeviceID, all the records in the TransactionFilesInfo table, records that are NOT in the list sent by the remote device. Hence, I will be able to decrease the burden (size) on the database table.
Remark: For the moment I just delete (#midnight) all the records that are older than let's say 10 days, based on RecordTimeStamp field. So, the size of the table doesn't increase over an alarming level :)
Finally, to clarify it a little bit: I would mainly need help with SQL. Yet, I would not refuse any idea on how to do some related things/tricks on the Qt side ;)
The SQL to delete those records might look something like this:
DELETE FROM [SAMPLE DATA]
WHERE DeviceID = 10203
and 'File' + CONVERT(varchar(11),FileNo) + '_' +
RIGHT('000' + CONVERT(varchar(11),FileSequence),3)
NOT IN ('File2_033','File2_034','File7_033','File7_034',...)
If you wanted to delete all of them for a device, you could drop the code that looks at the FileNo and FileSequence so it is simply:
DELETE FROM [SAMPLE DATA]
WHERE DeviceID = 10203

How can I get the number of all bitcoins in the network?

I am trying to query the bitcoin daemon in order to find out what's the total amount of bitcoins mined/produced so far in order to calculate the market capitalization. However, I can't seem to find any command that does that.
I've checked the following link to no avail:
https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
You can find that information from the number of blocks. 50 BTC per block were created for the first 210 000 blocks, then 25 BTC per block for the next 210 000 blocks, etc.
If I take, say:
http://bitcoincharts.com/
as I write this SO answer I can read:
Blocks 279383
Total BTC 12.235M
Starting from 279 383 blocks you can find:
210 000 * 50 = 10 500 000
(279 383 - 210 000) * 25 = 1 734 575
10 5000 + 1 734 575 = 12 234 575
12 234 575 which that site rounded up as "12.235M"
This is not 100% correct as the first 50 bitcoins were not usable etc. moreover it's a fact that quite a lot of the early bitcoins mined are lost forever.
But that approximation should be "close enough" and seems to be what most sites are using.

SQL Query: How to pull counts of two coulmns from respective tables

Given two tables:
1st Table Name: FACETS_Business_NPI_Provider
Buss_ID NPI Bussiness_Desc
11 222 Eleven 222
12 223 Twelve 223
13 224 Thirteen 224
14 225 Fourteen 225
11 226 Eleven 226
12 227 Tweleve 227
12 228 Tweleve 228
2nd Table : FACETS_PROVIDERs_Practitioners
NPI PRAC_NO PROV_NAME PRAC_NAME
222 943 P222 PR943
222 942 P222 PR942
223 931 P223 PR931
224 932 P224 PR932
224 933 P224 PR933
226 950 P226 PR950
227 951 P227 PR951
228 952 P228 PR952
228 953 P228 PR953
With below query I'm getting following results whereas it is expected to have the provider counts from table FACETS_Business_NPI_Provider (i.e. 3 instead of 4 for Buss_Id 12 and 2 instead of 3 for Buss_Id 11, etc).
SELECT BP.Buss_ID,
COUNT(BP.NPI) PROVIDER_COUNT,
COUNT(PP.PRAC_NO)PRACTITIONER_COUNT
FROM FACETS_Business_NPI_Provider BP
LEFT JOIN FACETS_PROVIDERs_Practitioners PP
ON PP.NOI=BP.NPI
group by BP.Buss_ID
Buss_ID PROVIDER_COUNT PRACTITIONER_COUNT
11 3 3
12 4 4
13 2 2
14 1 0
If I understood it correctly, you might want to add a DISTINCT clause to the columns.
Here is an SQL Fiddle, which we can probably use to discuss further.
http://sqlfiddle.com/#!2/d9a0e6/3