I want to have a filed on a table that I can increment myself based on variable in other tables so for instance I want to use the SQL
INSERT INTO Tab1( a, b, c )
SELECT ( x, y Z) FROM Tab2
WHERE......
But 'Z' is external from Tab2 and I need to increment it by another Variable for each record returned from Tab2
so the date in tab1 will end up
( FldaVal, fldbVal, 100 )
( FldaVal, fldbVal, 200 )
( FldaVal, fldbVal, 300 )
But the next time I run the insert command it may be
( FldaVal, fldbVal, 150 )
( FldaVal, fldbVal, 200 )
( FldaVal, fldbVal, 250 )
To End up with 6 Records as above
I will always have the Initial value i.e. 200 in the first instance and 150 in the second
Related
Need to plot data extracted for table into a pie-chart.
I am selecting data from a table to count tickets with different scenarios. I am able to simply select data to be plotted in excel.
But I need to select the same data in such a way that It can be plotted in pie-chart also.
SELECT Sum(CASE
WHEN Date(reportdate) < Date(current timestamp)
AND ( status NOT IN (SELECT value
FROM synonymdomain
WHERE maxvalue IN ( 'RESOLVED', 'CLOSED'
,
'REJECTED' )
AND domainid IN ( 'INCIDENTSTATUS'
)) )
AND incident.ir IS NOT NULL THEN 1
end) AS IMs_Balance_Carry_Forward,
( Sum(CASE
WHEN Date(reportdate) = Date(current timestamp)
AND ( status NOT IN (SELECT value
FROM synonymdomain
WHERE maxvalue IN (
'RESOLVED', 'CLOSED',
'REJECTED' )
AND domainid IN (
'INCIDENTSTATUS' )) )
AND ( incident.ir IS NOT NULL ) THEN 1
end) ) AS IM_Added_During_the_day,
from INCIDENT
Current Result:
IMS_BALANCE_CARRY_FORWARD IM_ADDED_DURING_THE_DAY
120 8
Required Result"
Column1 Column2
IMS_BALANCE_CARRY_FORWARD 120
IM_ADDED_DURING_THE_DAY 8
You want an unpivot capability, this has been answered here before. previous question and answer
I'm trying to INSERT into a table a column that is part of another column in another table using TSQL, but I get the error stating that there is more than one value returned when I used that subquery as an expression. I understand what causes the error, but I can't seem to think of a way to make it produce what I want.
I'm trying to do something similar to:
A.Base B.Reference C.Wanted
--- ---- ----
abcdaa aa abcdaa
bcdeab bb cdefbb
cdefbb cc efghcc
defgbc ddd fghddd
efghcc
fghddd
So I'm using the code:
INSERT INTO C ( [Some other column], Wanted )
SELECT
A.[Some other column],
, CASE
WHEN LEN( B.Reference ) = 2 THEN
( SELECT A.Base FROM A WHERE RIGHT( A.Base, 2 ) =
( SELECT B.Reference FROM B WHERE LEN( B.Reference ) = 2 )
)
WHEN LEN( B.Reference ) = 3 THEN
( SELECT A.Base FROM A WHERE RIGHT( A.Base, 3 ) =
( SELECT B.Reference FROM B WHERE LEN( B.Reference ) = 3 )
)
END
FROM
A
, B
Which will return me the "more than 1 value" error. Honestly, I'm probably making this way more convoluted than it needs to be, but I've been staring at these tables for a while now.
I hope I'm getting the idea across as to what I'm trying to do.
If you know the records aren't duplicate, and you are sure your JOIN between A and B works (as Martin mentioned) can't you just select distinct to return just the unique records?
I'd try it like this:
--Create a mockup with declared table variables and test data
DECLARE #tblA TABLE(someColumnInA VARCHAR(100));
DECLARE #tblB TABLE(someColumnInB VARCHAR(100));
DECLARE #tblC TABLE(someColumnInC VARCHAR(100));
INSERT INTO #tblA VALUES
('abcdaa')
,('bcdeab')
,('cdefbb')
,('defgbc')
,('efghcc')
,('fghddd')
INSERT INTO #tblB VALUES
('aa')
,('bb')
,('cc')
,('ddd');
--The query
INSERT INTO #tblC(someColumnInC)
SELECT SomeColumnInA
FROM #tblA a
WHERE EXISTS(SELECT 1 FROM #tblB b WHERE a.someColumnInA LIKE '%' + b.SomeColumnInB + '%');
SELECT * FROM #tblC;
The idea in short:
After creating a mockup (please do this next time in advance) we use a query to insert all values from #tblA into #tblC as long as there exists any value in #tblB, which is part of the current value in #tblA.
How about doing something like this?
select *
from A
where RIGHT(A.Base,2) IN (select B.Reference FROM B WHERE LEN(B.Reference) = 2)
UNION ALL
select *
from A
where RIGHT(A.Base,3) IN (select B.Reference FROM B WHERE LEN(B.Reference) = 3)
When I run the sub queries separately, I get unique dates for each row but together I am getting repeating rows. How do I correct?
SELECT a.*
,b.*
FROM (
SELECT DISTINCT serial_number_id
,s_start_dttm
,x_dept_id AS dept_a
,x_dept_name AS dept_name_a
,event_type_c AS event_type_a
,evnt.NAME AS event_name_a
FROM TABLE x
WHERE event_type_c = 3
) a
,(
SELECT DISTINCT serial_number_id
,s_end_dttm
,x_dept_id AS E_x_DEPT_ID
,x_DEPT_NAME AS E_x_DEPT_NAME
,event_type_c AS event_type_b
,event_name AS event_type_b
WHERE event_type_c = 4
) b
WHERE a.serial_number_id = b.serial_number_id
I need to select 10 or less rows with the SUM(FileSize) < 1000000. Results need to be ordered by AttachmentId. Let's say that if a single FileSize exceeds the limit it's okay just for that row (and no other) to be selected.
Ideally I'd like it to be just a select query with no more statements.
The table is:
CREATE TABLE [Attachment](
[AttachmentId] [int] NOT NULL,
[FileSize] [int] NOT NULL
)
Please help.
Updated. Sorry to hear that the requirements are unclear for most of the readers. There is no requirement to do any grouping. All I need to get is just plain first 10 rows or less. It will be less than 10 if their total on FileSize exceeds 1000000. It will be only 1 row if its FileSize equals 1000000 or more. The server is SQL 2008.
Updated. Many thanks to Nikola. We are getting there, but I'm still not sure how to implement the case when the first row exceeds FileSize of 1000000.
SELECT TOP 10 a.AttachmentId, rt.runningTotal
FROM Attachment a
CROSS APPLY (SELECT SUM(aa.FileSize) AS runningTotal
FROM Attachment aa
WHERE aa.AttachmentId <= a.AttachmentId
) AS rt
GROUP BY a.AttachmentId, rt.runningTotal
HAVING rt.runningTotal < 1000000
ORDER BY a.AttachmentId
Solution. This is the code (slightly modified) from Stuart which I accept as answer. Many thanks to Stuart!:
WITH CTE
AS ( SELECT TOP 10 AttachmentId, FileSize
, RunningID = ROW_NUMBER() OVER (ORDER BY AttachmentId)
FROM Attachment
)
SELECT AttachmentId, FileSize
FROM CTE AS a
WHERE (SELECT SUM(FileSize)
FROM CTE
WHERE RunningID <= a.RunningID
) <= 10000000
OR a.RunningID = 1
Maybe this will get you started:
BEGIN TRAN
CREATE TABLE [Attachment]
(
[AttachmentId] [int] NOT NULL
, [FileSize] [int] NOT NULL
)
INSERT INTO Attachment
SELECT 1
, 10
UNION
SELECT 2
, 20
UNION
SELECT 3
, 30
--values to exceed
DECLARE #p INT = 50
--row count to restrict to
DECLARE #r INT = 10
;
WITH CTE
AS ( SELECT AttachmentID
, FileSize
, RunningID = ROW_NUMBER() OVER ( ORDER BY FileSize DESC )
FROM Attachment
)
SELECT TOP ( #r )
AttachmentID
, FileSize
FROM CTE AS A
WHERE ( SELECT SUM(FileSize)
FROM CTE
WHERE RunningID <= A.RunningID
) <= #p
OR A.RunningID = 1
ROLLBACK
SELECT TOP 10 AttachmentId
FROM Attachment
GROUP BY AttachmentId
HAVING SUM(FileSize) < 1000000
ORDER BY AttachmentId
This query gives me 2 rows but the result is presented a just 1 row. I dont understand why. I want the result as 2 rows.
SELECT *
FROM table AS S1
INNER JOIN table AS S2
ON S1.code = S2.code
WHERE S1.column1 IS NULL
AND S2.column1 IS NOT NULL
Here is the ouptput that i am expecting:
EXPECTED OUTPUT:
ID login email code column1
--------------------------------------------
96 testid1 test-Email1 XPQR NULL
97 testid1 test-Email1 XPQR P
Just a wild guess:
SELECT *
FROM table AS S1
WHERE EXISTS
( SELECT *
FROM table AS S2
WHERE ( S2.code = S1.code )
AND ( ( S1.column1 IS NULL AND S2.column1 IS NOT NULL )
OR ( S2.column1 IS NULL AND S1.column1 IS NOT NULL )
)
) ;