How to select two rows into a single row output - sql

The select statement looks like this right now
Here i got the output in 2 lines for the same stg_edi835_id ,I want to select the results in a single line for that stg_edi835_id.
Output should look like this
Can some please help me in doing this
Thanks in advance..

SELECT STG_EDI835_PLB_ID, STG_EDI835_ID, ADJUSTMENTREASONCODE1, ADJUSTMENTIDENTIFIER1, SUM(NTVE_ADJUSTMENTAMOUNT1_T1+NTVE_ADJUSTMENTAMOUNT1_T2) AS ADJUSTMENTAMOUNT1,
SUM(PTVE_ADJUSTMENTAMOUNT1_T1+PTVE_ADJUSTMENTAMOUNT1_T2) AS ADJUSTMENTAMOUNT2, ADJUSTMENTREASONCODE2, ADJUSTMENTIDENTIFIER2
FROM
(
SELECT T1.STG_EDI835_PLB_ID , T2.STG_EDI835_ID, T1.ADJUSTMENTREASONCODE1, T1.ADJUSTMENTIDENTIFIER1,
(CASE WHEN T1.ADJUSTMENTAMOUNT1 < 0 THEN T1.ADJUSTMENTAMOUNT1 ELSE 0 END) AS NTVE_ADJUSTMENTAMOUNT1_T1,
(CASE WHEN T2.ADJUSTMENTAMOUNT1 < 0 THEN T2.ADJUSTMENTAMOUNT1 ELSE 0 END) AS NTVE_ADJUSTMENTAMOUNT1_T2,
(CASE WHEN T1.ADJUSTMENTAMOUNT1 >= 0 THEN T1.ADJUSTMENTAMOUNT1 ELSE 0 END) AS PTVE_ADJUSTMENTAMOUNT1_T1,
(CASE WHEN T2.ADJUSTMENTAMOUNT1 >= 0 THEN T2.ADJUSTMENTAMOUNT1 ELSE 0 END) AS PTVE_ADJUSTMENTAMOUNT1_T2,
COALESCE(T2.ADJUSTMENTREASONCODE1, 'NULL') AS ADJUSTMENTREASONCODE2, COALESCE(T2.ADJUSTMENTIDENTIFIER1, NULL) AS ADJUSTMENTIDENTIFIER2
FROM TABLE1 AS T1
INNER JOIN TABLES T2
ON T2.STG_EDI835_ID = T1.STG_EDI835_ID
AND T2.STG_EDI835_PLB_ID = T1.STG_EDI835_PLB_ID
) A
GROUP BY STG_EDI835_PLB_ID, STG_EDI835_ID, ADJUSTMENTREASONCODE1, ADJUSTMENTIDENTIFIER1, ADJUSTMENTREASONCODE2, ADJUSTMENTIDENTIFIER2

Your question is somewhat incomplete (you should show your desired output), however, here is a sample of what you could do:
get rid of all unique values you don't need. (column 1, containing ID's, etc.)
Use aggregate functions on the rest.
Example:
Select
--column 1 removed
MAX(column2) as ID,
MAX(column3) as RefID,
--column 4 removed
--column 5 removed
--column 6 removed
SUM(column7) as Ad1,
--column 8 removed
--column 9 removed
SUM(column10) as Ad2
From
table

Try something like
WITH DATA As (
select 697 as Stg_EDI835_Id, -87.75 as AdjustmentAmount1 union
select 697, -4.64 union
select 612, -6.39 union
select 612, 60.75
)
select SUM(AdjustmentAmount1) AS AdjustmentAmount1, 0 AS adjustmentamount2 FROM DATA GROUP BY Stg_EDI835_Id HAVING SUM(AdjustmentAmount1) <= 0 UNION
select 0, SUM(AdjustmentAmount1)FROM DATA GROUP BY Stg_EDI835_Id HAVING SUM(AdjustmentAmount1) > 0
Output is
AdjustmentAmount1 | Adjustmentamount2
-92.39 | 0.00
0.00 | 54.36

Related

Compare values in column, return a value based on what is in the columns

I have a table that has 3 columns, ID, Record and signed.... ID is unique but RecordID duplicates because each row is identifying if someone signed off on something in the record. The 3rd column is if they signed off on it or not.
Below is an example of what the table could look like.
What I'm trying to do is return a 1 if the record is completely signed off on or or a 0 if the record isn't completely signed off on....
Here is an example of the table
My output should be like this 5099 =0, 5100 = 0, 5101 = 1, 5102 = 0
I've been racking my head on it
I've tried the following code and it seems to work on items that have more than two rows. But if they have two rows and one signed is yes and the other no I'm getting a 1
select
(case
when not exists(
select *
From Table
Where signed<> '' no) then 0
Else '1'
end);
my output should be like this 5099 =0, 5100 = 0, 5101 = 1, 5102 = 0
SELECT RecordID, CASE WHEN n_signed=n THEN 1 ELSE 0 END FROM
(
SELECT RecordID, SUM(CASE WHEN signed='yes' THEN 1 ELSE 0 END) as n_signed, COUNT(*) as n
FROM table
GROUP BY RecordID
) a
You probably need something like this
select
recordid,
floor(avg(case when signed='yes' then 1 else 0 end)) as signed_status
from your_table
group by recordid;
Demo

Trying to find unique records in a table that don't have a negating record

I have a table with a whole bunch of records.
table looks like this (simplified):
ID DoID DoQty DoType DoValue
1 17 1 Door 15
2 17 -1 Door -15
3 18 1 Window 75
4 19 1 Bed 125
5 19 1 Bed 134
so this is what I'd like to pull
ID DoId DoQty DoType DoValue
3 18 1 WIndows 75
4 19 1 Bed 125
5 19 1 Bed 134
I don't need DoID=17 because it has a 2nd line where DoQty is -1. SO that overall DoQty = 0. I only need records where there isn't a DoQty=-1. The problem here is that I do not want to group by DoID I want to be able to see the whole record line (no group by)
EDIT:
Unfortunately I might not have explained my question correctly. Basically, if I run the following query, i get the correct counts, however my goal is to get the details of each line.
SELECT t.DoID,
'Available' = Sum(t.DoQty)
From t
GROUP BY t.DoID
This gives me grouped results from which I can't do anything with.
As i understand you dont what to record where has any negative DoQty in any row. If it is correct, a possible solution is below,
SELECT t1.ID, t1.DoID, t1.DoQty, t1.DoType, t1.DoValue
FROM table t1
LEFT JOIN table t2 ON t2.DoQty < 0 AND t1.DoID = t2.DoID
WHERE t2.DoID IS NULL
We can use conditional aggregation here:
WITH cte AS (
SELECT DoID
FROM yourTable
GROUP BY DoID
HAVING SUM(CASE WHEN DoQty < 0 THEN 1 ELSE 0 END) = 0
)
SELECT *
FROM yourTable
WHERE DoID IN (SELECT DoID FROM cte);
This would return every DoID whose group of records does not have any DoQty values which are negative.
Is this what you want?
select t.*
from t
where not exists (select 1 from t t2 where t2.doid = t.doid and t2.doqty = - t.doqty);
This filters out rows where the "negative" value exists.
What I'm trying to do here is only pull up DoID's that don't have have a DoQty>0
Try this...
SELECT DoID
FROM table tbl
WHERE NOT EXISTS (
SELECT DoID FROM table WHERE DoQty <= 0 AND DoID = tbl.DoID
);
You can try the below:
With Agg AS
(
select DOID,sum(DoQty) as Qty from TableName
group by DOID having sum(DoQty)>0
)
select T.* from TableName T Inner Join Agg A
on T.DOID=A.DOID;
You seems want :
SELECT *, SUM(t.DoQty) OVER (PARTITION BY t.DoID) as Available
From t;

SQL - Find the binary representation from the place of '1's

It is really hard to find a good title for this.
Here is the question: I have a SELECT query GROUP BY a field which returns me up to three values (1,2,3). These values are representing the positions of '1' in a binary number.
In other words:
Query Output | Reult
0,1,2 | 7 (111)
1,2 | 6 (110)
3 | 1 (001)
- | 0 (000)
Ok, I know it is easy. But there are two constraints. First, I want a query not a function/store procedure. Second, the result should be a string (like '010') not the number.
I found the solution for integer value, but not the string (varchar)
SELECT COALESCE(sum(power(2, field)), 0) AS test FROM (
SELECT field FROM myTable GROUP BY field) a
I am using SQL server 2008, just in case.
I also have this solution, but this one cannot be extended to bigger number of outputs:
SELECT output =
CASE TEST
WHEN 0 THEN '000'
WHEN 1 THEN '001'
WHEN 2 THEN '010'
WHEN 3 THEN '011'
WHEN 4 THEN '100'
WHEN 5 THEN '101'
WHEN 6 THEN '110'
WHEN 7 THEN '111'
END
FROM(
select COALESCE(sum(power(2, 3 - field)), 0) as test from (
select field from myTable group by field) a) b
You can use binary and and string concatenation:
select (case when test&4 > 0 then '1' else '0' end) +
(case when test&2 > 0 then '1' else '0' end) +
(case when test&1 > 0 then '1' else '0' end)
from (select 6 as test) t;
If you are allergic to case statements, you could do this:
select CHAR(ascii(0) + (test&4)/4) +
CHAR(ascii(0) + (test&2)/2) +
CHAR(ascii(0) + (test&1)/1)
from (select 6 as test) t

Select Distinct Attribute and Print out Count of another even when the count is 0

I don't quite know how I should describe the problem for title, but here's my question.
I have a table named hello with two columns named time and state.
Time | State
Here's an example of the data I have
1 DC
1 VA
1 VA
2 DC
2 MD
3 MD
3 MD
3 VA
3 DC
I would like to get all the possible time and the count of "VA" (0 if "VA" doesn't appear at the time)
The output would look like this
Time Number
1 2
2 0
3 1
I tried to do
SELECT DISTINCT time,
COUNT(state) as Number
FROM hello
WHERE state = 'VA'
GROUP BY time
but it doesn't seem to work.
This is a conditional aggregation:
select time, sum(case when state = 'VA' then 1 else 0 end) as NumVA
from hello
group by time
I want to add that you should never use distinct when you have a group by. The two are redundant. Distinct as a keyword is not even needed in the SQL language; semantically, it is just shorthand for grouping by all the columns.
SELECT TIME,
SUM(CASE WHEN State = 'VA' THEN 1 ELSE 0 END)
FROm tableName
GROUP BY Time
SQLFiddle Demo
One rule of thumb is to get your counts first and put them into a temp for use later.
See below:
Create table temp(Num int, [state] varchar(2))
Insert into temp(Num,[state])
Select 1,'DC'
UNION ALL
Select 1,'VA'
UNION ALL
Select 1,'VA'
UNION ALL
Select 2,'DC'
UNION ALL
Select 2,'MD'
UNION ALL
Select 3,'MD'
UNION All
Select 3,'MD'
UNION ALL
Select 3,'VA'
UNION ALL
Select 3,'DC'
Select t.Num [Time],t.[State]
, CASE WHEN t.[state] = 'VA' THEN Count(t.[State]) ELSE 0 END [Number]
INTO #temp2
From temp t
Group by t.Num, t.[state]
--drop table #temp2
Select
t2.[time]
,SUM(t2.[Number])
From #temp2 t2
group by t2.[time]

Get the distinct count of values from a table with multiple where clauses

My table structure is this
id last_mod_dt nr is_u is_rog is_ror is_unv
1 x uuid1 1 1 1 0
2 y uuid1 1 0 1 1
3 z uuid2 1 1 1 1
I want the count of rows with:
is_ror=1 or is_rog =1
is_u=1
is_unv=1
All in a single query. Is it possible?
The problem I am facing is that there can be same values for nr as is the case in the table above.
Case statments provide mondo flexibility...
SELECT
sum(case
when is_ror = 1 or is_rog = 1 then 1
else 0
end) FirstCount
,sum(case
when is_u = 1 then 1
else 0
end) SecondCount
,sum(case
when is_unv = 1 then 1
else 0
end) ThirdCount
from MyTable
you can use union to get multiple results e.g.
select count(*) from table with is_ror=1 or is_rog =1
union
select count(*) from table with is_u=1
union
select count(*) from table with is_unv=1
Then the result set will contain three rows each with one of the counts.
Sounds pretty simple if "all in a single query" does not disqualify subselects;
SELECT
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_ror=1 OR is_rog=1) cnt_ror_reg,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_u=1) cnt_u,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_unv=1) cnt_unv;
how about something like
SELECT
SUM(IF(is_u > 0 AND is_rog > 0, 1, 0)) AS count_something,
...
from table
group by nr
I think it will do the trick
I am of course not sure what you want exactly, but I believe you can use the logic to produce your desired result.