I want ssms query (SQL query) which will help me in getting YTD data.
Example if I am in Jan month then take Jan data and create column to add month=1,
if I am in feb month then take Jan + feb and month column=2.
Which means when I will in feb I will get Jan with month=1 and Jan+ feb with month=2. And so on...
SELECT will be able to add months together for you, here's a query I ran on my crime database recently, you can change as needed:
--returns the total of all 2019
select LGA, ([Jan-2019] + [Feb-2019] + [Mar-2019] + [Apr-2019] + [May-2019] + [Jun-2019] + [Jul-2019] + [Aug-2019] + [Sep-2019] + [Oct-2019] + [Nov-2019] + [Dec-2019])
FROM Crime_LGA
alternatively, you can make a new column for each MTD, or maybe even a new table. Here's some code i used recently to add columns:
--to add a total 2019 column
ALTER TABLE Crime_Suburb
ADD Total_2010 int NOT NULL DEFAULT (0),
--to add values to the column
UPDATE Crime_Suburb
SET Total_2010 = ([Jan-2010] + [Feb-2010] + [Mar-2010] + [Apr-2010] + [May-2010] + [Jun-2010] + [Jul-2010] + [Aug-2010] + [Sep-2010] + [Oct-2010] + [Nov-2010] + [Dec-2010])
FROM Crime_Suburb
Related
We have a set of databases (80 in total). Every single one has a table called tblProfessions. The tables are not standardized. For example:
EDIT: all the databases are on the same server.
The DB1.dbo.tblProfessions is like:
intProfessionCode
strProfessionDescription
1
lawyer
2
dentist
...
...
30
doctor
And the DB72.dbo.tblProfessions is as follows:
intProfessionCode
strProfessionDescription
1
designer
2
butcher
...
...
80
chef
Suppose I ran a script from DBO1 to DBO72, and I found that the biggest table has 80 entries (in this case the DBO72 is the biggest one).
By my limited knowledge, all I know is to run the below script database by database, and write it down in a spreadsheet manually:
SELECT MAX(intProfessionCode) FROM [DB].dbo.tblProfessions;
Is there a script to run and loop through all the tblProfessions and get the one with the most entries? All I want is the biggest number found.
Thanks in advance.
You should be able to do something like this:
WITH dat
AS
(
SELECT 'db1' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB1.dbo.tblProfessions
UNION ALL
...
UNION ALL
SELECT 'db72' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB72.dbo.tblProfessions
)
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall
For situations like this, I usually query the catalog to write the above script rather than typing it out:
WITH
dat AS
(
SELECT STRING_AGG('SELECT ''' + QUOTENAME(s.name) + ''' AS db,
MAX(intProfessionCode) AS max_intProfessionCode
FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME('dbo') + '.' + QUOTENAME('tblProfessions') + '
UNION ALL',' ') AS s
FROM sys.databases s
WHERE s.name LIKE 'DB%' --EDIT APPROPRIATELY
)
SELECT 'WITH dat
AS
(' + SUBSTRING(s,1,LEN(s) - LEN(' UNION ALL')) + ')
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall' AS scrpt
FROM dat;
Make sure the above is only returning data for the appropriate databases by editing the WHERE clause in the CTE, then copy the output, paste elsewhere and run it to get your results.
I have table following this format :
Id BillNo: Voucher No:
1 W2015-16/0001 W2015-16/0001
2 W2015-16/0002 W2015-16/0002
3 W2015-16/0003 W2015-16/0003
4 W2015-16/0004 W2015-16/0004
5 W2015-16/0005 W2015-16/0005
6 W2015-16/0006 W2015-16/0006
7 W2015-16/0007 W2015-16/0007
8 W2015-16/0008 W2015-16/0008
9 W2015-16/0009 W2015-16/0009
10 W2015-16/0010 W2015-16/0010
But Instead of this Format:
Now I want to Update All "Voucher No" and "Bill No" below on this format :
Id BillNo: Voucher No:
1 W0001/2015-16 W0001/2015-16
2 W0002/2015-16 W0002/2015-16
3 W0003/2015-16 W0003/2015-16
4 W0004/2015-16 W0004/2015-16
5 W0005/2015-16 W0005/2015-16
6 W0006/2015-16 W0006/2015-16
7 W0007/2015-16 W0007/2015-16
8 W0008/2015-16 W0008/2015-16
9 W0009/2015-16 W0009/2015-16
10 W0010/2015-16 W0010/2015-16
11 W0011/2015-16 W0011/2015-16
like this i have a 1000+ records i should update , But i don't know is this possible to do in Lesser time, Kindly give your suggestion , i am new to SQL
Thanks Advance
Assuming everything is in a fixed format, then you would just use update and string manipulations:
update table t
set billno = left(billno, 1) + right(billno, 4) + '/' + substring(billno, 2, 7),
VoucherNo = left(VoucherNo, 1) + right(VoucherNo, 4) + '/' + substring(VoucherNo, 2, 7);
UPDATE YourTable
SET BillNo = SUBSTRING(BillNo,1,1) + SUBSTRING(BillNo,10,4) + '/' + SUBSTRING(BillNo,2,7)
, VoucherNo = SUBSTRING(VoucherNo,1,1) + SUBSTRING(VoucherNo,10,4) + '/' + SUBSTRING(VoucherNo,2,7)
Test:
DECLARE #a NVARCHAR(max) = 'W2015-16/0001'
SELECT SUBSTRING(#a,1,1) +
SUBSTRING(#a,10,4) + '/' + SUBSTRING(#a,2,7)
There is no straight forward way to achieve this. You may need to define some patterns in SQL to replace and play with all string functions. Here is
declare #name varchar(50) ='W2015-16/0001';
select SUBSTRING(STUFF(#name,2,0,(SUBSTRING(#name,CHARINDEX('/',#name) + 1,len(#name)) + '/')),0,14)
--W0001/2015-16
This gives you the result that you are looking for. So you need to set this into your update statement by joining with the table by ID. This would work only if the pattern you have given is correct.
I am facing issue to convert the sql query to oracle .Actually i am new to oracle db.
SELECT TI1.FOLDERRSN, DBO.F_OPENTAX_PROPERTYROLL_FOLDER(TI1.FOLDERRSN) ROLL,
TI1.DUEDATE DUEDATE, TI1.YEARFORBILLING,(TI1.SUMDUETAX + TI1.SUMPAIDTAX + TI1.SUMDUEPENALTY + TI1.SUMPAIDPENALTY) OUTSTANDING
FROM TAXINSTALLMENT TI1 WHERE (TI1.SUMDUETAX + TI1.SUMPAIDTAX + TI1.SUMDUEPENALTY + TI1.SUMPAIDPENALTY) > 0
AND EXISTS (SELECT * FROM TAXINSTALLMENT TI2 WHERE YEAR(TI2.DUEDATE) BETWEEN 1980 AND YEAR(GETDATE()) - 5 AND (TI2.SUMDUETAX + TI2.SUMPAIDTAX + TI2.SUMDUEPENALTY + TI2.SUMPAIDPENALTY) > 0
AND TI2.FOLDERRSN = TI1.FOLDERRSN ) ORDER BY TI1.FOLDERRSN, DUEDATE DESC
Anyone give me idea to change to oracle .
Thanks
The statement uses a function F_OPENTAX_PROPERTYROLL_FOLDER(TI1.FOLDERRSN) in Schema DBO. Make sure you have installed that function in that database Schema and have granted an execution right to the user in question or to PUBLIC for that matter.
EDIT: Moreover there is no function YEAR in Oracle. You must replace it with EXTRACT: EXTRACT(YEAR FROM TI2.DUEDATE) and for GetDate(): EXTRACT(YEAR FROM SYSDATE).
I'm querying my Access table with this query:
SELECT (VIN&' '&Make&' '&Model&' '&CarYear&' '&ExColor&' '&InColor&' ')as CarDescript
FROM TestTable
WHERE (WorkOrderNumber='$workorder')
GROUP BY AssignedEmp;
But a similar type of query works just fine in this SQL Fiddle
Even if I replace the long (VIN&' '&....) with VIN it still doesn't work.
EDIT: Schema of the table is
WorkOrderNumber - Priority - JobStage - WorkItem - AssignedEmp - DueDate - VIN - Make - ... - InColor
In general use + instead of & for SQL. (Access will allow this however).
In a group by you need to pick which one in the group to use (if you are using mysql like your example it just picks a random one, see this fiddle) so to fix this in the general case for your example:
SELECT (max(VIN) + ' ' + max(Make) + ' ' + max(Model) + ' ' + max(CarYear) + ' ' + max(ExColor) + ' ' + max(InColor) + ' ')
as CarDescript
FROM TestTable
WHERE WorkOrderNumber='$workorder'
GROUP BY AssignedEmp;
I have an SQL table with Member ID's.
Each member has information regarding their insurance claims listed in the table.
One column lists insurance ID's in relation to Member-ID.
for example:-
|Member ID | Insurance Code |
----------------------------------
|C#$###!1231 | 67 |
Now the issue is the same member could have different Insurance Code#'s as a part of previous claims.
For example between 2010 -2011, the member had a claim Code = 67
But for 2011 - 2012, the member had a claim Code = 3
Now when I create a SQL query, I only get one value for claim code.... how can I get all the values, say 67, 3, and 110? Such that I can respond to all claims that the member has been a part of.
// SQL QUERY to gather member information.
DMS.ADOQuery1.SQL.Clear;
DMS.ADOQuery1.SQL.Add('select HPFROMDT, HPthruDt, MEMBERKEY, MEMBHPKEY, OPFROMDT, OPTHRUDT, HPCODEKEY' +
' from MEMBHP' +
' where MEMBERKEY = ''' + MembKey + ''' and OPTHRUDT >= ''' + init_date + ''' and OPFROMDT <= ''' + final_date +''' and OPFROMDT < OPTHRUDT'
);
// Showmessage(DMS.ADOQuery1.SQL[0]);
DMS.ADOQuery1.Open;
// Adding the query values to the appropriate variables.
HPCodeKey := (DMS.ADOQuery1.FieldByNAme('HPCODEKEY').AsString);
DMS.ADOQuery1.Close;
Check all the records returned by query, not only the first one.
Check also filter dates in SQL statement (where condition).
// SQL QUERY to gather member information.
DMS.ADOQuery1.SQL.Clear;
DMS.ADOQuery1.SQL.Add('select HPFROMDT, HPthruDt, MEMBERKEY, MEMBHPKEY, OPFROMDT, OPTHRUDT, HPCODEKEY' +
' from MEMBHP' +
' where MEMBERKEY = ''' + MembKey + ''' and OPTHRUDT >= ''' + init_date + ''' and OPFROMDT <= ''' + final_date +''' and OPFROMDT < OPTHRUDT'
);
// Showmessage(DMS.ADOQuery1.SQL[0]);
DMS.ADOQuery1.Open;
while not DMS.ADOQuery1.Eof do
begin
// Adding the query values to the appropriate variables.
Showmessage(DMS.ADOQuery1.FieldByNAme('HPCODEKEY').AsString);
DMS.ADOQuery1.Next;
end;
DMS.ADOQuery1.Close;