Sql need to sum column several times and break result in one row horizontally

Evdo1

SQL needed to sum column several times and break result in one row by store horizontally, but I'm getting null rows mixed with totals need to take them out
Query:

 SELECT distinct IdPurch, 
  Datediff(week, (dateadd(day,-1, '2021-01-03')), '2021-01-09') AS wks,
  (Select sum(WgtSold)   where   store = '000001'  ) as lam,
  (Select sum(WgtSold)   where   store = '000002') as Riv  
  FROM [LAHAMARK].[dbo].[itemSale]
  LEFT OUTER Join [BRdata].[dbo].[Item]  on [LAHAMARK].[dbo].[itemSale].UPC15 = [BRdata].[dbo].[Item].[UPC]  
  LEFT OUTER join [BRdata].[dbo].[ItemMovement] on [LAHAMARK].[dbo].[itemSale].UPC15 = [BRdata].[dbo].[ItemMovement].[UPC]    
  where  Date  between   '2021-01-03' and  '2021-01-03'  and   idPurch = '1018'
  group by IdPurch,Store  

query Result :

enter image description here

enter image description here

Gordon Linoff

You seem to want conditional aggregation:

select IdPurch, 
       sum(case when store = '000001' then WgtSold end) as lam,
       sum(case when store = '000002' then WgtSold end) as Riv  
from [LAHAMARK].[dbo].[itemSale] its join
     [BRdata].[dbo].[Item] i
     on its.UPC15 = i.[UPC] join
     [BRdata].[dbo].[ItemMovement] im
     on its.UPC15 = im.[UPC]    
where Date  between '2021-01-03' and '2021-01-03'  and
      idPurch = 1018
group by IdPurch ;

Notes:

  • Table aliases make the query much easier to write and to read.
  • The week calculation is superfluous for only one week and has nothing really to do with the gist of the question, so I removed it.
  • There is no need to aggregate by store. You don't want a separate row for each store.
  • I removed the single quotes around 1018. It looks like a number and ids typically are numbers. If it is a string, definitely include the single quotes, though.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Convert one column with distinct values into several columns

分類Dev

SQL server random row for each unique result in column

分類Dev

Combine Multiple Rows into One Row (One Column) when Using SQL

分類Dev

How can i get the result from DB if one column have several keywrods?

分類Dev

SQL sum data between 2 dates in one column

分類Dev

How to combine multiple rows into one row and multiple column in SQL Server?

分類Dev

Is there a way to determine which combination of values that would result in lowest sum of one column?

分類Dev

In SQL Joins - one to many select one column multiple times based upon context

分類Dev

Sql returns row multiple times

分類Dev

SQL column sum and difference

分類Dev

One row test insertion to SQL Server RDS works but full load times out

分類Dev

Find row number of sum result in range

分類Dev

SQL Access, Sum one column's values only when ALL values in another column in are in specified range

分類Dev

Counting the amount of times several table names appear in a column of strings

分類Dev

Put the result of one specific row of a JOIN statement into a record variable in PL/SQL

分類Dev

MySQL SUM of one column, DISTINCT of ID column

分類Dev

Oracle SQL VARCHAR column with Line break

分類Dev

Store multiple values for multiple times in one column?

分類Dev

Do I need next() for a ResultSet with one row?

分類Dev

Select the sum of the column with the higher value in reach row

分類Dev

One row result with multiple join in MySQL

分類Dev

Condense multiple rows into one sum row

分類Dev

How to SUM two SUMmed SQL result fields

分類Dev

Regarding SQL sum return result that is unreadable

分類Dev

Sum specific MySQL table column and write the result in other column

分類Dev

SQL : How to sum multiple bit columns in a row

分類Dev

Performing sum per row based on conditions SQL

分類Dev

Can one separate column into several columns starting from the end of the line?

分類Dev

Executing function several times

Related 関連記事

  1. 1

    SQL Convert one column with distinct values into several columns

  2. 2

    SQL server random row for each unique result in column

  3. 3

    Combine Multiple Rows into One Row (One Column) when Using SQL

  4. 4

    How can i get the result from DB if one column have several keywrods?

  5. 5

    SQL sum data between 2 dates in one column

  6. 6

    How to combine multiple rows into one row and multiple column in SQL Server?

  7. 7

    Is there a way to determine which combination of values that would result in lowest sum of one column?

  8. 8

    In SQL Joins - one to many select one column multiple times based upon context

  9. 9

    Sql returns row multiple times

  10. 10

    SQL column sum and difference

  11. 11

    One row test insertion to SQL Server RDS works but full load times out

  12. 12

    Find row number of sum result in range

  13. 13

    SQL Access, Sum one column's values only when ALL values in another column in are in specified range

  14. 14

    Counting the amount of times several table names appear in a column of strings

  15. 15

    Put the result of one specific row of a JOIN statement into a record variable in PL/SQL

  16. 16

    MySQL SUM of one column, DISTINCT of ID column

  17. 17

    Oracle SQL VARCHAR column with Line break

  18. 18

    Store multiple values for multiple times in one column?

  19. 19

    Do I need next() for a ResultSet with one row?

  20. 20

    Select the sum of the column with the higher value in reach row

  21. 21

    One row result with multiple join in MySQL

  22. 22

    Condense multiple rows into one sum row

  23. 23

    How to SUM two SUMmed SQL result fields

  24. 24

    Regarding SQL sum return result that is unreadable

  25. 25

    Sum specific MySQL table column and write the result in other column

  26. 26

    SQL : How to sum multiple bit columns in a row

  27. 27

    Performing sum per row based on conditions SQL

  28. 28

    Can one separate column into several columns starting from the end of the line?

  29. 29

    Executing function several times

ホットタグ

アーカイブ